PR Images Preview #189
This file contains hidden or 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: PR Images Preview | |
| on: | |
| workflow_run: | |
| workflows: [ "Generate graphics" ] | |
| types: | |
| - completed | |
| branches-ignore: | |
| - 'main' | |
| concurrency: | |
| group: "workflow = ${{ github.workflow }}, ref = ${{ github.event.ref }}, pr = ${{ github.event.pull_request.id }}" | |
| cancel-in-progress: ${{ github.event_name == 'pull_request' }} | |
| jobs: | |
| preview: | |
| runs-on: ubuntu-latest | |
| if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' | |
| steps: | |
| - uses: actions/checkout@v6 | |
| - name: Set up temporary branch | |
| id: branch | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| git config user.name github-actions | |
| git config user.email [email protected] | |
| git fetch origin | |
| git branch --set-upstream-to=origin/pr-images pr-images || true | |
| git checkout pr-images | |
| git reset --hard origin/main | |
| - name: Check for changes from the reset | |
| id: reset_needed | |
| run: | | |
| # Decide if any images has changed on this branch | |
| if [ -n "$(git status --porcelain images/)" ]; then | |
| echo "Changes detected from reset." | |
| echo "reset_needed=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "No changes from reset." | |
| echo "reset_needed=false" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Push to temporary branch after reset | |
| if: steps.reset_needed.outputs.reset_needed == 'true' | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| git add images/ | |
| git commit -m "Reset while processing #${{ steps.pr.outputs.id }}" || echo "No changes to commit" | |
| git push origin -u pr-images-reset --force | |
| - name: Tidy existing files | |
| id: tidy | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| git rm -f images/tuned/*svg || echo "No files to remove" | |
| - name: Download PR Artifact | |
| uses: actions/download-artifact@v7 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| run-id: ${{ github.event.workflow_run.id }} | |
| name: images | |
| path: images/tuned | |
| - name: Check for changes relative to main | |
| id: changed | |
| run: | | |
| # Refresh index so git diff-index doesn't return true just because of timestamp changes | |
| git add images/tuned/*svg | |
| git update-index --refresh | |
| # Decide if any images has changed relative to origin/main (not just the head of this branch) | |
| if ! git diff-index --quiet origin/main -- images/tuned/*svg; then | |
| echo "Changes detected." | |
| echo "files_changed=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "No changes." | |
| echo "files_changed=false" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Check for changes on this branch | |
| id: branch_changed | |
| run: | | |
| # Decide if any images has changed on this branch | |
| if [ -n "$(git status --porcelain images/tuned/*svg)" ]; then | |
| echo "Changes detected." | |
| echo "branch_changed=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "No changes." | |
| echo "branch_changed=false" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Download PR Artifact | |
| uses: actions/download-artifact@v7 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| run-id: ${{ github.event.workflow_run.id }} | |
| name: pr_number | |
| - name: Store PR id as variable | |
| id: pr | |
| run: | | |
| echo "id=$(<pr_number)" >> $GITHUB_OUTPUT | |
| rm pr_number | |
| - name: Push to temporary branch | |
| if: steps.branch_changed.outputs.branch_changed == 'true' | |
| id: push | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| # Stage all SVG files | |
| git add images/tuned/*svg | |
| DIFF=$(git diff --cached --name-status) | |
| # Changed SVGs | |
| CHANGED=$(echo "$DIFF" \ | |
| | grep -E '^M\s+images/.*\.svg' \ | |
| | awk '{print $2}') | |
| # Added SVGs | |
| ADDED=$(echo "$DIFF" \ | |
| | grep -E '^A\s+images/tuned/.*\.svg' \ | |
| | awk '{print $2}') | |
| # Removed SVGs | |
| REMOVED=$(echo "$DIFF" \ | |
| | grep -E '^D\s+images/tuned/.*\.svg' \ | |
| | awk '{print $2}') | |
| # Renamed SVGs: old -> removed, new -> added | |
| RENAMED_OLD=$(echo "$DIFF" \ | |
| | grep -E '^R[0-9]*\s+images/tuned/.*\.svg\s+images/tuned/.*\.svg' \ | |
| | awk '{print $2}') | |
| RENAMED_NEW=$(echo "$DIFF" \ | |
| | grep -E '^R[0-9]*\s+images/tuned/.*\.svg\s+images/tuned/.*\.svg' \ | |
| | awk '{print $3}') | |
| # Merge lists and convert to comma-separated | |
| ADDED=$(printf "%s\n%s" "$ADDED" "$RENAMED_NEW" | sed '/^$/d' | paste -sd "," -) | |
| REMOVED=$(printf "%s\n%s" "$REMOVED" "$RENAMED_OLD" | sed '/^$/d' | paste -sd "," -) | |
| CHANGED=$(printf "%s" "$CHANGED" | sed '/^$/d' | paste -sd "," -) | |
| echo Files changed: $CHANGED | |
| echo Files added: $ADDED | |
| echo Files removed: $REMOVED | |
| echo "svgs_changed=$CHANGED" >> $GITHUB_OUTPUT | |
| echo "svgs_added=$ADDED" >> $GITHUB_OUTPUT | |
| echo "svgs_removed=$REMOVED" >> $GITHUB_OUTPUT | |
| git commit -m "Images for #${{ steps.pr.outputs.id }}" || echo "No changes to commit" | |
| git push origin -u pr-images --force | |
| - name: Capture most recent hash on branch | |
| if: steps.changed.outputs.files_changed == 'true' | |
| id: hash | |
| run: | | |
| echo "hash=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT | |
| - name: Prepare markdown for changed SVGs | |
| if: steps.push.outputs.svgs_changed != '' | |
| id: svgs_changed_md | |
| run: | | |
| HASH=${{ steps.hash.outputs.hash }} | |
| FILES="${{ steps.push.outputs.svgs_changed }}" | |
| BAR_FILES=$(echo "$FILES" | tr ',' '\n' | grep -v "memory" | grep -v "main" | head -n2) | |
| CUBE_FILES=$(echo "$FILES" | tr ',' '\n' | grep "memory" | grep -v "main" | head -n2) | |
| SUBSET_BAR_FILES=$(echo "$FILES" | tr ',' '\n' | grep -v "memory" | grep "main" | head -n2) | |
| SUBSET_CUBE_FILES=$(echo "$FILES" | tr ',' '\n' | grep "memory" | grep "main" | head -n2) | |
| MD="" | |
| for f in $SUBSET_BAR_FILES; do | |
| MD="$MD \n-\n" | |
| done | |
| for f in $SUBSET_CUBE_FILES; do | |
| MD="$MD \n-\n" | |
| done | |
| for f in $BAR_FILES; do | |
| MD="$MD \n-\n" | |
| done | |
| for f in $CUBE_FILES; do | |
| MD="$MD \n-\n" | |
| done | |
| { | |
| echo "svgs_changed_md<<EOF" | |
| echo -e "$MD" | |
| echo "EOF" | |
| } >> $GITHUB_OUTPUT | |
| - name: Prepare markdown for added SVGs | |
| if: steps.push.outputs.svgs_added != '' | |
| id: svgs_added_md | |
| run: | | |
| HASH=${{ steps.hash.outputs.hash }} | |
| FILES="${{ steps.push.outputs.svgs_added }}" | |
| FILES=$(echo "$FILES" | tr ',' '\n' | head -n4) | |
| MD="" | |
| for f in $FILES; do | |
| MD="$MD \n-\n" | |
| done | |
| { | |
| echo "svgs_added_md<<EOF" | |
| echo -e "$MD" | |
| echo "EOF" | |
| } >> $GITHUB_OUTPUT | |
| - name: Prepare markdown for removed SVGs | |
| if: steps.push.outputs.svgs_removed != '' | |
| id: svgs_removed_md | |
| run: | | |
| FILES="${{ steps.push.outputs.svgs_removed }}" | |
| MD="#### Some files will no longer be generated:" | |
| for f in $(echo "$FILES" | tr ',' '\n'); do | |
| MD="$MD \n- $f" | |
| done | |
| { | |
| echo "svgs_removed_md<<EOF" | |
| echo -e "$MD" | |
| echo "EOF" | |
| } >> $GITHUB_OUTPUT | |
| - name: Update PR status comment with link | |
| if: steps.changed.outputs.files_changed == 'true' | |
| uses: actions-cool/maintain-one-comment@v3 | |
| with: | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| body: | | |
| 🚀 This is what some of the generated images would look like with this change. You can browse the full set on [the preview branch](../blob/${{ steps.hash.outputs.hash }}/images/). | |
| ${{ steps.svgs_changed_md.outputs.svgs_changed_md }} | |
| ${{ steps.svgs_added_md.outputs.svgs_added_md }} | |
| ${{ steps.svgs_removed_md.outputs.svgs_removed_md }} | |
| number: ${{ steps.pr.outputs.id }} | |
| - name: Update PR status comment with absence of link | |
| if: steps.changed.outputs.files_changed == 'false' | |
| uses: actions-cool/maintain-one-comment@v3 | |
| with: | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| body: This PR would not change any of the generated images. | |
| number: ${{ steps.pr.outputs.id }} |