Skip to content

PR Playground Preview #1012

PR Playground Preview

PR Playground Preview #1012

name: PR Playground Preview
# Use workflow_run for privileged operations
# Runs with write permissions and access to secrets
# Operates on artifacts from the unprivileged build workflow
on:
workflow_run:
workflows: ["Build Plugin Zip"]
types:
- completed
# Disable permissions for all available scopes by default.
# Any needed permissions should be configured at the job level.
permissions: {}
jobs:
# Leaves a comment on a pull request with a link to test the changes in a WordPress Playground instance.
playground-details:
name: Comment on a pull request with Playground details
runs-on: ubuntu-24.04
# Only run if the build workflow succeeded and was triggered by a pull_request
if: >
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success'
outputs:
artifact-url: ${{ steps.expose.outputs.artifact-url }}
artifact-name: ${{ steps.expose.outputs.artifact-name }}
permissions:
contents: write
pull-requests: write
steps:
- name: Extract PR metadata from artifact name
id: pr-metadata
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: ${{ github.event.workflow_run.id }},
});
const artifact = artifacts.data.artifacts.find(a =>
a.name.startsWith("ai-plugin-zip-pr")
);
if (!artifact) {
throw new Error('Could not find plugin artifact');
}
// Parse: ai-plugin-zip-pr123-abc123def...
const match = artifact.name.match(/^ai-plugin-zip-pr(\d+)-(.+)$/);
if (!match) {
throw new Error(`Could not parse artifact name: ${artifact.name}`);
}
const [, prNumber, commitSha] = match;
core.setOutput('pr-number', prNumber);
core.setOutput('commit-sha', commitSha);
core.setOutput('artifact-name', artifact.name);
- name: Expose built artifact
id: expose
uses: WordPress/action-wp-playground-pr-preview/.github/actions/expose-artifact-on-public-url@c8607529dac8d2bf9a1e8493865fc97cd1c3c87b # v2
with:
artifact-name: ${{ steps.pr-metadata.outputs.artifact-name }}
artifact-filename: ai.zip
pr-number: ${{ steps.pr-metadata.outputs.pr-number }}
commit-sha: ${{ steps.pr-metadata.outputs.commit-sha }}
artifact-source-run-id: ${{ github.event.workflow_run.id }}
artifacts-to-keep: '2'
- name: Generate Playground blueprint JSON
id: blueprint
run: |
node - <<'NODE' >> "$GITHUB_OUTPUT"
const url = process.env.ARTIFACT_URL;
if (!url) {
throw new Error('ARTIFACT_URL is required');
}
const blueprint = {
preferredVersions: {
wp: 'beta',
},
steps: [
{
step: 'installPlugin',
pluginZipFile: {
resource: 'url',
url,
},
},
],
};
console.log(`blueprint=${JSON.stringify(blueprint)}`);
NODE
env:
ARTIFACT_URL: ${{ steps.expose.outputs.artifact-url }}
- name: Post Playground preview button
uses: WordPress/action-wp-playground-pr-preview@c8607529dac8d2bf9a1e8493865fc97cd1c3c87b # v2
with:
mode: append-to-description
blueprint: ${{ steps.blueprint.outputs.blueprint }}
pr-number: ${{ steps.pr-metadata.outputs.pr-number }}
github-token: ${{ secrets.GITHUB_TOKEN }}