|
| 1 | +name: PR Playground Preview |
| 2 | + |
| 3 | +# Use workflow_run for privileged operations |
| 4 | +# Runs with write permissions and access to secrets |
| 5 | +# Operates on artifacts from the unprivileged build workflow |
| 6 | +on: |
| 7 | + workflow_run: |
| 8 | + workflows: ["Build Plugin Zip"] |
| 9 | + types: |
| 10 | + - completed |
| 11 | + |
| 12 | +# Disable permissions for all available scopes by default. |
| 13 | +# Any needed permissions should be configured at the job level. |
| 14 | +permissions: {} |
| 15 | + |
| 16 | +jobs: |
| 17 | + |
| 18 | + # Leaves a comment on a pull request with a link to test the changes in a WordPress Playground instance. |
| 19 | + playground-details: |
| 20 | + name: Comment on a pull request with Playground details |
| 21 | + runs-on: ubuntu-24.04 |
| 22 | + # Only run if the build workflow succeeded and was triggered by a pull_request |
| 23 | + if: > |
| 24 | + github.event.workflow_run.event == 'pull_request' && |
| 25 | + github.event.workflow_run.conclusion == 'success' |
| 26 | + outputs: |
| 27 | + artifact-url: ${{ steps.expose.outputs.artifact-url }} |
| 28 | + artifact-name: ${{ steps.expose.outputs.artifact-name }} |
| 29 | + permissions: |
| 30 | + contents: read |
| 31 | + pull-requests: write |
| 32 | + |
| 33 | + steps: |
| 34 | + - name: Extract PR metadata from artifact name |
| 35 | + id: pr-metadata |
| 36 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 |
| 37 | + with: |
| 38 | + script: | |
| 39 | + const artifacts = await github.rest.actions.listWorkflowRunArtifacts({ |
| 40 | + owner: context.repo.owner, |
| 41 | + repo: context.repo.repo, |
| 42 | + run_id: ${{ github.event.workflow_run.id }}, |
| 43 | + }); |
| 44 | +
|
| 45 | + const artifact = artifacts.data.artifacts.find(a => |
| 46 | + a.name.startsWith("ai-plugin-zip-pr") |
| 47 | + ); |
| 48 | +
|
| 49 | + if (!artifact) { |
| 50 | + throw new Error('Could not find plugin artifact'); |
| 51 | + } |
| 52 | +
|
| 53 | + // Parse: ai-plugin-zip-pr123-abc123def... |
| 54 | + const match = artifact.name.match(/^ai-plugin-zip-pr(\d+)-(.+)$/); |
| 55 | + if (!match) { |
| 56 | + throw new Error(`Could not parse artifact name: ${artifact.name}`); |
| 57 | + } |
| 58 | +
|
| 59 | + const [, prNumber, commitSha] = match; |
| 60 | +
|
| 61 | + core.setOutput('pr-number', prNumber); |
| 62 | + core.setOutput('commit-sha', commitSha); |
| 63 | + core.setOutput('artifact-name', artifact.name); |
| 64 | +
|
| 65 | + - name: Expose built artifact |
| 66 | + id: expose |
| 67 | + uses: WordPress/action-wp-playground-pr-preview/.github/actions/expose-artifact-on-public-url@c8607529dac8d2bf9a1e8493865fc97cd1c3c87b # v2 |
| 68 | + with: |
| 69 | + artifact-name: ${{ steps.pr-metadata.outputs.artifact-name }} |
| 70 | + artifact-filename: ai.zip |
| 71 | + pr-number: ${{ steps.pr-metadata.outputs.pr-number }} |
| 72 | + commit-sha: ${{ steps.pr-metadata.outputs.commit-sha }} |
| 73 | + artifact-source-run-id: ${{ github.event.workflow_run.id }} |
| 74 | + artifacts-to-keep: '2' |
| 75 | + |
| 76 | + - name: Generate Playground blueprint JSON |
| 77 | + id: blueprint |
| 78 | + run: | |
| 79 | + node - <<'NODE' >> "$GITHUB_OUTPUT" |
| 80 | + const url = process.env.ARTIFACT_URL; |
| 81 | + if (!url) { |
| 82 | + throw new Error('ARTIFACT_URL is required'); |
| 83 | + } |
| 84 | +
|
| 85 | + const blueprint = { |
| 86 | + steps: [ |
| 87 | + { |
| 88 | + step: 'installPlugin', |
| 89 | + pluginZipFile: { |
| 90 | + resource: 'url', |
| 91 | + url, |
| 92 | + }, |
| 93 | + }, |
| 94 | + ], |
| 95 | + }; |
| 96 | +
|
| 97 | + console.log(`blueprint=${JSON.stringify(blueprint)}`); |
| 98 | + NODE |
| 99 | + env: |
| 100 | + ARTIFACT_URL: ${{ steps.expose.outputs.artifact-url }} |
| 101 | + |
| 102 | + - name: Post Playground preview button |
| 103 | + uses: WordPress/action-wp-playground-pr-preview@c8607529dac8d2bf9a1e8493865fc97cd1c3c87b # v2 |
| 104 | + with: |
| 105 | + mode: append-to-description |
| 106 | + blueprint: ${{ steps.blueprint.outputs.blueprint }} |
| 107 | + pr-number: ${{ steps.pr-metadata.outputs.pr-number }} |
| 108 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments