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
235 changes: 136 additions & 99 deletions .github/workflows/build-and-distribute.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,58 +86,39 @@ on:
outputs:
artifact:
description: The name of the generated release artifact.
value: ${{ jobs.build-and-distribute.outputs.artifact }}
value: ${{ jobs.package.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 }}
value: ${{ jobs.package.outputs.build-commit }}

# Serializes whole runs (lookup, build, and push) per build branch, so a queued run sees the previous run's push.
# Note: a newly queued run cancels an already pending one in the same group.
# The group uses the origin branch because expressions cannot strip the 'dev/' prefix.
concurrency:
group: build-and-distribute-${{ inputs.BUILT_BRANCH_NAME || github.ref_name }}
cancel-in-progress: false

env:
PACKAGE_NAME: ${{ inputs.PACKAGE_NAME && inputs.PACKAGE_NAME || github.event.repository.name }}

jobs:
build-and-distribute:
# Serialize concurrent builds targeting the same SHA + branch.
# 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
resolve:
timeout-minutes: 5
runs-on: ubuntu-latest
outputs:
artifact: ${{ steps.set-outputs.outputs.artifact }}
build-commit: ${{ steps.set-outputs.outputs.build-commit }}
package-version: ${{ steps.set-outputs.outputs.package-version }}
built-branch-name: ${{ steps.set-outputs.outputs.built-branch-name }}
build-key: ${{ steps.set-outputs.outputs.build-key }}
build-commit: ${{ steps.lookup.outputs.build-commit }}
env:
# 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.
COMPOSER_MIRROR_PATH_REPOS: 1
PACKAGE_VERSION: ${{ inputs.PACKAGE_VERSION }} # Will be overridden by determine package version step
PACKAGE_NAME: ${{ inputs.PACKAGE_NAME && inputs.PACKAGE_NAME || github.event.repository.name }}
steps:
- name: Checkout
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
with:
fetch-depth: 0
ssh-key: ${{ secrets.GITHUB_USER_SSH_KEY }}

- name: Set up Git
uses: inpsyde/reusable-workflows/.github/actions/setup-git@ce4cfd9ad8176f895d09ae11cb12dd65590cc94e # main
with:
ssh-private-key: ${{ secrets.GITHUB_USER_SSH_KEY }}
user-email: ${{ secrets.GITHUB_USER_EMAIL }}
user-name: ${{ secrets.GITHUB_USER_NAME }}
ssh-public-key: ${{ secrets.GITHUB_USER_SSH_PUBLIC_KEY }}
automated-commits: 'true'

- name: Set up custom environment variables
env:
ENV_VARS: ${{ secrets.ENV_VARS }}
if: ${{ env.ENV_VARS }}
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7.1.0
with:
script: |
JSON
.parse(process.env.ENV_VARS)
.forEach(envVar => core.exportVariable(envVar.name, envVar.value));

- name: Determine package version
if: ${{ !inputs.PACKAGE_VERSION }}
env:
Expand Down Expand Up @@ -219,26 +200,6 @@ jobs:
echo " Source branch: $ORIGIN_BRANCH"
echo " Build branch: ${{ env.BUILT_BRANCH_NAME }}"

- name: Determine package type
run: |
# Check for WordPress theme
if [ -f "style.css" ] && grep -q "Theme Name:" style.css; then
echo "Package type: WordPress Theme"
echo "PACKAGE_FILE=style.css" >> $GITHUB_ENV
# Check for WordPress plugin
else
# Find the first PHP file in the project root with "Plugin Name" header
PLUGIN_FILE=$(find . -maxdepth 1 -name "*.php" -type f -exec grep -l "Plugin Name:" {} \; 2>/dev/null | head -n1)
if [ -n "$PLUGIN_FILE" ]; then
echo "Package type: WordPress Plugin"
echo "Plugin file found: $PLUGIN_FILE"
echo "PACKAGE_FILE=$PLUGIN_FILE" >> $GITHUB_ENV
# Nothing to do for generic library
else
echo "Package type: Library"
fi
fi

# Identifies a build by everything that affects its output. Hashed because it is stored in a commit trailer.
- name: Compute build fingerprint
env:
Expand All @@ -259,6 +220,7 @@ jobs:
# 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
id: lookup
run: |
set -euo pipefail

Expand All @@ -274,43 +236,104 @@ jobs:
)"

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"
echo "Existing build found for fingerprint $BUILD_KEY: $BUILD_HIT — skipping build."
echo "build-commit=$BUILD_HIT" >> "$GITHUB_OUTPUT"
else
echo "No existing build for fingerprint $BUILD_KEY — proceeding with build..."
fi

- name: Checkout the build branch with clean slate
- name: Set outputs
id: set-outputs
run: |
echo "package-version=$PACKAGE_VERSION" >> "$GITHUB_OUTPUT"
echo "built-branch-name=$BUILT_BRANCH_NAME" >> "$GITHUB_OUTPUT"
echo "build-key=$BUILD_KEY" >> "$GITHUB_OUTPUT"

