Skip to content

Commit 0e9c94b

Browse files
jeffpauldkotteradamziel
authored
Merge pull request #144 from WordPress/try/playground-action
Try out the new `WordPress/action-wp-playground-pr-preview` GitHub Action Co-authored-by: dkotter <[email protected]> Co-authored-by: adamziel <[email protected]>
2 parents cf1c8f7 + 604d584 commit 0e9c94b

File tree

3 files changed

+120
-140
lines changed

3 files changed

+120
-140
lines changed

.github/workflows/build-plugin-zip.yml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
name: Build Plugin Zip
22

33
on:
4-
workflow_call:
4+
pull_request:
5+
types: [ 'opened', 'synchronize', 'reopened', 'edited' ]
6+
7+
# Cancels all previous workflow runs for pull requests that have not completed.
8+
concurrency:
9+
# The concurrency group contains the workflow name and the branch name for pull requests
10+
# or the commit hash for any other events.
11+
group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.head_ref || github.sha }}
12+
cancel-in-progress: true
513

614
# Disable permissions for all available scopes by default.
715
# Any needed permissions should be configured at the job level.
816
permissions: {}
917

1018
jobs:
1119
build:
12-
name: Build Release Artifact
1320
runs-on: 'ubuntu-24.04'
1421
permissions:
1522
contents: read
@@ -54,5 +61,6 @@ jobs:
5461
- name: Upload artifact
5562
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
5663
with:
57-
name: ${{ github.event.repository.name }}
58-
path: ${{ github.event.repository.name }}.zip
64+
name: ai-plugin-zip-pr${{ github.event.pull_request.number }}-${{ github.event.pull_request.head.sha }}
65+
path: ai.zip
66+
if-no-files-found: error
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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 }}

.github/workflows/pull-request-comments.yml

Lines changed: 0 additions & 136 deletions
This file was deleted.

0 commit comments

Comments
 (0)