diff --git a/.github/workflows/build-and-distribute.yml b/.github/workflows/build-and-distribute.yml index 91ab564b..21e22f55 100644 --- a/.github/workflows/build-and-distribute.yml +++ b/.github/workflows/build-and-distribute.yml @@ -86,31 +86,32 @@ 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 @@ -118,26 +119,6 @@ jobs: 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: @@ -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: @@ -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 @@ -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 @@ -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)" @@ -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 }} @@ -347,7 +369,7 @@ 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 }}' @@ -355,7 +377,6 @@ jobs: 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 }} @@ -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: | @@ -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: | @@ -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: | @@ -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: | @@ -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 diff --git a/docs/build-and-distribute.md b/docs/build-and-distribute.md index 8159908d..4766f255 100644 --- a/docs/build-and-distribute.md +++ b/docs/build-and-distribute.md @@ -2,18 +2,20 @@ This action can be used to build plugin and theme archives and push them to corresponding build branches in a controlled and isolated environment via GitHub Actions. -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 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` -6. Executes [WordPress Translation Downloader](https://github.com/inpsyde/wp-translation-downloader) if configured by the package -7. Executes [PHP-Scoper](https://github.com/humbug/php-scoper) if configured by the package -8. Applies `.distignore` file filtering if present -9. Commits and pushes the build artifact to the determined build branch -10. Uploads the build as a GitHub Actions artifact for download +To achieve that, the reusable workflow runs three jobs: + +1. **`resolve`** + 1. Determines the package version and the correlating build branch (strips `dev/` prefix) + 2. Checks whether the build branch already contains a build with the same inputs (see [Duplicate run detection](#duplicate-run-detection)) +2. **`build`** — skipped entirely if a matching build exists + 1. Installs dependencies (including dev-dependencies) defined in `composer.json` + 2. Installs Node.js dependencies and compiles assets via `npm run build` + 3. Updates version and SHA information in plugin/theme headers and `package.json` + 4. Executes [WordPress Translation Downloader](https://github.com/inpsyde/wp-translation-downloader) if configured by the package + 5. Executes [PHP-Scoper](https://github.com/humbug/php-scoper) if configured by the package + 6. Applies `.distignore` file filtering if present + 7. Commits and pushes the build to the determined build branch +3. **`package`** — uploads the build commit (freshly built or reused) as a GitHub Actions artifact for download ## Branch naming convention @@ -181,7 +183,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 with the same inputs will skip the actual build and resolve via the existing build +- Use [concurrency settings](https://docs.github.com/en/actions/using-jobs/using-concurrency) in the calling workflow if needed. The workflow itself already serializes runs per build branch (see [Concurrency](#concurrency)), so don't reuse its group name `build-and-distribute-` in the calling job, as that would deadlock ```yml name: Build and push assets @@ -284,8 +286,8 @@ Before any toolchain setup, the workflow computes a build fingerprint (a SHA-256 The workflow then searches the **whole history** of the build branch, not just its tip, for a commit with the same fingerprint: -- **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. +- **Match:** The `build` job is skipped. The artifact is created from the matching commit, and nothing is pushed — the build branch stays untouched. +- **No match:** The `build` job builds the package, commits it with its fingerprint, and pushes it. 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. @@ -294,6 +296,17 @@ This is transparent to downstream jobs: the `artifact` and `build-commit` output > [!IMPORTANT] > Do not rewrite the history of build branches (squash, force-push). Removed builds can no longer be found and are rebuilt. +### Concurrency + +All runs targeting the same build branch are serialized as a whole (lookup, build, and push), so a queued run always sees the build pushed by the run before it. Two identical runs never build twice, and two different commits on the same branch never race on push. + +GitHub keeps at most one pending run per concurrency group, and a newly queued run cancels the pending one. For rapid pushes this means intermediate commits may not be built, which is usually fine for a build branch. + +> [!WARNING] +> A pending release run (explicit `PACKAGE_VERSION`) is cancelled as well if a development push to the same branch is queued after it. The release run then shows as cancelled and has to be re-run. + +The concurrency group uses `BUILT_BRANCH_NAME` if given, otherwise the origin branch name (e.g. `dev/main`), because workflow expressions cannot strip the `dev/` prefix. + ### Distignore Support If a `.distignore` file is present, the workflow will: @@ -309,7 +322,7 @@ This is particularly useful for excluding source files, tests, and development t 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) +2. **GitHub Artifact**: A downloadable archive named `{package-name}-{version}` containing exactly the files committed in the build commit (without `.git` folder) ### Workflow outputs