build:
needs: resolve
if: ${{ needs.resolve.outputs.build-commit == '' }}
timeout-minutes: 10
runs-on: ubuntu-latest
outputs:
build-commit: ${{ steps.push.outputs.build-commit }}
env:
# 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.
COMPOSER_MIRROR_PATH_REPOS: 1
PACKAGE_VERSION: ${{ needs.resolve.outputs.package-version }}
BUILT_BRANCH_NAME: ${{ needs.resolve.outputs.built-branch-name }}
BUILD_KEY: ${{ needs.resolve.outputs.build-key }}
steps:
- name: Checkout
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
with:
fetch-depth: 0
ssh-key: ${{ secrets.GITHUB_USER_SSH_KEY }}

- name: Set up Git
uses: inpsyde/reusable-workflows/.github/actions/setup-git@ce4cfd9ad8176f895d09ae11cb12dd65590cc94e # main
with:
ssh-private-key: ${{ secrets.GITHUB_USER_SSH_KEY }}
user-email: ${{ secrets.GITHUB_USER_EMAIL }}
user-name: ${{ secrets.GITHUB_USER_NAME }}
ssh-public-key: ${{ secrets.GITHUB_USER_SSH_PUBLIC_KEY }}
automated-commits: 'true'

- name: Set up custom environment variables
env:
ENV_VARS: ${{ secrets.ENV_VARS }}
if: ${{ env.ENV_VARS }}
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7.1.0
with:
script: |
JSON
.parse(process.env.ENV_VARS)
.forEach(envVar => core.exportVariable(envVar.name, envVar.value));

- name: Determine package type
run: |
if [ "${{ env.SKIP_BUILD }}" = "true" ]; then
# 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)."
# Check for WordPress theme
if [ -f "style.css" ] && grep -q "Theme Name:" style.css; then
echo "Package type: WordPress Theme"
echo "PACKAGE_FILE=style.css" >> $GITHUB_ENV
# Check for WordPress plugin
else
# Check if build branch exists remotely
if git show-ref -q refs/remotes/origin/${{ env.BUILT_BRANCH_NAME }}; then
echo "Build branch exists, checking out and cleaning..."
git checkout ${{ env.BUILT_BRANCH_NAME }}

# Clean everything except .git directory for a fresh start
git rm -rf . || true
git clean -fxd
echo "✅ Cleaned existing build branch for fresh build"
# Find the first PHP file in the project root with "Plugin Name" header
PLUGIN_FILE=$(find . -maxdepth 1 -name "*.php" -type f -exec grep -l "Plugin Name:" {} \; 2>/dev/null | head -n1)
if [ -n "$PLUGIN_FILE" ]; then
echo "Package type: WordPress Plugin"
echo "Plugin file found: $PLUGIN_FILE"
echo "PACKAGE_FILE=$PLUGIN_FILE" >> $GITHUB_ENV
# Nothing to do for generic library
else
echo "Build branch doesn't exist, creating new branch..."
git checkout -b ${{ env.BUILT_BRANCH_NAME }}
echo "✅ Created new build branch"
echo "Package type: Library"
fi
fi

# Copy all files from source branch
git checkout ${{ github.ref_name }} -- .
echo "✅ Copied source files to build branch"
- name: Checkout the build branch with clean slate
run: |
# Check if build branch exists remotely
if git show-ref -q refs/remotes/origin/${{ env.BUILT_BRANCH_NAME }}; then
echo "Build branch exists, checking out and cleaning..."
git checkout ${{ env.BUILT_BRANCH_NAME }}

# Clean everything except .git directory for a fresh start
git rm -rf . || true
git clean -fxd
echo "✅ Cleaned existing build branch for fresh build"
else
echo "Build branch doesn't exist, creating new branch..."
git checkout -b ${{ env.BUILT_BRANCH_NAME }}
echo "✅ Created new build branch"
fi

# Copy all files from source branch
git checkout ${{ github.ref_name }} -- .
echo "✅ Copied source files to build branch"

- name: Prepare package
if: ${{ env.SKIP_BUILD != 'true' && env.PACKAGE_FILE != '' }}
if: ${{ env.PACKAGE_FILE != '' }}
run: |
echo "Updating ${{ env.PACKAGE_FILE }}..."
if grep -q "SHA:" "${{ env.PACKAGE_FILE }}"; then
Expand All @@ -327,7 +350,6 @@ jobs:

- name: Check for composer.json
id: check-composer
if: ${{ env.SKIP_BUILD != 'true' }}
run: |
if [ -f "composer.json" ]; then
echo "Composer project detected (composer.json exists)"
Expand All @@ -338,7 +360,7 @@ jobs:
fi

