diff --git a/.github/actionlint.yaml b/.github/actionlint.yaml new file mode 100644 index 00000000..7e4fb45d --- /dev/null +++ b/.github/actionlint.yaml @@ -0,0 +1,6 @@ +paths: + .github/workflows/build-and-distribute.yml: + ignore: + # job.workflow_sha was added to GitHub Actions in September 2026 and is not yet known to actionlint. + # Remove this once actionlint supports it. + - 'property "workflow_sha" is not defined in object type' diff --git a/.github/workflows/build-and-distribute.yml b/.github/workflows/build-and-distribute.yml index debaae83..91ab564b 100644 --- a/.github/workflows/build-and-distribute.yml +++ b/.github/workflows/build-and-distribute.yml @@ -44,7 +44,7 @@ on: default: '' type: string PACKAGE_VERSION: - description: The new package version. If not provided, will use latest release version with branch name as pre-release identifier. + description: The new package version. If not provided, will use latest release version with branch name and short SHA as build metadata. required: false type: string PRE_SCRIPT: @@ -87,22 +87,24 @@ on: artifact: description: The name of the generated release artifact. value: ${{ jobs.build-and-distribute.outputs.artifact }} + build-commit: + description: SHA of the build branch commit the artifact was created from. Use it instead of the build branch name to tag, deploy, or publish this exact build. + value: ${{ jobs.build-and-distribute.outputs.build-commit }} jobs: build-and-distribute: # Serialize concurrent builds targeting the same SHA + branch. - # cancel-in-progress: false queues the second run; it will then detect - # the already-built SHA and resolve transparently via artifact generation. + # cancel-in-progress: false queues the second run; if its build fingerprint + # matches, it will find the existing build and resolve transparently via artifact generation. concurrency: group: build-and-distribute-${{ github.sha }}-${{ inputs.BUILT_BRANCH_NAME || github.ref_name }} cancel-in-progress: false timeout-minutes: 10 runs-on: ubuntu-latest outputs: - artifact: ${{ steps.set-artifact-name.outputs.artifact }} - # Used to check whether there are changes to commit + artifact: ${{ steps.set-outputs.outputs.artifact }} + build-commit: ${{ steps.set-outputs.outputs.build-commit }} env: - HAS_GIT_CHANGES: '' # Disables symlinking of local "path" repositories during package installation. # During development, symlinking is preferable. # In production builds, we ship the actual install location and remove the source directory. @@ -159,11 +161,11 @@ jobs: ORIGIN_BRANCH="${{ github.ref_name }}" VERSION_BRANCH="${ORIGIN_BRANCH#dev/}" - # Normalize branch name for semver pre-release identifier + # Normalize branch name for a semver build metadata identifier # Replace invalid characters with hyphens and convert to lowercase NORMALIZED_BRANCH=$(echo "$VERSION_BRANCH" | sed 's/[^a-zA-Z0-9-]/-/g' | sed 's/--*/-/g' | sed 's/^-\|-$//g' | tr '[:upper:]' '[:lower:]') - # Construct semver with pre-release identifier using short SHA + # Construct semver with build metadata from branch name and short SHA PACKAGE_VERSION="${LATEST_RELEASE}+${NORMALIZED_BRANCH}.${SHORT_SHA}" echo "Generated package version: $PACKAGE_VERSION" @@ -237,33 +239,55 @@ jobs: fi fi - - name: Check if already built for this SHA + # Identifies a build by everything that affects its output. Hashed because it is stored in a commit trailer. + - name: Compute build fingerprint + env: + WORKFLOW_SHA: ${{ job.workflow_sha }} + BUILD_INPUTS: | + ${{ github.sha }} + ${{ job.workflow_sha }} + ${{ env.PACKAGE_VERSION }} + ${{ toJSON(inputs) }} run: | - SHA="${{ github.sha }}" - BUILD_BRANCH="${{ env.BUILT_BRANCH_NAME }}" - PACKAGE_FILE="${{ env.PACKAGE_FILE }}" + # Without the workflow revision, a fix to this workflow would not invalidate builds made before it. + if [ -z "$WORKFLOW_SHA" ]; then + echo "::error::job.workflow_sha is empty. It requires GitHub-hosted runners or runner v2.334.0+ and is not available on GHES." + exit 1 + fi + echo "BUILD_KEY=$(printf '%s' "$BUILD_INPUTS" | sha256sum | cut -d' ' -f1)" >> "$GITHUB_ENV" - if [ -z "$PACKAGE_FILE" ]; then - echo "No package file (library) — proceeding with build..." - elif ! git show-ref -q "refs/remotes/origin/$BUILD_BRANCH"; then + # Searches the whole build branch history, not just its tip, so alternating + # inputs (e.g. a dev build and a release build of the same commit) don't rebuild each other away. + - name: Check for an existing build with this fingerprint + run: | + set -euo pipefail + + if ! git show-ref -q "refs/remotes/origin/$BUILT_BRANCH_NAME"; then echo "Build branch does not exist yet — proceeding with build..." + exit 0 + fi + + # No early `exit` in awk: closing the pipe early would SIGPIPE git log and fail the step under pipefail. + BUILD_HIT="$( + git log "origin/$BUILT_BRANCH_NAME" --format='%H %(trailers:key=Build-Key,valueonly,separator=%x2C)' \ + | awk -v key="$BUILD_KEY" '!found && $2 == key { print $1; found = 1 }' + )" + + if [ -n "$BUILD_HIT" ]; then + echo "Existing build found for fingerprint $BUILD_KEY: $BUILD_HIT — reusing." + echo "SKIP_BUILD=true" >> "$GITHUB_ENV" + echo "BUILD_COMMIT=$BUILD_HIT" >> "$GITHUB_ENV" else - COMMITTED="$(git show "origin/$BUILD_BRANCH:$PACKAGE_FILE" 2>/dev/null || echo "")" - if echo "$COMMITTED" | grep -qF "SHA: $SHA" \ - && echo "$COMMITTED" | grep -qF "Version: ${{ env.PACKAGE_VERSION }}"; then - echo "Already built: SHA $SHA at version ${{ env.PACKAGE_VERSION }} — reusing." - echo "SKIP_BUILD=true" >> $GITHUB_ENV - else - echo "SHA or version differs from committed build — rebuilding..." - fi + echo "No existing build for fingerprint $BUILD_KEY — proceeding with build..." fi - name: Checkout the build branch with clean slate run: | if [ "${{ env.SKIP_BUILD }}" = "true" ]; then - # Build already completed for this SHA — check out existing compiled build branch for artifact generation. - git checkout "${{ env.BUILT_BRANCH_NAME }}" - echo "✅ Checked out existing build branch (skipping rebuild)." + # Check out the matching build commit itself, which is not necessarily the branch tip. + # Nothing is pushed on this path, so the build branch stays untouched. + git checkout --detach "$BUILD_COMMIT" + echo "✅ Checked out existing build $BUILD_COMMIT (skipping rebuild)." else # Check if build branch exists remotely if git show-ref -q refs/remotes/origin/${{ env.BUILT_BRANCH_NAME }}; then @@ -387,13 +411,19 @@ jobs: - name: Git add, commit, and push if: ${{ env.SKIP_BUILD != 'true' }} + # --allow-empty records the fingerprint even when the output is identical to the previous build, + # so the next run with the same fingerprint finds it instead of rebuilding. run: | + set -euo pipefail + git add -A - git commit -m "[BOT] Add build artifact from ${{ github.ref_name }} -> ${{ env.BUILT_BRANCH_NAME }}." --no-verify || ((echo "HAS_GIT_CHANGES=yes" >> $GITHUB_ENV) && (echo "No changes to commit.")) - if [ "${{ env.HAS_GIT_CHANGES }}" != "yes" ]; then - git push - echo "Build artifact pushed to branch: ${{ env.BUILT_BRANCH_NAME }}" - fi + git commit --allow-empty --no-verify \ + -m "[BOT] Add build artifact from ${{ github.ref_name }} -> ${{ env.BUILT_BRANCH_NAME }}." \ + --trailer "Build-Key: $BUILD_KEY" + git push origin "HEAD:refs/heads/$BUILT_BRANCH_NAME" + + echo "BUILD_COMMIT=$(git rev-parse HEAD)" >> "$GITHUB_ENV" + echo "Build artifact pushed to branch: ${{ env.BUILT_BRANCH_NAME }}" # Copy the compiled package into a subdirectory to ensure a stable install path in WordPress - name: Prepare artifact with package directory structure @@ -408,12 +438,13 @@ jobs: echo " Package name: ${{ env.PACKAGE_NAME }}" echo " Artifact structure: artifact-staging/${{ env.PACKAGE_NAME }}/" - - name: Set artifact name - id: set-artifact-name + - name: Set outputs + id: set-outputs run: | ARTIFACT_NAME="${{ env.PACKAGE_NAME }}-${{ env.PACKAGE_VERSION }}" echo "ARTIFACT_NAME=$ARTIFACT_NAME" >> $GITHUB_ENV echo "artifact=$ARTIFACT_NAME" >> $GITHUB_OUTPUT + echo "build-commit=$BUILD_COMMIT" >> $GITHUB_OUTPUT - name: Upload artifact uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 @@ -422,4 +453,3 @@ jobs: path: ./artifact-staging/ include-hidden-files: true compression-level: 9 - diff --git a/.github/workflows/wordpress-org-release.yml b/.github/workflows/wordpress-org-release.yml index 584127fe..b5dfe480 100644 --- a/.github/workflows/wordpress-org-release.yml +++ b/.github/workflows/wordpress-org-release.yml @@ -108,7 +108,14 @@ jobs: EXTRACTION_ERROR=0 - PLUGIN_FILE_VERSION=$(grep -m1 'Version:' "$PLUGIN_FILE" | grep -oP '[\d]+\.[\d]+\.[\d]+') + # Mirrors WordPress' get_file_data(): optional ").*%%;s%^[[:space:]]+|[[:space:]]+$%%g;p;q}" "$2" + } + + PLUGIN_FILE_VERSION=$(header_value 'Version' "$PLUGIN_FILE") if [ -z "$PLUGIN_FILE_VERSION" ]; then echo "❌ Could not extract version from $PLUGIN_FILE" EXTRACTION_ERROR=1 @@ -118,7 +125,7 @@ jobs: echo "❌ readme.txt not found" EXTRACTION_ERROR=1 else - README_VERSION=$(grep -m1 'Stable tag:' readme.txt | grep -oP '[\d]+\.[\d]+\.[\d]+' || true) + README_VERSION=$(header_value 'Stable tag' readme.txt) if [ -z "$README_VERSION" ]; then echo "❌ Could not extract 'Stable tag:' from readme.txt" EXTRACTION_ERROR=1 diff --git a/docs/build-and-distribute.md b/docs/build-and-distribute.md index 8ccad8f9..8159908d 100644 --- a/docs/build-and-distribute.md +++ b/docs/build-and-distribute.md @@ -5,7 +5,7 @@ This action can be used to build plugin and theme archives and push them to corr To achieve that, the reusable workflow: 1. Inspects the origin branch and determines the correlating build branch (strips `dev/` prefix) -2. Checks whether the build branch already contains the current commit SHA — if so, skips steps 3–8 entirely (see [Duplicate run detection](#duplicate-run-detection)) +2. Checks whether the build branch already contains a build with the same inputs — if so, skips steps 3–9 entirely (see [Duplicate run detection](#duplicate-run-detection)) 3. Installs dependencies (including dev-dependencies) defined in `composer.json` 4. Installs Node.js dependencies and compiles assets via `npm run build` 5. Updates version and SHA information in plugin/theme headers and `package.json` @@ -31,17 +31,20 @@ This approach keeps source code separate from build artifacts while maintaining If no `PACKAGE_VERSION` is provided, the workflow automatically: 1. Fetches the latest GitHub Release from the repository -2. Strips the `dev/` prefix from the branch name and normalizes it to be semver-compatible -3. Creates a pre-release version like `1.2.3-main` or `2.0.0-abc-123` -4. Falls back to `0.0.0-{branch}` if no published release exists +2. Strips the `dev/` prefix from the branch name and normalizes it to be semver-compatible (characters other than letters, digits, and hyphens become hyphens, the result is lowercased) +3. Appends the normalized branch name and the short (7-character) commit SHA as semver build metadata: `{release}+{branch}.{short-sha}` +4. Falls back to `0.0.0+{branch}.{short-sha}` if no published release exists **Examples:** -- `dev/main` with latest tag `1.2.3` → `1.2.3-main` -- `dev/ABC-123` with latest tag `1.2.3` → `1.2.3-abc-123` -- `dev/feature/user-auth` with latest tag `2.0.0` → `2.0.0-feature-user-auth` +- `dev/main` at commit `abc1234…` with latest tag `1.2.3` → `1.2.3+main.abc1234` +- `dev/ABC-123` at commit `def5678…` with latest tag `1.2.3` → `1.2.3+abc-123.def5678` +- `dev/feature/user-auth` at commit `0a1b2c3…` with latest tag `2.0.0` → `2.0.0+feature-user-auth.0a1b2c3` -This ensures every build has a unique, meaningful version identifier that traces back to both the base release and the source branch. +This ensures every build has a unique, meaningful version identifier that traces back to the base release, the source branch, and the exact commit. + +> [!NOTE] +> The suffix is build metadata (`+`), not a pre-release identifier (`-`). Semver ignores build metadata when comparing versions, so `1.2.3+main.abc1234` has the same precedence as `1.2.3`. It does **not** sort before the release, unlike a pre-release such as `1.2.3-main` would. ### GitHub Release tag format @@ -150,7 +153,7 @@ jobs: | `PHP_TOOLS` | `''` | PHP tools supported by shivammathur/setup-php to be installed | | `COMPOSER_ARGS` | `'--no-dev --prefer-dist --optimize-autoloader'` | Set of arguments passed to Composer when gathering production dependencies | | `PACKAGE_NAME` | `''` | The name of the package (falls back to the repository name) | -| `PACKAGE_VERSION` | `''` | The new package version. If not provided, will use the latest GitHub Release version with branch name as pre-release identifier | +| `PACKAGE_VERSION` | `''` | The new package version. If not provided, will use the latest GitHub Release version with branch name and short SHA as build metadata | | `PRE_SCRIPT` | `''` | Run custom shell code before creating the release archive | | `BUILT_BRANCH_NAME` | `''` | Override the automatic build branch naming (defaults to stripping `dev/` prefix from origin branch) | @@ -178,7 +181,7 @@ By default, the workflow strips the `dev/` prefix from the origin branch to dete - Ensure your `package.json` includes a `build` script for asset compilation - Use `.distignore` to exclude development files from the final build - Consider using [path filters](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#example-including-paths) to avoid unnecessary builds when only documentation changes -- Use [concurrency settings](https://docs.github.com/en/actions/using-jobs/using-concurrency) to prevent conflicts when multiple pushes occur rapidly — the workflow serializes concurrent builds for the same SHA internally, so a queued second run will skip the actual build and resolve via the pre-built artifact +- Use [concurrency settings](https://docs.github.com/en/actions/using-jobs/using-concurrency) to prevent conflicts when multiple pushes occur rapidly — the workflow serializes concurrent builds for the same SHA internally, so a queued second run with the same inputs will skip the actual build and resolve via the existing build ```yml name: Build and push assets @@ -194,7 +197,7 @@ on: workflow_dispatch: inputs: CUSTOM_PACKAGE_VERSION: - description: 'Custom Package Version (skip auto-detection of pre-release version)' + description: 'Custom Package Version (skip auto-generation of the development version)' required: false CUSTOM_BUILD_BRANCH: description: 'Custom build branch name' @@ -275,13 +278,21 @@ If a `scoper.inc.php` file is present, the workflow will: ### Duplicate run detection -The workflow detects when the build branch already contains a build for the current commit SHA and skips the entire compilation pipeline in that case. +The workflow detects when the build branch already contains a build made from the same inputs and skips the entire compilation pipeline in that case. + +Before any toolchain setup, the workflow computes a build fingerprint (a SHA-256 hash) from everything that affects the build output: the commit SHA, the resolved package version, all workflow inputs, and the revision of this reusable workflow itself. `ENV_VARS` is a secret and is not part of the fingerprint: changing it alone does not trigger a rebuild. Every build commit records its fingerprint in a `Build-Key:` commit trailer. + +The workflow then searches the **whole history** of the build branch, not just its tip, for a commit with the same fingerprint: -After each successful build, the commit SHA is written into the `SHA:` header of `style.css` (themes) or the main plugin PHP file (plugins). On subsequent runs for the same commit, the workflow reads that field from the remote build branch _before_ any toolchain setup. If the SHA matches, all build and compile steps are skipped and the workflow proceeds directly to packaging the existing build branch content and uploading the artifact. +- **Match:** All build steps are skipped. The artifact is created from the matching commit, and nothing is pushed — the build branch stays untouched. +- **No match:** The package is built, committed with its fingerprint, and pushed. -This is transparent to downstream jobs: the `artifact` output is always populated, whether the build was freshly compiled or reused. +As a result, the same commit can be built with different inputs without them interfering. For example, a release build with an explicit `PACKAGE_VERSION` always rebuilds, even if a development build of the same commit already exists, while re-running that release build reuses it. -**Applies to WordPress plugins and themes only.** Library projects do not embed a `SHA:` field, so they always rebuild. +This is transparent to downstream jobs: the `artifact` and `build-commit` outputs are always populated, whether the build was freshly compiled or reused. + +> [!IMPORTANT] +> Do not rewrite the history of build branches (squash, force-push). Removed builds can no longer be found and are rebuilt. ### Distignore Support @@ -300,9 +311,16 @@ The workflow produces two outputs: 1. **Build Branch**: The compiled code pushed to the build branch (e.g., `dev/main` → `main`) 2. **GitHub Artifact**: A downloadable archive named `{package-name}-{version}` containing the build (without `.git` folder) -### Workflow output +### Workflow outputs + +| Name | Description | +|----------------|--------------------------------------------------------------| +| `artifact` | Name of the uploaded GitHub artifact | +| `build-commit` | SHA of the build branch commit the artifact was created from | -The artifact name is also exposed as a workflow output called `artifact`, so downstream jobs in the calling workflow can reference it without having to reconstruct the name themselves: +When tagging, deploying, or publishing a build, always reference `build-commit` (or the artifact) instead of the build branch name. If a build is reused (see [Duplicate run detection](#duplicate-run-detection)), it is not necessarily the tip of the build branch, so a branch name can resolve to a different build than the one this run produced. + +Downstream jobs in the calling workflow can reference the outputs without having to reconstruct them: ```yml jobs: @@ -319,6 +337,18 @@ jobs: - uses: actions/download-artifact@v4 with: name: ${{ needs.build-and-distribute.outputs.artifact }} + + # Releases only: development builds have no MAJOR.MINOR.PATCH version to publish. + publish: + needs: build-and-distribute + if: ${{ inputs.PACKAGE_VERSION != '' }} + uses: inpsyde/reusable-workflows/.github/workflows/wordpress-org-release.yml@main + secrets: inherit + with: + SVN_PLUGIN_SLUG: my-plugin + PLUGIN_VERSION: ${{ inputs.PACKAGE_VERSION }} + GIT_REF: ${{ needs.build-and-distribute.outputs.build-commit }} + UPDATE_TRUNK_ONLY: false ``` ## Migration from previous workflows diff --git a/docs/wordpress-org-release.md b/docs/wordpress-org-release.md index b22e2b9d..e5747a76 100644 --- a/docs/wordpress-org-release.md +++ b/docs/wordpress-org-release.md @@ -6,7 +6,7 @@ To achieve that, this workflow: 1. Validates that `PLUGIN_VERSION` is formatted as `MAJOR.MINOR.PATCH` 2. Checks out the Git repository at `GIT_REF` -3. Verifies that the plugin file header's `Version:` matches `PLUGIN_VERSION` +3. Verifies that the plugin file header's `Version:` exactly equals `PLUGIN_VERSION` (a build-stamped version like `1.2.3+main.abc1234` is rejected for `1.2.3`) 4. Checks out the WordPress.org SVN repository (trunk is fully checked out; tag contents are never fetched — only tag names are listed when needed for the checks below) 5. Verifies `readme.txt`'s `Stable tag`, depending on `UPDATE_TRUNK_ONLY`: - `true` (trunk-only): must already exist as a published SVN tag