Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/actionlint.yaml
Original file line number Diff line number Diff line change
@@ -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'
98 changes: 64 additions & 34 deletions .github/workflows/build-and-distribute.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -422,4 +453,3 @@ jobs:
path: ./artifact-staging/
include-hidden-files: true
compression-level: 9

11 changes: 9 additions & 2 deletions .github/workflows/wordpress-org-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<?php", then blanks or comment characters,
# case-insensitive field name, comment closers stripped. Reads the complete value, so e.g.
# "1.2.3+main.abc1234" or "1.2.3-rc1" is rejected instead of being truncated to "1.2.3".
header_value() {
sed -nE "\%^([[:blank:]]*<\?php)?[[:blank:]/*#@]*$1:%I{s%^([[:blank:]]*<\?php)?[[:blank:]/*#@]*$1:(.*)$%\2%I;s%[[:space:]]*(\*/|\?>).*%%;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
Expand All @@ -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
Expand Down
Loading
Loading