-
Notifications
You must be signed in to change notification settings - Fork 19
401 lines (356 loc) · 17.5 KB
/
Copy pathbuild-plugin-with-ref.yml
File metadata and controls
401 lines (356 loc) · 17.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
name: Build Accessibility Checker Plugin with Ref Param
on:
workflow_dispatch:
inputs:
ref_param:
description: "Ref param string to set in EDAC_REF_PARAM (optional)"
required: false
type: string
pull_request:
types: [labeled]
release:
types: [created, published]
permissions:
contents: write
pull-requests: write
jobs:
build-plugin:
name: Build plugin zip
runs-on: ubuntu-latest
# Run on release, manual, or PR when a specific label is added
if: |
github.event_name == 'release' ||
github.event_name == 'workflow_dispatch' ||
(github.event_name == 'pull_request' && (contains(github.event.pull_request.labels.*.name, 'gha-build') || contains(github.event.pull_request.labels.*.name, 'gha-build-all')))
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Determine mode and ref param for this run
id: setref
run: |
MODE="${{ github.event_name }}"
echo "mode=$MODE" >> $GITHUB_OUTPUT
# Manual run: take input if provided; else empty
if [ "$MODE" = "workflow_dispatch" ]; then
REF_INPUT="${{ github.event.inputs.ref_param }}"
echo "ref_param=${REF_INPUT}" >> $GITHUB_OUTPUT
# For manual mode: if ref is provided, skip primary build
if [ -n "$REF_INPUT" ]; then
echo "skip_primary=true" >> $GITHUB_OUTPUT
else
echo "skip_primary=false" >> $GITHUB_OUTPUT
fi
# Release: use fixed ref 'woocommerce'
elif [ "$MODE" = "release" ]; then
echo "ref_param=woocommerce" >> $GITHUB_OUTPUT
echo "skip_primary=false" >> $GITHUB_OUTPUT
# PR labeled: check which label
else
# Check if gha-build-all label is present (build both)
if echo "${{ github.event.pull_request.labels.*.name }}" | grep -q "gha-build-all"; then
echo "ref_param=woocommerce" >> $GITHUB_OUTPUT
echo "skip_primary=false" >> $GITHUB_OUTPUT
else
# gha-build label (build primary only)
echo "ref_param=" >> $GITHUB_OUTPUT
echo "skip_primary=false" >> $GITHUB_OUTPUT
fi
fi
- name: Show selected mode/ref
run: |
echo "Triggered on: $GITHUB_EVENT_NAME"
echo "Mode: ${{ steps.setref.outputs.mode }}"
echo "Head ref: ${{ github.head_ref }}"
echo "PR number: ${{ github.event.pull_request.number }}"
echo "Release tag: ${{ github.event.release.tag_name }}"
echo "Ref param: ${{ steps.setref.outputs.ref_param }}"
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
cache-dependency-path: package-lock.json
- name: Cache node_modules
uses: actions/cache@v4
with:
path: node_modules
key: node-${{ runner.os }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
node-${{ runner.os }}-
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
tools: composer
coverage: none
- name: Install Composer dependencies
run: |
if [ -f composer.json ]; then composer install --no-dev --prefer-dist --no-progress --no-interaction; else echo "No composer.json"; fi
- name: Install npm dependencies
run: |
if [ -f package.json ]; then
if [ -d node_modules ]; then
echo "node_modules cache hit, skipping install"
else
npm ci --prefer-offline --no-audit
fi
else
echo "No package.json"
fi
- name: Extract plugin version and commit hash
id: version
run: |
VERSION=$(grep "Version:" accessibility-checker.php | head -1 | sed 's/.*Version:[[:space:]]*\([^ ]*\).*/\1/')
SHORT_SHA=$(git rev-parse --short HEAD)
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "short_sha=$SHORT_SHA" >> $GITHUB_OUTPUT
echo "Plugin version: $VERSION"
echo "Short commit hash: $SHORT_SHA"
- name: Verify EDAC_REF_PARAM is empty before first build
if: steps.setref.outputs.skip_primary != 'true'
run: |
grep -n "define( 'EDAC_REF_PARAM', '' )" accessibility-checker.php || { echo "EDAC_REF_PARAM should be empty for first build"; exit 1; }
- name: Build primary dist zip (empty ref)
if: steps.setref.outputs.skip_primary != 'true'
run: |
echo "Building primary zip with empty ref..."
npm run dist
ZIP_PATH=$(ls -1 dist/*.zip build/*.zip 2>/dev/null | head -n 1 || true)
if [ -z "$ZIP_PATH" ]; then
ZIP_PATH=$(ls -1 *.zip 2>/dev/null | head -n 1 || true)
fi
if [ -z "$ZIP_PATH" ]; then
echo "Error: Could not locate produced zip after npm run dist" >&2
exit 1
fi
mkdir -p builds
# Construct the new filename based on trigger mode
VERSION="${{ steps.version.outputs.version }}"
SHORT_SHA="${{ steps.version.outputs.short_sha }}"
if [ "${{ github.event_name }}" = "pull_request" ]; then
# PR: accessibility-checker-{version}-{prnumber}-{hash}.zip
PRIMARY_ZIP_NAME="accessibility-checker-${VERSION}-${{ github.event.pull_request.number }}-${SHORT_SHA}.zip"
elif [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
# Manual: accessibility-checker-{version}-{hash}.zip
PRIMARY_ZIP_NAME="accessibility-checker-${VERSION}-${SHORT_SHA}.zip"
elif [ "${{ github.event_name }}" = "release" ]; then
# Release: accessibility-checker-{version}.zip
PRIMARY_ZIP_NAME="accessibility-checker-${VERSION}.zip"
else
# Fallback
PRIMARY_ZIP_NAME="accessibility-checker-${VERSION}-${SHORT_SHA}.zip"
fi
mv "$ZIP_PATH" "builds/$PRIMARY_ZIP_NAME"
PRIMARY_ZIP_BASENAME="${PRIMARY_ZIP_NAME%.zip}"
unzip -q "builds/$PRIMARY_ZIP_NAME" -d "builds/$PRIMARY_ZIP_BASENAME"
PRIMARY_ZIP_FOLDER="builds/$PRIMARY_ZIP_BASENAME"
PRIMARY_ZIP_NAME_NO_EXT="${PRIMARY_ZIP_NAME%.zip}"
echo "PRIMARY_ZIP_PATH=builds/$PRIMARY_ZIP_NAME" >> $GITHUB_ENV
echo "PRIMARY_ZIP_NAME=$PRIMARY_ZIP_NAME" >> $GITHUB_ENV
echo "PRIMARY_ZIP_NAME_NO_EXT=$PRIMARY_ZIP_NAME_NO_EXT" >> $GITHUB_ENV
echo "PRIMARY_ZIP_FOLDER=$PRIMARY_ZIP_FOLDER" >> $GITHUB_ENV
echo "Produced primary zip: builds/$PRIMARY_ZIP_NAME"
echo "Produced primary folder: $PRIMARY_ZIP_FOLDER"
- name: Upload primary build artifact
if: steps.setref.outputs.skip_primary != 'true'
uses: actions/upload-artifact@v4
with:
name: ${{ env.PRIMARY_ZIP_NAME_NO_EXT }}
path: ${{ env.PRIMARY_ZIP_FOLDER }}
if-no-files-found: error
- name: Check if ref build is needed
id: check_ref
run: |
MODE="${{ steps.setref.outputs.mode }}"
REF_VALUE="${{ steps.setref.outputs.ref_param }}"
# For manual: build ref if ref is provided
# For release: always build ref (woocommerce)
# For PR: never build ref
if [ "$MODE" = "workflow_dispatch" ] && [ -n "$REF_VALUE" ]; then
echo "need_ref_build=true" >> $GITHUB_OUTPUT
echo "Ref build needed with value: $REF_VALUE"
elif [ "$MODE" = "release" ] && [ -n "$REF_VALUE" ]; then
echo "need_ref_build=true" >> $GITHUB_OUTPUT
echo "Ref build needed with value: $REF_VALUE"
else
echo "need_ref_build=false" >> $GITHUB_OUTPUT
echo "No ref build needed (mode=$MODE, ref='$REF_VALUE')"
fi
- name: Update EDAC_REF_PARAM for second build
if: steps.check_ref.outputs.need_ref_build == 'true'
run: |
chmod +x ./scripts/update-ref-param.sh
./scripts/update-ref-param.sh "${{ steps.setref.outputs.ref_param }}"
- name: Verify EDAC_REF_PARAM change
if: steps.check_ref.outputs.need_ref_build == 'true'
run: |
grep -n "EDAC_REF_PARAM" accessibility-checker.php || { echo "EDAC_REF_PARAM not found"; exit 1; }
echo "Updated EDAC_REF_PARAM contents:"
grep "EDAC_REF_PARAM" accessibility-checker.php
- name: Build ref dist zip (custom ref)
if: steps.check_ref.outputs.need_ref_build == 'true'
run: |
echo "Building ref zip with custom ref value..."
npm run dist
ZIP_PATH=$(ls -1 dist/*.zip build/*.zip 2>/dev/null | head -n 1 || true)
if [ -z "$ZIP_PATH" ]; then
ZIP_PATH=$(ls -1 *.zip 2>/dev/null | head -n 1 || true)
fi
if [ -z "$ZIP_PATH" ]; then
echo "Error: Could not locate produced zip after npm run dist" >&2
exit 1
fi
mkdir -p builds
VERSION="${{ steps.version.outputs.version }}"
SHORT_SHA="${{ steps.version.outputs.short_sha }}"
REF_VALUE="${{ steps.setref.outputs.ref_param }}"
# Ref build naming: accessibility-checker-{version}-ref-{refvalue}-{hash}.zip
REF_ZIP_NAME="accessibility-checker-${VERSION}-ref-${REF_VALUE}-${SHORT_SHA}.zip"
mv "$ZIP_PATH" "builds/$REF_ZIP_NAME"
REF_ZIP_BASENAME="${REF_ZIP_NAME%.zip}"
unzip -q "builds/$REF_ZIP_NAME" -d "builds/$REF_ZIP_BASENAME"
REF_ZIP_FOLDER="builds/$REF_ZIP_BASENAME"
REF_ZIP_NAME_NO_EXT="${REF_ZIP_NAME%.zip}"
echo "REF_ZIP_PATH=builds/$REF_ZIP_NAME" >> $GITHUB_ENV
echo "REF_ZIP_NAME=$REF_ZIP_NAME" >> $GITHUB_ENV
echo "REF_ZIP_NAME_NO_EXT=$REF_ZIP_NAME_NO_EXT" >> $GITHUB_ENV
echo "REF_ZIP_FOLDER=$REF_ZIP_FOLDER" >> $GITHUB_ENV
echo "Produced ref zip: builds/$REF_ZIP_NAME"
echo "Produced ref folder: $REF_ZIP_FOLDER"
- name: Upload ref build artifact
if: steps.check_ref.outputs.need_ref_build == 'true'
uses: actions/upload-artifact@v4
with:
name: ${{ env.REF_ZIP_NAME_NO_EXT }}
path: ${{ env.REF_ZIP_FOLDER }}
if-no-files-found: error
- name: Resolve public plugin URLs for Playground
id: playground-zips
if: github.event.repository.private == false
run: |
PRIMARY_PLUGIN_ZIP_URL=""
REF_PLUGIN_ZIP_URL=""
if [ -n "${PRIMARY_ZIP_NAME:-}" ]; then
if [ "${{ github.event_name }}" = "release" ]; then
PRIMARY_PLUGIN_ZIP_URL="https://github.com/${GITHUB_REPOSITORY}/releases/download/${{ github.event.release.tag_name }}/${PRIMARY_ZIP_NAME}"
else
PRIMARY_PLUGIN_ZIP_URL="https://nightly.link/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}/${PRIMARY_ZIP_NAME_NO_EXT}.zip"
fi
fi
if [ "${{ steps.check_ref.outputs.need_ref_build }}" = "true" ] && [ -n "${REF_ZIP_NAME:-}" ]; then
if [ "${{ github.event_name }}" = "release" ]; then
REF_PLUGIN_ZIP_URL="https://github.com/${GITHUB_REPOSITORY}/releases/download/${{ github.event.release.tag_name }}/${REF_ZIP_NAME}"
else
REF_PLUGIN_ZIP_URL="https://nightly.link/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}/${REF_ZIP_NAME_NO_EXT}.zip"
fi
fi
echo "primary_plugin_zip_url=$PRIMARY_PLUGIN_ZIP_URL" >> "$GITHUB_OUTPUT"
echo "ref_plugin_zip_url=$REF_PLUGIN_ZIP_URL" >> "$GITHUB_OUTPUT"
- name: Build WordPress Playground links
if: github.event.repository.private == false
run: |
export PRIMARY_PLUGIN_ZIP_URL='${{ steps.playground-zips.outputs.primary_plugin_zip_url }}'
export REF_PLUGIN_ZIP_URL='${{ steps.playground-zips.outputs.ref_plugin_zip_url }}'
build_playground_url() {
local plugin_url="$1"
python3 -c 'import base64,json,sys,urllib.parse; plugin_url=sys.argv[1]; blueprint={"landingPage":"/wp-admin/plugins.php","steps":[{"step":"login"},{"step":"installPlugin","pluginZipFile":{"resource":"url","url":plugin_url}},{"step":"activatePlugin","pluginPath":"accessibility-checker/accessibility-checker.php"}]}; blueprint_json=json.dumps(blueprint,separators=(",",":")); data_uri="data:application/json;base64,"+base64.b64encode(blueprint_json.encode("utf-8")).decode("ascii"); encoded_blueprint_url=urllib.parse.quote(data_uri,safe=""); print(f"https://playground.wordpress.net/?blueprint-url={encoded_blueprint_url}")' "$plugin_url"
}
PRIMARY_PLAYGROUND_URL=""
REF_PLAYGROUND_URL=""
if [ -n "$PRIMARY_PLUGIN_ZIP_URL" ]; then
PRIMARY_PLAYGROUND_URL="$(build_playground_url "$PRIMARY_PLUGIN_ZIP_URL")"
fi
if [ -n "$REF_PLUGIN_ZIP_URL" ]; then
REF_PLAYGROUND_URL="$(build_playground_url "$REF_PLUGIN_ZIP_URL")"
fi
echo "PRIMARY_PLAYGROUND_URL=$PRIMARY_PLAYGROUND_URL" >> "$GITHUB_ENV"
echo "REF_PLAYGROUND_URL=$REF_PLAYGROUND_URL" >> "$GITHUB_ENV"
- name: Upload to release (both zips)
if: github.event_name == 'release'
uses: softprops/action-gh-release@v2
with:
files: |
${{ env.PRIMARY_ZIP_PATH }}
${{ steps.check_ref.outputs.need_ref_build == 'true' && env.REF_ZIP_PATH || '' }}
fail_on_unmatched_files: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Comment on PR with primary build details
if: github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
script: |
const prNumber = context.payload.pull_request.number;
const runId = context.runId;
const runUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${runId}`;
const primaryName = process.env.PRIMARY_ZIP_NAME_NO_EXT;
// Get the artifact ID for the download URL
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: runId,
});
const artifact = artifacts.data.artifacts.find(a => a.name === primaryName);
const downloadUrl = artifact
? `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${runId}/artifacts/${artifact.id}`
: runUrl;
const primaryPlaygroundUrl = process.env.PRIMARY_PLAYGROUND_URL;
const primaryPlaygroundLine = primaryPlaygroundUrl
? `\n- **Playground (primary)**: [Open with plugin preinstalled](${primaryPlaygroundUrl})`
: `\n- **Playground (primary)**: Not available for this run (repository may be private or plugin ZIP URL is not publicly accessible).`;
const refPlaygroundUrl = process.env.REF_PLAYGROUND_URL;
const refPlaygroundLine = refPlaygroundUrl
? `\n- **Playground (ref)**: [Open with plugin preinstalled](${refPlaygroundUrl})`
: '';
const body = `✅ Accessibility Checker build (primary only)\n\n- **Artifact**: [Download ${primaryName}.zip](${downloadUrl})\n- **Workflow run**: [View logs](${runUrl})${primaryPlaygroundLine}${refPlaygroundLine}`;
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body,
});
- name: Remove triggering label from PR
if: github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
script: |
const prNumber = context.payload.pull_request.number;
const labels = context.payload.pull_request.labels.map(l => l.name);
const labelToRemove = labels.includes('gha-build-all') ? 'gha-build-all' : 'gha-build';
try {
await github.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
name: labelToRemove,
});
console.log(`Removed label '${labelToRemove}' from PR #${prNumber}`);
} catch (e) {
console.log(`Could not remove label '${labelToRemove}' from PR #${prNumber}: ${e.message}`);
}
- name: Summary
run: |
echo "=== Build Summary ==="
echo "Mode: ${{ steps.setref.outputs.mode }}"
echo "Primary zip (empty ref): ${{ env.PRIMARY_ZIP_PATH }}"
if [ -n "${PRIMARY_PLAYGROUND_URL:-}" ]; then
echo "Playground (primary): ${PRIMARY_PLAYGROUND_URL}"
else
echo "Playground (primary): skipped (repo is private or URL unavailable)"
fi
if [ "${{ steps.check_ref.outputs.need_ref_build }}" = "true" ]; then
echo "Ref zip (ref=${{ steps.setref.outputs.ref_param }}): ${{ env.REF_ZIP_PATH }}"
if [ -n "${REF_PLAYGROUND_URL:-}" ]; then
echo "Playground (ref): ${REF_PLAYGROUND_URL}"
else
echo "Playground (ref): skipped (repo is private or URL unavailable)"
fi
else
echo "Ref zip: Not built"
echo "Playground (ref): Not built"
fi
if [ "${{ github.event_name }}" = "release" ]; then
echo "Uploaded to release: ${{ github.event.release.html_url }}"
fi