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