Implement directory setup for build comparison and enhance diff repor… #58
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: build-site | |
on: | |
push: | |
branches: | |
tags: | |
paths-ignore: | |
- 'publisher/**' | |
- 'updater/**' | |
- '.github/workflows/ci-build-publisher.yml' | |
- '.github/workflows/ci-build-updater.yml' | |
- '.github/workflows/ci-frontend-check.yml' | |
- '.github/workflows/ci-frontend-lint.yml' | |
- '.github/dependabot.yml' | |
- '.github/FUNDING.yml' | |
- 'hugo/content/**' # content likely won't break the build | |
pull_request: | |
paths-ignore: | |
- 'publisher/**' | |
- 'updater/**' | |
- '.github/workflows/ci-build-publisher.yml' | |
- '.github/workflows/ci-build-updater.yml' | |
- '.github/workflows/ci-frontend-check.yml' | |
- '.github/workflows/ci-frontend-lint.yml' | |
- '.github/dependabot.yml' | |
- '.github/FUNDING.yml' | |
- 'hugo/content/**' # content likely won't break the build | |
jobs: | |
build: | |
name: build-and-compare | |
runs-on: ubuntu-latest | |
steps: | |
- 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: | | |
if [[ "${{ github.event_name }}" == "pull_request" ]]; then | |
echo "compare_sha=$(git merge-base ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }})" >> $GITHUB_OUTPUT | |
else | |
echo "compare_sha=$(git rev-parse HEAD^1)" >> $GITHUB_OUTPUT | |
fi | |
- name: Build previous version | |
run: | | |
git checkout ${{ steps.get-comparison-commit.outputs.compare_sha }} | |
docker build -t radio-t/site:previous . | |
# Create directories for previous build | |
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 }}/${{ 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: | | |
git checkout ${{ github.event.pull_request.head.sha || github.sha }} | |
docker build -t radio-t/site:current . | |
# Create directories for current build | |
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 }}/${{ 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: | | |
# 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:" > 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#$new_dir/}" | |
elif ! cmp -s "$1" "$prev_file"; then | |
echo "Modified: ${1#$new_dir/}" | |
fi | |
' sh {} \; >> summary.txt | |
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#$old_dir/}" | |
fi | |
' sh {} \; >> summary.txt | |
# Add size comparison | |
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 changes.diff as artifact | |
uses: actions/upload-artifact@v4 | |
with: | |
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<<EOF" >> $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 |