From e6ceb01c813c96e83d92e25438a2f66ae7c6d80f Mon Sep 17 00:00:00 2001 From: Dmitry Verkhoturov Date: Fri, 1 Nov 2024 12:33:20 +0100 Subject: [PATCH] Implement directory setup for build comparison and enhance diff reporting in GitHub Actions --- .github/workflows/ci-build-site.yml | 123 ++++++++++++++++++++-------- 1 file changed, 89 insertions(+), 34 deletions(-) diff --git a/.github/workflows/ci-build-site.yml b/.github/workflows/ci-build-site.yml index f91e6584..893442f3 100644 --- a/.github/workflows/ci-build-site.yml +++ b/.github/workflows/ci-build-site.yml @@ -30,11 +30,22 @@ jobs: name: build-and-compare runs-on: ubuntu-latest steps: - - name: check out code + - name: Check out code uses: actions/checkout@v4 with: fetch-depth: 0 # Needed to fetch all history for comparison + - name: Set directory names + id: set-dirs + run: | + if [[ "${{ github.event_name }}" == "pull_request" ]]; then + echo "old_dir=master" >> $GITHUB_OUTPUT + echo "new_dir=pr-${{ github.event.pull_request.number }}" >> $GITHUB_OUTPUT + else + echo "old_dir=old-master" >> $GITHUB_OUTPUT + echo "new_dir=new-master" >> $GITHUB_OUTPUT + fi + - name: Get comparison commit id: get-comparison-commit run: | @@ -50,15 +61,19 @@ jobs: docker build -t radio-t/site:previous . # Create directories for previous build - mkdir -p hugo-prev/hugo - cp -r hugo/* hugo-prev/hugo/ - mkdir -p hugo-prev/public + mkdir -p ${{ steps.set-dirs.outputs.old_dir }}/hugo + cp -r hugo/* ${{ steps.set-dirs.outputs.old_dir }}/hugo/ + mkdir -p ${{ steps.set-dirs.outputs.old_dir }}/public # Run the container with proper volume mounts docker run --rm \ - -v ${{ github.workspace }}/hugo-prev/hugo:/srv/hugo \ - -v ${{ github.workspace }}/hugo-prev/public:/srv/hugo/public \ + -v ${{ github.workspace }}/${{ steps.set-dirs.outputs.old_dir }}/hugo:/srv/hugo \ + -v ${{ github.workspace }}/${{ steps.set-dirs.outputs.old_dir }}/public:/srv/hugo/public \ radio-t/site:previous + + # Move files up one level for cleaner diff + mv ${{ steps.set-dirs.outputs.old_dir }}/public/* ${{ steps.set-dirs.outputs.old_dir }}/ + rm -rf ${{ steps.set-dirs.outputs.old_dir }}/public - name: Build current version run: | @@ -66,51 +81,91 @@ jobs: docker build -t radio-t/site:current . # Create directories for current build - mkdir -p hugo-current/hugo - cp -r hugo/* hugo-current/hugo/ - mkdir -p hugo-current/public + mkdir -p ${{ steps.set-dirs.outputs.new_dir }}/hugo + cp -r hugo/* ${{ steps.set-dirs.outputs.new_dir }}/hugo/ + mkdir -p ${{ steps.set-dirs.outputs.new_dir }}/public # Run the container with proper volume mounts docker run --rm \ - -v ${{ github.workspace }}/hugo-current/hugo:/srv/hugo \ - -v ${{ github.workspace }}/hugo-current/public:/srv/hugo/public \ + -v ${{ github.workspace }}/${{ steps.set-dirs.outputs.new_dir }}/hugo:/srv/hugo \ + -v ${{ github.workspace }}/${{ steps.set-dirs.outputs.new_dir }}/public:/srv/hugo/public \ radio-t/site:current + + # Move files up one level for cleaner diff + mv ${{ steps.set-dirs.outputs.new_dir }}/public/* ${{ steps.set-dirs.outputs.new_dir }}/ + rm -rf ${{ steps.set-dirs.outputs.new_dir }}/public - name: Generate diff run: | - # Create a directory for the diff - mkdir -p site-diff - - # Generate diff for HTML files - diff -r -N -u hugo-prev/public hugo-current/public > site-diff/changes.diff || true + # Generate diff for files + diff -r -N -u ${{ steps.set-dirs.outputs.old_dir }} ${{ steps.set-dirs.outputs.new_dir }} > changes.diff || true # Generate a summary of changed files - echo "Changed files:" > site-diff/summary.txt - find hugo-current/public -type f -exec sh -c ' - prev_file="hugo-prev/public/${1#hugo-current/public/}" + echo "Changed files:" > summary.txt + find ${{ steps.set-dirs.outputs.new_dir }} -type f -exec sh -c ' + old_dir="${{ steps.set-dirs.outputs.old_dir }}" + new_dir="${{ steps.set-dirs.outputs.new_dir }}" + prev_file="$old_dir/${1#$new_dir/}" if [ ! -f "$prev_file" ]; then - echo "New: ${1#hugo-current/public/}" + echo "New: ${1#$new_dir/}" elif ! cmp -s "$1" "$prev_file"; then - echo "Modified: ${1#hugo-current/public/}" + echo "Modified: ${1#$new_dir/}" fi - ' sh {} \; >> site-diff/summary.txt + ' sh {} \; >> summary.txt - find hugo-prev/public -type f -exec sh -c ' - current_file="hugo-current/public/${1#hugo-prev/public/}" + find ${{ steps.set-dirs.outputs.old_dir }} -type f -exec sh -c ' + old_dir="${{ steps.set-dirs.outputs.old_dir }}" + new_dir="${{ steps.set-dirs.outputs.new_dir }}" + current_file="$new_dir/${1#$old_dir/}" if [ ! -f "$current_file" ]; then - echo "Deleted: ${1#hugo-prev/public/}" + echo "Deleted: ${1#$old_dir/}" fi - ' sh {} \; >> site-diff/summary.txt + ' sh {} \; >> summary.txt # Add size comparison - echo -e "\nSize comparison:" >> site-diff/summary.txt - echo "Previous build:" >> site-diff/summary.txt - du -sh hugo-prev/public >> site-diff/summary.txt - echo "Current build:" >> site-diff/summary.txt - du -sh hugo-current/public >> site-diff/summary.txt + echo -e "\nSize comparison:" >> summary.txt + echo "${{ steps.set-dirs.outputs.old_dir }}:" >> summary.txt + du -sh ${{ steps.set-dirs.outputs.old_dir }} >> summary.txt + echo "${{ steps.set-dirs.outputs.new_dir }}:" >> summary.txt + du -sh ${{ steps.set-dirs.outputs.new_dir }} >> summary.txt - - name: Upload diff artifacts + - name: Upload changes.diff as artifact uses: actions/upload-artifact@v4 with: - name: site-diff - path: site-diff/ \ No newline at end of file + name: changes.diff + path: changes.diff + + - name: Find Comment + if: github.event_name == 'pull_request' + uses: peter-evans/find-comment@v3 + id: find-comment + with: + issue-number: ${{ github.event.pull_request.number }} + comment-author: 'github-actions[bot]' + body-includes: '### Site Build Comparison' + + - name: Generate comment body + id: get-comment-body + run: | + # Read the summary and escape it for multi-line output + SUMMARY=$(cat summary.txt) + echo "body<> $GITHUB_OUTPUT + echo "### Site Build Comparison" >> $GITHUB_OUTPUT + echo "" >> $GITHUB_OUTPUT + echo "**Build completed at:** $(date -u '+%Y-%m-%d %H:%M:%S UTC')" >> $GITHUB_OUTPUT + echo "" >> $GITHUB_OUTPUT + echo "\`\`\`" >> $GITHUB_OUTPUT + echo "$SUMMARY" >> $GITHUB_OUTPUT + echo "\`\`\`" >> $GITHUB_OUTPUT + echo "" >> $GITHUB_OUTPUT + echo "[View full changes diff](${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID})" >> $GITHUB_OUTPUT + echo "EOF" >> $GITHUB_OUTPUT + + - name: Create or update comment + if: github.event_name == 'pull_request' + uses: peter-evans/create-or-update-comment@v4 + with: + comment-id: ${{ steps.find-comment.outputs.comment-id }} + issue-number: ${{ github.event.pull_request.number }} + body: ${{ steps.get-comment-body.outputs.body }} + edit-mode: replace \ No newline at end of file