- name: Set up PHP
if: ${{ env.SKIP_BUILD != 'true' && env.HAS_COMPOSER == 'true' }}
if: ${{ env.HAS_COMPOSER == 'true' }}
uses: shivammathur/setup-php@f3e473d116dcccaddc5834248c87452386958240 # v2.37.2
with:
php-version: ${{ inputs.PHP_VERSION }}
Expand All @@ -347,15 +369,14 @@ jobs:
coverage: none

- name: Install Composer dependencies
if: ${{ env.SKIP_BUILD != 'true' && env.HAS_COMPOSER == 'true' }}
if: ${{ env.HAS_COMPOSER == 'true' }}
uses: ramsey/composer-install@65e4f84970763564f46a70b8a54b90d033b3bdda # v4.0.0
env:
COMPOSER_AUTH: '${{ secrets.COMPOSER_AUTH_JSON }}'
with:
composer-options: ${{ inputs.COMPOSER_ARGS }}

- name: Set up Node
if: ${{ env.SKIP_BUILD != 'true' }}
uses: inpsyde/reusable-workflows/.github/actions/setup-node@6fa02cd6f055bc5161f433af6f442806ff1721d9 # main
with:
node-version: ${{ inputs.NODE_VERSION }}
Expand All @@ -365,31 +386,28 @@ jobs:
node-options: ${{ inputs.NODE_OPTIONS }}

- name: Update version in package.json
if: ${{ env.SKIP_BUILD != 'true' }}
run: npm version ${{ env.PACKAGE_VERSION }} --no-git-tag-version --allow-same-version

- name: Update version in composer.json
if: ${{ env.SKIP_BUILD != 'true' && env.HAS_COMPOSER == 'true' }}
if: ${{ env.HAS_COMPOSER == 'true' }}
run: |
echo "Updating composer.json version to ${{ env.PACKAGE_VERSION }}."
jq --arg version "${{ env.PACKAGE_VERSION }}" '.version = $version' composer.json > composer.json.tmp && mv composer.json.tmp composer.json
echo "✅ Updated composer.json version field."

- name: Execute custom code before archive creation
if: ${{ env.SKIP_BUILD != 'true' }}
run: |
${{ inputs.PRE_SCRIPT }}

- name: Compile assets
if: ${{ env.SKIP_BUILD != 'true' }}
run: npm run build

- name: Run WordPress Translation Downloader
if: ${{ env.SKIP_BUILD != 'true' && env.HAS_COMPOSER == 'true' }}
if: ${{ env.HAS_COMPOSER == 'true' }}
run: composer wp-translation-downloader:download

- name: Run PHP-Scoper
if: ${{ env.SKIP_BUILD != 'true' && env.HAS_COMPOSER == 'true' && hashFiles('scoper.inc.php') != '' }}
if: ${{ env.HAS_COMPOSER == 'true' && hashFiles('scoper.inc.php') != '' }}
# The sed call appends the Git commit SHA to the Composer autoload cache key to ensure unique identification for prefixed files.
# This prevents Composer from skipping autoloaded files due to hash collisions based on relative paths.
run: |
Expand All @@ -399,7 +417,7 @@ jobs:
rsync -av ./build/ . && rm -rf ./build/

- name: Apply .distignore file
if: ${{ env.SKIP_BUILD != 'true' && hashFiles('.distignore') != '' }}
if: ${{ hashFiles('.distignore') != '' }}
# Configure git to (gracefully) use .distignore file during packaging instead of the regular .gitignore file.
# Then clean up all files possibly mentioned in .distignore (e.g., source files).
run: |
Expand All @@ -410,7 +428,7 @@ jobs:
git clean -Xdf

- name: Git add, commit, and push
if: ${{ env.SKIP_BUILD != 'true' }}
id: push
# --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: |
Expand All @@ -422,9 +440,28 @@ jobs:
--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-commit=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"
echo "Build artifact pushed to branch: ${{ env.BUILT_BRANCH_NAME }}"

# Packages the build commit, whether it was just built or found by the lookup.
package:
needs: [resolve, build]
if: ${{ !cancelled() && !failure() }}
timeout-minutes: 5
runs-on: ubuntu-latest
outputs:
artifact: ${{ steps.set-outputs.outputs.artifact }}
build-commit: ${{ steps.set-outputs.outputs.build-commit }}
env:
BUILD_COMMIT: ${{ needs.build.outputs.build-commit || needs.resolve.outputs.build-commit }}
PACKAGE_VERSION: ${{ needs.resolve.outputs.package-version }}
steps:
- name: Checkout the build commit
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
with:
ref: ${{ env.BUILD_COMMIT }}
ssh-key: ${{ secrets.GITHUB_USER_SSH_KEY }}

# Copy the compiled package into a subdirectory to ensure a stable install path in WordPress
- name: Prepare artifact with package directory structure
run: |
Expand All @@ -441,10 +478,10 @@ jobs:
- 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
ARTIFACT_NAME="$PACKAGE_NAME-$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 Down
Loading
Loading