|
| 1 | +# Release |
| 2 | +# |
| 3 | +# Open or update the release pull request, test the package, and publish the |
| 4 | +# GitHub Release. |
| 5 | +# |
| 6 | +# Adapted from swift6-module-template release 16.5.0: |
| 7 | +# https://github.com/fulldecent/swift6-module-template/blob/v16.5.0/.github/workflows/release.yml |
| 8 | +# That workflow attests a Linux static library from `swift build -c release`. |
| 9 | +# This package imports UIKit, so there is no Linux library to publish. Swift |
| 10 | +# Package Manager installs from the git tag. The published release is the tag |
| 11 | +# and the generated notes. |
| 12 | +# |
| 13 | +# Tags stay `5.1.1`, without a `v` prefix, because that is the shape of the |
| 14 | +# existing tags. release-please-config.json sets include-v-in-tag to false. |
| 15 | +# |
| 16 | +# One-time setup |
| 17 | +# 1. GitHub repo > Settings > General > Releases > Enable release immutability. |
| 18 | +# 2. GitHub repo > Settings > Actions > General > Workflow permissions: Read and write permissions. |
| 19 | +# 3. Under Workflow permissions, check "Allow GitHub Actions to create and approve pull requests". |
| 20 | + |
| 21 | +name: Release |
| 22 | + |
| 23 | +on: |
| 24 | + push: |
| 25 | + branches: ["main"] |
| 26 | + workflow_dispatch: |
| 27 | + inputs: |
| 28 | + tag: |
| 29 | + description: Tag to publish, for example 5.1.2. Leave empty to open or update the release pull request only. |
| 30 | + required: false |
| 31 | + type: string |
| 32 | + |
| 33 | +permissions: |
| 34 | + contents: write |
| 35 | + issues: write |
| 36 | + pull-requests: write |
| 37 | + |
| 38 | +jobs: |
| 39 | + propose: |
| 40 | + name: Open or update the release pull request |
| 41 | + if: github.event_name == 'push' || inputs.tag == '' |
| 42 | + runs-on: ubuntu-latest |
| 43 | + outputs: |
| 44 | + pr: ${{ steps.merged.outputs.pr }} |
| 45 | + tag_name: ${{ steps.merged.outputs.tag_name }} |
| 46 | + steps: |
| 47 | + - name: Open the release pull request |
| 48 | + uses: googleapis/release-please-action@v5 |
| 49 | + with: |
| 50 | + config-file: release-please-config.json |
| 51 | + manifest-file: .release-please-manifest.json |
| 52 | + skip-github-release: true |
| 53 | + |
| 54 | + - name: Find a merged release pull request |
| 55 | + id: merged |
| 56 | + if: github.event_name == 'push' |
| 57 | + env: |
| 58 | + GH_TOKEN: ${{ github.token }} |
| 59 | + run: | |
| 60 | + found=$(gh api "repos/$GITHUB_REPOSITORY/commits/$GITHUB_SHA/pulls" --jq ' |
| 61 | + .[] | select(.merged_at != null and any(.labels[]; .name == "autorelease: pending")) |
| 62 | + | "\(.number) \(.title | capture("release (?<v>[0-9]+[.][0-9]+[.][0-9]+)").v)"' | head -n 1) |
| 63 | + if [ -n "$found" ]; then |
| 64 | + read -r pr tag <<< "$found" |
| 65 | + echo "pr=$pr" >> "$GITHUB_OUTPUT" |
| 66 | + echo "tag_name=$tag" >> "$GITHUB_OUTPUT" |
| 67 | + fi |
| 68 | +
|
| 69 | + build-test: |
| 70 | + name: Build and test |
| 71 | + needs: propose |
| 72 | + if: | |
| 73 | + always() && |
| 74 | + (needs.propose.result == 'success' || needs.propose.result == 'skipped') && |
| 75 | + (needs.propose.outputs.tag_name != '' || |
| 76 | + (github.event_name == 'workflow_dispatch' && inputs.tag != '')) |
| 77 | + uses: ./.github/workflows/ci.yml |
| 78 | + |
| 79 | + publish: |
| 80 | + name: Publish release |
| 81 | + needs: [propose, build-test] |
| 82 | + if: | |
| 83 | + always() && |
| 84 | + needs.build-test.result == 'success' && |
| 85 | + (needs.propose.outputs.tag_name != '' || |
| 86 | + (github.event_name == 'workflow_dispatch' && inputs.tag != '')) |
| 87 | + runs-on: ubuntu-latest |
| 88 | + permissions: |
| 89 | + contents: write |
| 90 | + pull-requests: write |
| 91 | + steps: |
| 92 | + - name: Resolve the tag |
| 93 | + id: tag |
| 94 | + env: |
| 95 | + TAG: ${{ inputs.tag || needs.propose.outputs.tag_name }} |
| 96 | + run: | |
| 97 | + case "$TAG" in |
| 98 | + [0-9]*.[0-9]*.[0-9]*) ;; |
| 99 | + *) echo "tag must look like 5.1.2" >&2; exit 1 ;; |
| 100 | + esac |
| 101 | + echo "tag=$TAG" >> "$GITHUB_OUTPUT" |
| 102 | +
|
| 103 | + - name: Publish |
| 104 | + env: |
| 105 | + GH_TOKEN: ${{ github.token }} |
| 106 | + TAG: ${{ steps.tag.outputs.tag }} |
| 107 | + run: | |
| 108 | + gh release create "$TAG" \ |
| 109 | + --repo "$GITHUB_REPOSITORY" \ |
| 110 | + --target "$GITHUB_SHA" \ |
| 111 | + --title "$TAG" \ |
| 112 | + --generate-notes \ |
| 113 | + --latest |
| 114 | +
|
| 115 | + - name: Mark the release pull request tagged |
| 116 | + if: needs.propose.outputs.pr != '' |
| 117 | + env: |
| 118 | + GH_TOKEN: ${{ github.token }} |
| 119 | + PR: ${{ needs.propose.outputs.pr }} |
| 120 | + run: | |
| 121 | + gh api -X DELETE "repos/$GITHUB_REPOSITORY/issues/$PR/labels/autorelease:%20pending" |
| 122 | + gh api "repos/$GITHUB_REPOSITORY/issues/$PR/labels" -f "labels[]=autorelease: tagged" |
0 commit comments