diff --git a/.github/workflows/build-core.yml b/.github/workflows/build-core.yml index 69cc958e..05891346 100644 --- a/.github/workflows/build-core.yml +++ b/.github/workflows/build-core.yml @@ -44,79 +44,82 @@ jobs: github-repo: ${{ github.repository }} github-token: ${{ secrets.GITHUB_TOKEN }} - update-changelog: - runs-on: ubuntu-latest - outputs: - was_updated: ${{ steps.check-change.outputs.change_detected }} - check_commit: ${{ steps.check-changelog.outputs.check_commit }} - steps: - - name: Checkout code - uses: actions/checkout@v2 - with: - ref: ${{ github.head_ref }} - fetch-depth: 0 + jobs: + update-changelog: + runs-on: ubuntu-latest + outputs: + was_updated: ${{ steps.check-change.outputs.change_detected }} + check_commit: ${{ steps.check-changelog.outputs.check_commit }} + steps: + - name: Checkout code + uses: actions/checkout@v2 + with: + ref: ${{ github.head_ref }} + fetch-depth: 0 + + - name: Check for updated CHANGELOG.md using git + id: check-changelog + run: | + if git diff --name-only origin/${{ github.base_ref }} | grep -q "^CHANGELOG.md$"; then + echo "CHANGELOG.md has been updated." + echo "::set-output name=check_commit::true" + else + echo "ERROR: CHANGELOG.md has not been updated." + echo "::set-output name=check_commit::false" + fi + - name: Extract changelog info + if: steps.check-changelog.outputs.check_commit == 'false' + id: extract-changelog + run: | + PR_DESCRIPTION="${{ github.event.pull_request.body }}" + # Check if "changelog:" exists in PR description + if echo "$PR_DESCRIPTION" | grep -q "VERSION:" && echo "$PR_DESCRIPTION" | grep -q "CHANGELOG:"; then + # Extract text after "changelog:" + CHANGELOG_TEXT=$(echo $PR_DESCRIPTION | sed -n 's/.*CHANGELOG: \(.*\)/\1/p') + # Extract VERSION: from PR description + VERSION=$(echo "$PR_DESCRIPTION" | grep -oP 'VERSION:\s*\K\d+\.\d+\.\d+') + echo "Extracted changelog: $CHANGELOG_TEXT" + echo "::set-output name=changelog::$CHANGELOG_TEXT" + echo "::set-output name=version::$VERSION" + else + echo -e "No changelog and version information found in PR description please add them.\n Expected Format:\n VERSION:vX.XX.X\n CHANGELOG:This is changelog note.\n + To re-run the action, just make a push or commit after updating the PR description or updating the changelog via a manual file changing commit." + exit 1 + fi + - name: Check PR body against changelog + if: steps.check-changelog.outputs.check_commit == 'false' + run: | + ESCAPED_CHANGELOG="${{ steps.extract-changelog.outputs.changelog }}" + ESCAPED_CHANGELOG=$(echo "$ESCAPED_CHANGELOG" | sed "s/'/\\\\'/g") + VERSION="${{ steps.extract-changelog.outputs.version }}" + + if ! grep -Fq "$ESCAPED_CHANGELOG" CHANGELOG.md; then + # Check if version exists in CHANGELOG.md + if grep -q "^## \`$VERSION\`" CHANGELOG.md; then + # Append PR description to existing version + sed -i "/^## \`$VERSION\`/a - $ESCAPED_CHANGELOG (#${{ github.event.pull_request.number }})" CHANGELOG.md + else + # Append new version (with backticks) and PR description below the anchor line + ANCHOR_LINE=$(awk '/All notable changes to the Zlux App Manager will be documented in this file\./ {print NR}' CHANGELOG.md) + sed -i "$ANCHOR_LINE a\\ + \n## \`$VERSION\`\n- $ESCAPED_CHANGELOG (#${{ github.event.pull_request.number }})\n" CHANGELOG.md + fi + git config --local user.email "action@github.com" + git config --local user.name "GitHub Action" + git add CHANGELOG.md + git commit -m "Update changelog with PR #${{ github.event.pull_request.number }} description" + git push + fi - - name: Check for updated CHANGELOG.md using git - id: check-changelog - run: | - if git diff --name-only origin/${{ github.base_ref }} | grep -q "^CHANGELOG.md$"; then - echo "CHANGELOG.md has been updated." - echo "::set-output name=check_commit::true" - else - echo "ERROR: CHANGELOG.md has not been updated." - echo "::set-output name=check_commit::false" - fi - - name: Extract changelog info - if: steps.check-changelog.outputs.check_commit == 'false' - id: extract-changelog - run: | - PR_DESCRIPTION="${{ github.event.pull_request.body }}" - # Check if "changelog:" exists in PR description - if echo "$PR_DESCRIPTION" | grep -q "VERSION:" && echo "$PR_DESCRIPTION" | grep -q "CHANGELOG:"; then - # Extract text after "changelog:" - CHANGELOG_TEXT=$(echo $PR_DESCRIPTION | sed -n 's/.*CHANGELOG: \(.*\)/\1/p') - # Extract VERSION: from PR description - VERSION=$(echo "$PR_DESCRIPTION" | grep -oP 'VERSION:\s*\K\d+\.\d+\.\d+') - echo "Extracted changelog: $CHANGELOG_TEXT" - echo "::set-output name=changelog::$CHANGELOG_TEXT" - echo "::set-output name=version::$VERSION" - else - echo -e "No changelog and version information found in PR description please add them.\n Expected Format:\n VERSION:vX.XX.X\n CHANGELOG:This is changelog note.\n - To re-run the action, just pushed/commit the changes along with PR description, it will automatically triggered by sync." - exit 1 - fi - - name: Check PR body against changelog - if: steps.check-changelog.outputs.check_commit == 'false' - run: | - if ! grep -Fq "${{ steps.extract-changelog.outputs.changelog }}" CHANGELOG.md; then - # Check if version exists in CHANGELOG.md - if grep -q "## ${{ steps.extract-changelog.outputs.version }}" CHANGELOG.md; then - # Append PR description to existing version - sed -i "/## ${{ steps.extract-changelog.outputs.version }}/ a- ${{ steps.extract-changelog.outputs.changelog }} (#${{ github.event.pull_request.number }})" CHANGELOG.md + - name: check for changes + id: check-change + run: | + if git diff --name-only HEAD^ HEAD | grep 'changelog.md'; then + echo "No Changes detected, setting flag to false" + echo "::set-output name=change_detected::false" else - # Append new version and PR description - echo "$(awk '/All notable changes to the Zlux App Server package will be documented in this file\./ { - print - print "\n## " "${{ steps.extract-changelog.outputs.version }}" - print "- " "${{ steps.extract-changelog.outputs.changelog }}" " (#${{ github.event.pull_request.number }})" - next - } 1' CHANGELOG.md)" > CHANGELOG.md - fi - git config --local user.email "action@github.com" - git config --local user.name "GitHub Action" - git add CHANGELOG.md - git commit -m "Update changelog with PR #${{ github.event.pull_request.number }} description" - git push - fi - - name: check for changes - id: check-change - run: | - if git diff --name-only HEAD^ HEAD | grep 'changelog.md'; then - echo "No Changes detected, setting flag to false" - echo "::set-output name=change_detected::false" - else - echo "::set-output name=change_detected::true" - fi + echo "::set-output name=change_detected::true" + fi check_changelog: needs: update-changelog @@ -132,8 +135,9 @@ jobs: exit 1 else echo "changelog was updated successfully." - fi - + fi + + build: runs-on: ubuntu-latest needs: check-permission