Deploy PR Preview (Trusted) #355
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: Deploy PR Preview (Trusted) | |
| # | |
| # Triggers after 'PR Build (Preview Artifact)' completes successfully. | |
| # | |
| on: | |
| workflow_run: | |
| workflows: ["PR Build (Preview Artifact)"] | |
| types: [completed] | |
| permissions: | |
| actions: read | |
| contents: write | |
| pages: write | |
| id-token: write | |
| pull-requests: write | |
| concurrency: | |
| group: pr-deploy-${{ github.event.workflow_run.id }} | |
| cancel-in-progress: true | |
| jobs: | |
| deploy: | |
| if: > | |
| github.event.workflow_run.conclusion == 'success' && | |
| github.event.workflow_run.event == 'pull_request' | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: List artifacts | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| run: gh api repos/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }}/artifacts | |
| - name: Get PR number from artifact name | |
| id: pr | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| PR_NUMBER=$(gh api repos/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }}/artifacts \ | |
| --jq '.artifacts[].name | select(startswith("pr-preview-site-")) | sub("^pr-preview-site-"; "")' | head -n1) | |
| if [ -z "$PR_NUMBER" ]; then | |
| echo "Failed to determine PR number from artifacts" >&2 | |
| exit 1 | |
| fi | |
| echo "pr_number=$PR_NUMBER" >> "$GITHUB_OUTPUT" | |
| echo "Deploying preview for PR #$PR_NUMBER" | |
| - name: Download artifact | |
| uses: dawidd6/action-download-artifact@v3 | |
| with: | |
| run_id: ${{ github.event.workflow_run.id }} | |
| name: pr-preview-site-${{ steps.pr.outputs.pr_number }} | |
| path: ./site | |
| - name: List downloaded files (debug) | |
| run: find ./site -maxdepth 4 -type f | head -100 | |
| - name: Deploy PR Preview | |
| uses: JamesIves/github-pages-deploy-action@v4 | |
| with: | |
| branch: gh-pages | |
| folder: ./site | |
| target-folder: pr-preview/pr-${{ steps.pr.outputs.pr_number }} | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Get timestamp (UTC) | |
| id: ts | |
| run: echo "utc=$(date -u '+%Y-%m-%d %H:%M UTC')" >> "$GITHUB_OUTPUT" | |
| - name: Post preview link as sticky PR comment | |
| uses: marocchino/sticky-pull-request-comment@v2 | |
| with: | |
| number: ${{ steps.pr.outputs.pr_number }} | |
| header: pr-preview | |
| message: | | |
| **PR Preview** | |
| :---: | |
| :rocket: View PR Preview at https://wpaccessibility.org/pr-preview/pr-${{ steps.pr.outputs.pr_number }}. | |
| If you see a 404 error, please wait a few minutes and refresh the page. | |
| ${{ steps.ts.outputs.utc }} |