yams/.github/workflows/release-please.yml

136 lines
5.6 KiB
YAML

# yamllint disable rule:line-length
name: Release Please
'on':
push:
branches:
- main
workflow_dispatch: {}
permissions:
actions: write
contents: write
pull-requests: write
jobs:
release-please:
runs-on: ubuntu-latest
outputs:
release_created: ${{ steps.rp.outputs.release_created }}
tag_name: ${{ steps.rp.outputs.tag_name }}
major: ${{ steps.rp.outputs.major }}
minor: ${{ steps.rp.outputs.minor }}
patch: ${{ steps.rp.outputs.patch }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Align manifest with latest published stable release
env:
GH_TOKEN: ${{ secrets.RELEASE_PLEASE_TOKEN || secrets.GITHUB_TOKEN }}
shell: bash
run: |
set -euo pipefail
manifest_version="$(sed -n 's/^[[:space:]]*"\."[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' .release-please-manifest.json | head -n 1)"
if [ -z "${manifest_version}" ]; then
echo "ERROR: Could not read current version from .release-please-manifest.json" >&2
exit 1
fi
latest_tag="$(gh api "repos/${GITHUB_REPOSITORY}/releases?per_page=100" --jq 'first(.[] | select((.draft | not) and (.prerelease | not) and (.tag_name | startswith("v"))) | .tag_name) // ""')"
if [ -z "${latest_tag}" ]; then
echo "No published stable release found; leaving manifest at ${manifest_version}."
exit 0
fi
latest_version="${latest_tag#v}"
highest_version="$(printf '%s\n%s\n' "${manifest_version}" "${latest_version}" | sort -V | tail -n 1)"
if [ "${highest_version}" != "${latest_version}" ] || [ "${manifest_version}" = "${latest_version}" ]; then
echo "Manifest already current enough for release-please (manifest=${manifest_version}, latest_published=${latest_version})."
exit 0
fi
echo "Manifest is behind latest published stable release; syncing ${manifest_version} -> ${latest_version} before release-please runs."
printf '{\n ".": "%s"\n}\n' "${latest_version}" > .release-please-manifest.json
- uses: googleapis/release-please-action@v4
id: rp
with:
token: ${{ secrets.RELEASE_PLEASE_TOKEN || secrets.GITHUB_TOKEN }}
config-file: release-please-config.json
manifest-file: .release-please-manifest.json
# When a release is created, the v* tag triggers the existing
# release.yml workflow which handles the full build matrix,
# packaging, and artifact upload. Nothing else to do here.
- name: Release info
if: steps.rp.outputs.release_created == 'true'
run: |
echo "Release ${{ steps.rp.outputs.tag_name }} created."
echo "The existing release.yml workflow will handle build & packaging."
- name: Ensure Release workflow run exists
if: steps.rp.outputs.release_created == 'true'
env:
GH_TOKEN: ${{ secrets.RELEASE_PLEASE_TOKEN || secrets.GITHUB_TOKEN }}
HAS_RELEASE_PLEASE_TOKEN: ${{ secrets.RELEASE_PLEASE_TOKEN != '' }}
TAG_NAME: ${{ steps.rp.outputs.tag_name }}
CURRENT_RUN_ID: ${{ github.run_id }}
shell: bash
run: |
set -euo pipefail
if [ -z "${TAG_NAME:-}" ]; then
echo "ERROR: release-please reported release_created=true but tag_name is empty" >&2
exit 1
fi
tag_sha=""
for attempt in 1 2 3 4 5 6 7 8 9 10 11 12; do
if tag_sha="$(gh api "repos/${GITHUB_REPOSITORY}/git/ref/tags/${TAG_NAME}" --jq '.object.sha' 2>/dev/null)"; then
tag_type="$(gh api "repos/${GITHUB_REPOSITORY}/git/ref/tags/${TAG_NAME}" --jq '.object.type' 2>/dev/null)"
if [ "$tag_type" = "tag" ]; then
tag_sha="$(gh api "repos/${GITHUB_REPOSITORY}/git/tags/${tag_sha}" --jq '.object.sha' 2>/dev/null)"
fi
echo "Resolved release tag ${TAG_NAME} -> ${tag_sha}"
break
fi
echo "Release tag ${TAG_NAME} not visible yet (attempt ${attempt}/12); waiting 5s..." >&2
sleep 5
done
if [ -z "$tag_sha" ]; then
echo "ERROR: release-please reported release_created=true but tag ${TAG_NAME} never became visible" >&2
exit 1
fi
if [ "${HAS_RELEASE_PLEASE_TOKEN}" = "true" ]; then
release_url=""
for attempt in 1 2 3 4 5 6 7 8 9 10 11 12; do
release_url="$(gh api "repos/${GITHUB_REPOSITORY}/actions/workflows/release.yml/runs?event=push&per_page=50" \
--jq "first(.workflow_runs[] | select((.id | tostring) != \"${CURRENT_RUN_ID}\") | select(.head_sha == \"${tag_sha}\") | .html_url) // \"\"" \
2>/dev/null || true)"
if [ -n "$release_url" ]; then
echo "Detected Release workflow run for ${TAG_NAME}: ${release_url}"
break
fi
echo "No tag-push Release run detected yet for ${TAG_NAME} (attempt ${attempt}/12); waiting 5s..." >&2
sleep 5
done
if [ -n "$release_url" ]; then
echo "Release workflow already triggered by tag push; fallback dispatch not needed."
exit 0
fi
else
echo "RELEASE_PLEASE_TOKEN not set; tag pushes from GITHUB_TOKEN will not trigger Release automatically." >&2
fi
echo "Dispatching fallback Release workflow for ${TAG_NAME}."
gh workflow run "release.yml" --repo "${GITHUB_REPOSITORY}" --ref "${TAG_NAME}" -f channel=stable