|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# Steps 6-8 of docs/releases.md, run from the tag instead of by hand. |
| 3 | +name: Release |
| 4 | + |
| 5 | +on: |
| 6 | + push: |
| 7 | + tags: ["v*"] |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: write |
| 11 | + packages: write |
| 12 | + |
| 13 | +jobs: |
| 14 | + verify: |
| 15 | + name: Verify release source |
| 16 | + runs-on: ubuntu-latest |
| 17 | + env: |
| 18 | + BOT_TOKEN: "123456:CITOKEN1234567890" |
| 19 | + BOT_USERNAME: quoto_ci_bot |
| 20 | + steps: |
| 21 | + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
| 22 | + with: |
| 23 | + fetch-depth: 0 |
| 24 | + |
| 25 | + - name: Require the tag commit on main |
| 26 | + # docs/versioning.md: every release is published from main. A tag on a |
| 27 | + # branch that was never merged would publish an image nobody reviewed. |
| 28 | + run: | |
| 29 | + git fetch --no-tags origin main:refs/remotes/origin/main |
| 30 | + if ! git merge-base --is-ancestor "${GITHUB_SHA}" origin/main; then |
| 31 | + echo "tag ${GITHUB_REF_NAME} is not an ancestor of origin/main" |
| 32 | + exit 1 |
| 33 | + fi |
| 34 | +
|
| 35 | + - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0 |
| 36 | + with: |
| 37 | + python-version: "3.12" |
| 38 | + cache: pip |
| 39 | + cache-dependency-path: requirements.txt |
| 40 | + |
| 41 | + - name: Install dependencies |
| 42 | + run: | |
| 43 | + python -m pip install --upgrade pip |
| 44 | + pip install -r requirements.txt |
| 45 | +
|
| 46 | + - name: Test |
| 47 | + run: python -m pytest -q tests |
| 48 | + |
| 49 | + - name: Require a changelog section for this tag |
| 50 | + run: | |
| 51 | + grep -qE "^## ${GITHUB_REF_NAME}( |$)" CHANGELOG.md \ |
| 52 | + || { echo "CHANGELOG.md has no '## ${GITHUB_REF_NAME}' section"; exit 1; } |
| 53 | +
|
| 54 | + release: |
| 55 | + name: Publish image and release |
| 56 | + needs: verify |
| 57 | + runs-on: ubuntu-latest |
| 58 | + steps: |
| 59 | + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
| 60 | + |
| 61 | + - name: Resolve release metadata |
| 62 | + id: meta |
| 63 | + run: | |
| 64 | + version="${GITHUB_REF_NAME}" |
| 65 | + echo "version=${version}" >>"$GITHUB_OUTPUT" |
| 66 | + echo "date=$(date -u +%Y-%m-%dT%H:%M:%SZ)" >>"$GITHUB_OUTPUT" |
| 67 | + # A pre-release suffix (-alpha/-beta/-rc) is never tagged "latest". |
| 68 | + if printf '%s' "${version}" | grep -Eq -- '-(alpha|beta|rc)\.'; then |
| 69 | + echo "prerelease=true" >>"$GITHUB_OUTPUT" |
| 70 | + else |
| 71 | + echo "prerelease=false" >>"$GITHUB_OUTPUT" |
| 72 | + fi |
| 73 | +
|
| 74 | + - name: Lowercase image name |
| 75 | + id: image |
| 76 | + run: echo "repo=ghcr.io/${GITHUB_REPOSITORY,,}" >>"$GITHUB_OUTPUT" |
| 77 | + |
| 78 | + - name: Extract changelog section |
| 79 | + id: changelog |
| 80 | + run: | |
| 81 | + version="${GITHUB_REF_NAME}" |
| 82 | + notes_file="$(mktemp)" |
| 83 | + # Match by literal prefix AND a boundary, so "## v0.10.3" does not |
| 84 | + # capture "## v0.10.3-alpha.1". |
| 85 | + awk -v ver="## ${version}" ' |
| 86 | + index($0, ver) == 1 && (length($0) == length(ver) || substr($0, length(ver) + 1, 1) == " ") { capture=1; next } |
| 87 | + capture && /^## / { exit } |
| 88 | + capture { print } |
| 89 | + ' CHANGELOG.md >"${notes_file}" |
| 90 | + if [ ! -s "${notes_file}" ]; then |
| 91 | + printf 'Release %s\n' "${version}" >"${notes_file}" |
| 92 | + fi |
| 93 | + echo "file=${notes_file}" >>"$GITHUB_OUTPUT" |
| 94 | +
|
| 95 | + - name: Log in to GitHub Container Registry |
| 96 | + uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4.6.0 |
| 97 | + with: |
| 98 | + registry: ghcr.io |
| 99 | + username: ${{ github.actor }} |
| 100 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 101 | + |
| 102 | + - name: Set up Buildx |
| 103 | + uses: docker/setup-buildx-action@37fe631027851001ddb9b187196cc803df7f5f0e # v4.3.0 |
| 104 | + |
| 105 | + - name: Build and push image |
| 106 | + uses: docker/build-push-action@53b7df96c91f9c12dcc8a07bcb9ccacbed38856a # v7.3.0 |
| 107 | + with: |
| 108 | + context: . |
| 109 | + push: true |
| 110 | + tags: ${{ steps.image.outputs.repo }}:${{ steps.meta.outputs.version }} |
| 111 | + build-args: | |
| 112 | + QUOTO_VERSION=${{ steps.meta.outputs.version }} |
| 113 | + QUOTO_REVISION=${{ github.sha }} |
| 114 | +
|
| 115 | + - name: Create GitHub Release |
| 116 | + env: |
| 117 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 118 | + run: | |
| 119 | + flags="" |
| 120 | + if [ "${{ steps.meta.outputs.prerelease }}" = "true" ]; then |
| 121 | + flags="--prerelease" |
| 122 | + fi |
| 123 | + # Idempotent: update an existing release (manual, or a re-run), |
| 124 | + # otherwise create it. |
| 125 | + if gh release view "${GITHUB_REF_NAME}" >/dev/null 2>&1; then |
| 126 | + gh release edit "${GITHUB_REF_NAME}" \ |
| 127 | + --title "${GITHUB_REF_NAME}" \ |
| 128 | + --notes-file "${{ steps.changelog.outputs.file }}" ${flags} |
| 129 | + else |
| 130 | + gh release create "${GITHUB_REF_NAME}" \ |
| 131 | + --title "${GITHUB_REF_NAME}" \ |
| 132 | + --notes-file "${{ steps.changelog.outputs.file }}" ${flags} |
| 133 | + fi |
0 commit comments