diff --git a/.github/release-please/split-publish.sh b/.github/release-please/split-publish.sh new file mode 100644 index 00000000..51e2ee0f --- /dev/null +++ b/.github/release-please/split-publish.sh @@ -0,0 +1,109 @@ +#!/usr/bin/env bash +# +# Usage: split-publish.sh +# +# Publishes of the monorepo checkout as the content of in +# the target repository, then tags that commit . Retries the publication +# a few times; fails if every attempt fails. +# +# An existing in the target repository means the release is already +# published, and a newer tag there means publishing would roll back; +# both stop the script without pushing. +# +# Env: GITHUB_TOKEN with `contents: write` on the target repository. + +set -euo pipefail + +if [ "$#" -ne 4 ] || [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ]; then + echo "::error::Usage: split-publish.sh , all non-empty; got: $*" + exit 1 +fi + +if [ ! -d "$1" ]; then + echo "::error::Package directory $1 does not exist." + exit 1 +fi + +if [ -z "${GITHUB_TOKEN:-}" ]; then + echo "::error::GITHUB_TOKEN is not set; pass a token with write access to $2." + exit 1 +fi + +directory="$(realpath "$1")" +repository="$2" +branch="$3" +tag="$4" + +url="https://x-access-token:${GITHUB_TOKEN}@github.com/${repository}.git" +message="$(git log -1 --format=%s)" +work="$(mktemp -d)" + +# Exit status of publish() when there is nothing to retry. +readonly REFUSED=3 + +publish() { + # Plain `ls-remote` prints nothing for an absent ref and fails only on a lookup + # error; its output is assigned on a line of its own so `set -e` sees that failure. + local tags newest + tags="$(git ls-remote --tags --refs "${url}" | sed 's|.*refs/tags/||')" + if grep -qxF "${tag}" <<< "${tags}"; then + echo "Tag ${tag} already exists in ${repository}; nothing to publish." + return 0 + fi + # Each major series has its own branch, and `sort -V` ranks a pre-release + # above its release: compare plain X.Y.Z releases of the same major only. + if [[ "${tag}" =~ ^([0-9]+)\.[0-9]+\.[0-9]+$ ]]; then + newest="$( { grep -E "^${BASH_REMATCH[1]}\.[0-9]+\.[0-9]+$" <<< "${tags}" || true; echo "${tag}"; } \ + | sort -V | tail -n 1)" + if [ "${newest}" != "${tag}" ]; then + echo "::error::${repository} already has ${newest}, newer than ${tag}; publishing would roll ${branch} back." + return "${REFUSED}" + fi + fi + + local heads + heads="$(git ls-remote --heads "${url}" "refs/heads/${branch}")" + rm -rf "${work}/repo" + if [ -n "${heads}" ]; then + git clone --quiet --depth 1 --branch "${branch}" "${url}" "${work}/repo" + else + git init --quiet --initial-branch "${branch}" "${work}/repo" + git -C "${work}/repo" remote add origin "${url}" + fi + + find "${work}/repo" -mindepth 1 -maxdepth 1 ! -name .git -exec rm -rf {} + + cp -a "${directory}/." "${work}/repo/" + + local git=(git -C "${work}/repo" + -c user.name='github-actions[bot]' + -c user.email='41898282+github-actions[bot]@users.noreply.github.com') + "${git[@]}" add --all + if "${git[@]}" diff --cached --quiet; then + echo "No changes in ${directory}; tagging the current ${branch} head." + else + "${git[@]}" commit --quiet --message "${message}" + fi + "${git[@]}" tag --annotate "${tag}" --message "Publishing ${tag}" + + # Atomic: a rejected branch never leaves a tag behind on an orphan commit. + "${git[@]}" push --atomic origin "HEAD:refs/heads/${branch}" "refs/tags/${tag}" +} + +for attempt in 1 2 3; do + # Not `if publish`: a function called as a condition runs with `set -e` off. + set +e + (set -e; publish) + status=$? + set -e + case "${status}" in + 0) exit 0 ;; + "${REFUSED}") exit 1 ;; + esac + echo "::warning::Publishing ${tag} to ${repository} failed (attempt ${attempt})." + if [ "${attempt}" -lt 3 ]; then + sleep 30 + fi +done + +echo "::error::Could not publish ${tag} to ${repository}." +exit 1 diff --git a/.github/workflows/create-release.yml b/.github/workflows/create-release.yml index 39637b30..31fbc4b5 100644 --- a/.github/workflows/create-release.yml +++ b/.github/workflows/create-release.yml @@ -29,18 +29,54 @@ jobs: release: runs-on: ubuntu-latest outputs: - # Root testo/testo version released by this run, by release-please or by the guard; empty otherwise. - root_version: ${{ steps.release.outputs.release_created == 'true' && steps.release.outputs.version || steps.guard.outputs.version }} + # Root testo/testo version released by this run, by release-please, the guard or the recovery; empty otherwise. + root_version: ${{ steps.release.outputs.release_created == 'true' && steps.release.outputs.version || steps.guard.outputs.version || steps.recover.outputs.version }} steps: + # A single 5xx from the GitHub API fails release-please, and walking a long + # history for a package with no release yet makes one likely. A second run + # is safe: it finds the releases and the PR the failed attempt already made. - name: ๐ŸŽ‰ Create release uses: googleapis/release-please-action@v5 - id: release - with: + id: attempt + continue-on-error: true + with: &release-please-inputs token: ${{ secrets.RELEASE_TOKEN }} config-file: .github/.release-please-config.json manifest-file: resources/version.json target-branch: 1.x + - name: โณ Wait before retrying release-please + if: ${{ steps.attempt.outcome == 'failure' }} + run: sleep 60 + + - name: ๐ŸŽ‰ Create release (retry) + uses: googleapis/release-please-action@v5 + id: retry + if: ${{ steps.attempt.outcome == 'failure' }} + with: *release-please-inputs + + # release-please writes the release outputs before it handles PRs, so an + # attempt that failed on PRs still reports the releases it made; per output, + # `||` takes what the attempt reported and falls back to the retry. + - name: ๐Ÿงพ Collect release-please outputs + id: release + env: + RELEASE_CREATED: ${{ steps.attempt.outputs.release_created || steps.retry.outputs.release_created }} + VERSION: ${{ steps.attempt.outputs.version || steps.retry.outputs.version }} + PRS_CREATED: ${{ steps.attempt.outputs.prs_created || steps.retry.outputs.prs_created }} + PR: ${{ steps.attempt.outputs.pr || steps.retry.outputs.pr }} + run: | + pr_branch='' + if [ -n "${PR}" ]; then + pr_branch=$(jq -r '.headBranchName // empty' <<< "${PR}") + fi + { + echo "release_created=${RELEASE_CREATED}" + echo "version=${VERSION}" + echo "prs_created=${PRS_CREATED}" + echo "pr_branch=${pr_branch}" + } >> "$GITHUB_OUTPUT" + # Guard against the root-only release deadlock. release-please cannot tag # a *combined* release PR whose body has a single, root (empty-component) # entry: it falls back to branch-name component matching, the combined @@ -106,6 +142,27 @@ jobs: || echo "::notice::Tagged ${version} but could not update labels on PR #${number}; remove 'autorelease: pending' manually if it lingers." fi + # An attempt that created the root release and failed before reporting it + # leaves only the tag behind, and the retry finds nothing left to release. + # A release made by this run tags the very commit the run was pushed for. + - name: ๐Ÿฉน Recover the root version after a failed attempt + id: recover + if: ${{ steps.attempt.outcome == 'failure' && steps.release.outputs.release_created != 'true' }} + env: + GH_TOKEN: ${{ secrets.RELEASE_TOKEN }} + run: | + set -euo pipefail + + version=$(gh api \ + "repos/${GITHUB_REPOSITORY}/contents/resources/version.json?ref=${GITHUB_SHA}" \ + -H "Accept: application/vnd.github.raw" --jq '."."') + [ -n "${version}" ] && [ "${version}" != "null" ] || exit 0 + + tagged=$(gh api "repos/${GITHUB_REPOSITORY}/git/ref/tags/${version}" --jq '.object.sha' 2>/dev/null || true) + if [ "${tagged}" = "${GITHUB_SHA}" ]; then + echo "version=${version}" >> "$GITHUB_OUTPUT" + fi + # Mirror the just-released versions into every testo/* constraint of the # release PR. Re-applied on every run, so it survives release-please # regenerating (force-pushing) the PR branch. @@ -113,7 +170,7 @@ jobs: if: ${{ steps.release.outputs.prs_created == 'true' }} uses: actions/checkout@v7 with: - ref: ${{ fromJSON(steps.release.outputs.pr).headBranchName }} + ref: ${{ steps.release.outputs.pr_branch }} token: ${{ secrets.RELEASE_TOKEN }} fetch-depth: 0 diff --git a/.github/workflows/split-publish.yml b/.github/workflows/split-publish.yml index b42026db..12bb4f19 100644 --- a/.github/workflows/split-publish.yml +++ b/.github/workflows/split-publish.yml @@ -94,17 +94,14 @@ jobs: echo "name=${branch}" >> "$GITHUB_OUTPUT" - name: ๐Ÿš€ Push subtree to php-testo/${{ steps.c.outputs.component }} - uses: symplify/monorepo-split-github-action@v2.3.0 - with: - tag: ${{ steps.c.outputs.version }} - package_directory: ${{ steps.c.outputs.directory }} - repository_organization: php-testo - repository_name: ${{ steps.c.outputs.component }} - branch: ${{ steps.branch.outputs.name }} - user_name: 'github-actions[bot]' - user_email: '41898282+github-actions[bot]@users.noreply.github.com' env: GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }} + run: >- + bash .github/release-please/split-publish.sh + "${{ steps.c.outputs.directory }}" + "php-testo/${{ steps.c.outputs.component }}" + "${{ steps.branch.outputs.name }}" + "${{ steps.c.outputs.version }}" packagist: name: ๐Ÿ”Ž Verify Packagist