Commit Snapshots #1131
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: Commit Snapshots | |
| on: | |
| workflow_run: | |
| workflows: ['Update Snapshots'] | |
| types: | |
| - completed | |
| jobs: | |
| commit: | |
| name: Commit Snapshots | |
| if: github.event.workflow_run.conclusion == 'success' | |
| runs-on: ubuntu-24.04 | |
| permissions: | |
| contents: write | |
| steps: | |
| - name: Checkout repo | |
| uses: actions/checkout@v4 | |
| with: | |
| ref: ${{ github.event.workflow_run.head_branch }} | |
| repository: ${{ github.event.workflow_run.head_repository.full_name }} | |
| - name: Download Artifacts | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const fs = require('fs'); | |
| const { execSync } = require('child_process'); | |
| const artifacts = await github.paginate(github.rest.actions.listWorkflowRunArtifacts, { | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| run_id: context.payload.workflow_run.id, | |
| per_page: 100 | |
| }); | |
| console.log(`Found ${artifacts.length} artifacts in workflow run`); | |
| await Promise.all( | |
| artifacts.map(async (artifact) => { | |
| const download = await github.rest.actions.downloadArtifact({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| artifact_id: artifact.id, | |
| archive_format: 'zip' | |
| }); | |
| const tmpDir = `/tmp/${artifact.name.replace(/[^a-zA-Z0-9-_]/g, '')}`; | |
| execSync(`mkdir -p ${tmpDir}`); | |
| fs.writeFileSync(`${tmpDir}.zip`, Buffer.from(download.data)); | |
| console.log(`Checking artifact: ${artifact.name}`) | |
| execSync(`unzip -o ${tmpDir}.zip -d ${tmpDir}`); | |
| if (artifact.name === 'metadata') { | |
| execSync(`rsync -zavpmr --no-links --include='pull_number.txt' --exclude='*' ${tmpDir}/ ${{ github.workspace }}`); | |
| } else { | |
| const snapshotsDir = artifact.name.includes('v2') ? 'tests/functional/v2/snapshots' : 'tests/functional/snapshots'; | |
| execSync(`rsync -zavpmr --no-links --include='*.png' --include='*/' --exclude='*' ${tmpDir}/ ${{ github.workspace }}/${snapshotsDir}`); | |
| } | |
| }) | |
| ); | |
| - name: List Snapshots | |
| run: | | |
| git status --porcelain | |
| - name: Detect Snapshot Changes | |
| id: snapshot_changes | |
| run: | | |
| SNAPSHOT_STATUS="$(git status --porcelain tests/functional/snapshots tests/functional/v2/snapshots || true)" | |
| if [ -z "$SNAPSHOT_STATUS" ] | |
| then | |
| echo "has_changes=false" >> "$GITHUB_OUTPUT" | |
| echo "::error::Update Snapshots succeeded, but no snapshot file changes were present to commit." | |
| exit 1 | |
| fi | |
| echo "has_changes=true" >> "$GITHUB_OUTPUT" | |
| echo "$SNAPSHOT_STATUS" | |
| - name: Reset Label | |
| if: github.event.workflow_run.event == 'pull_request' | |
| uses: actions/github-script@v7 | |
| with: | |
| # The workflow_run that comes along with the event drops the PR number for some reason | |
| # so we have to pass the PR number from the original workflow as an artifact | |
| script: | | |
| const fs = require('fs'); | |
| let pullNumber = null; | |
| if (fs.existsSync('./pull_number.txt')) { | |
| pullNumber = Number(fs.readFileSync('./pull_number.txt', 'utf8')).toString(); | |
| } else if (context.payload.workflow_run.pull_requests?.[0]?.number) { | |
| pullNumber = context.payload.workflow_run.pull_requests[0].number.toString(); | |
| console.log('pull_number.txt not found, using workflow_run.pull_requests[0].number'); | |
| } else { | |
| console.log('No PR number available; skipping snapshots label reset'); | |
| return; | |
| } | |
| await github.rest.issues.removeLabel({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: pullNumber, | |
| name: 'snapshots' | |
| }).catch(() => { | |
| console.log('snapshots label has already been removed'); | |
| }); | |
| - name: Commit Updated Snapshots | |
| if: steps.snapshot_changes.outputs.has_changes == 'true' | |
| uses: stefanzweifel/git-auto-commit-action@v4 | |
| with: | |
| commit_message: 'chore(snapshots): ${{ github.event.workflow.name }} [skip ci]' | |
| commit_options: '--no-verify --quiet' | |
| file_pattern: tests/functional/snapshots tests/functional/v2/snapshots | |
| push_options: '-v' |