diff --git a/.github/workflows/deployment.yml b/.github/workflows/deployment.yml index 7c87d40da..ded61d737 100644 --- a/.github/workflows/deployment.yml +++ b/.github/workflows/deployment.yml @@ -350,18 +350,33 @@ jobs: chmod +x /tmp/wp-cli-php-probe WP_CLI_PHP=/tmp/wp-cli-php-probe WP_CLI_PHP_ARGS="-d memory_limit=256M" rpm-verify/usr/bin/wp cli version + # Replaces the package of a version that is not released yet, so it + # always carries the Phar this push built; never touches a released one. - name: Copy RPM package into builds folder + env: + GH_TOKEN: ${{ github.token }} run: | - cp -n rpm-src/noarch/wp-cli-*.noarch.rpm rpm + bash bundle/utils/copy-release-package.sh rpm-src/noarch/wp-cli-*.noarch.rpm rpm - name: Commit files + id: commit run: | git config --local user.email "action@github.com" git config --local user.name "GitHub Action" git add rpm + + if git diff --staged --quiet; then + echo "The RPM package in wp-cli/builds is already up to date." + exit 0 + fi + git commit -m "rpm build: $GITHUB_REPOSITORY@$GITHUB_SHA" + echo "committed=true" >> "$GITHUB_OUTPUT" + # Nothing to push otherwise, and a no-op push can still fail when + # another build pushed to gh-pages in the meantime. - name: Push changes + if: ${{ steps.commit.outputs.committed == 'true' }} uses: ad-m/github-push-action@881a6320fdb16eb5318c5054f31c218aec2b324c # v1 with: github_token: ${{ secrets.ACTIONS_BOT }} @@ -450,18 +465,33 @@ jobs: exit 1 fi + # Replaces the package of a version that is not released yet, so it + # always carries the Phar this push built; never touches a released one. - name: Copy DEB package into builds folder + env: + GH_TOKEN: ${{ github.token }} run: | - cp -n php-wpcli*all.deb deb + bash bundle/utils/copy-release-package.sh php-wpcli*all.deb deb - name: Commit files + id: commit run: | git config --local user.email "action@github.com" git config --local user.name "GitHub Action" git add deb + + if git diff --staged --quiet; then + echo "The DEB package in wp-cli/builds is already up to date." + exit 0 + fi + git commit -m "deb build: $GITHUB_REPOSITORY@$GITHUB_SHA" + echo "committed=true" >> "$GITHUB_OUTPUT" + # Nothing to push otherwise, and a no-op push can still fail when + # another build pushed to gh-pages in the meantime. - name: Push changes + if: ${{ steps.commit.outputs.committed == 'true' }} uses: ad-m/github-push-action@881a6320fdb16eb5318c5054f31c218aec2b324c # v1 with: github_token: ${{ secrets.ACTIONS_BOT }} diff --git a/utils/copy-release-package.sh b/utils/copy-release-package.sh new file mode 100755 index 000000000..4bf74faf3 --- /dev/null +++ b/utils/copy-release-package.sh @@ -0,0 +1,60 @@ +#!/usr/bin/env bash +# +# Copies a freshly built RPM or DEB package into the wp-cli/builds checkout. +# +# Usage: copy-release-package.sh +# +# Every push to a release-** branch rebuilds wp-cli-release.phar, so the +# package for that version has to be rebuilt along with it: the release +# automation in wp-cli/wp-cli refuses a package that carries a different Phar +# than the one being released. A package for a version that is not released +# yet is therefore replaced. +# +# A package for a published release is never replaced, as users may already +# have installed it. Published releases on wp-cli/wp-cli are what counts: the +# release-by-tag endpoint never returns drafts, whatever the credentials. + +set -euo pipefail + +if [ "$#" -ne 2 ] || [ ! -f "$1" ] || [ ! -d "$2" ]; then + echo "Usage: $0 " >&2 + exit 1 +fi + +PACKAGE="$1" +NAME="$(basename "$PACKAGE")" +DEST="$2/$NAME" + +# Only a plain version is accepted: it ends up in the API path below, and +# php-wpcli_latest_all.deb must never match. The RPM release may carry a +# %{?dist} suffix (wp-cli-3.0.1-3.el9.noarch.rpm). +VERSION_PATTERN='[0-9]+\.[0-9]+\.[0-9]+(-[A-Za-z0-9.]+)?' +if [[ "$NAME" =~ ^php-wpcli_(${VERSION_PATTERN})_all\.deb$ ]] || [[ "$NAME" =~ ^wp-cli-(${VERSION_PATTERN})-[0-9]+(\.[^.]+)?\.noarch\.rpm$ ]]; then + VERSION="${BASH_REMATCH[1]}" +else + echo "::error::Cannot tell the version of $NAME." >&2 + exit 1 +fi + +if [ -e "$DEST" ]; then + if cmp -s "$PACKAGE" "$DEST"; then + echo "$DEST is already up to date." + exit 0 + fi + + if ERROR="$(gh api "repos/wp-cli/wp-cli/releases/tags/v${VERSION}" --silent 2>&1)"; then + echo "::notice::v${VERSION} is already released. Keeping the published $DEST." + exit 0 + fi + + # Fails closed: anything but "no such release" could be a published one. + if [[ "$ERROR" != *'HTTP 404'* ]]; then + echo "::error::Could not check whether v${VERSION} is released:" >&2 + printf '%s\n' "$ERROR" >&2 + exit 1 + fi + + echo "::notice::Replacing $DEST, which was built from an earlier push of the release branch." +fi + +cp "$PACKAGE" "$DEST"