|
| 1 | +name: Release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: |
| 6 | + - 'v*' |
| 7 | + workflow_dispatch: |
| 8 | + inputs: |
| 9 | + tag: |
| 10 | + description: 'Existing tag to publish (for backfill), e.g. v0.4.2' |
| 11 | + required: true |
| 12 | + type: string |
| 13 | + |
| 14 | +permissions: |
| 15 | + contents: write |
| 16 | + |
| 17 | +jobs: |
| 18 | + publish-release: |
| 19 | + runs-on: ubuntu-latest |
| 20 | + steps: |
| 21 | + - uses: actions/checkout@v4 |
| 22 | + with: |
| 23 | + fetch-depth: 0 |
| 24 | + |
| 25 | + - name: Resolve tag |
| 26 | + id: tag |
| 27 | + shell: bash |
| 28 | + run: | |
| 29 | + if [[ "${GITHUB_EVENT_NAME}" == "workflow_dispatch" ]]; then |
| 30 | + TAG="${{ inputs.tag }}" |
| 31 | + else |
| 32 | + TAG="${GITHUB_REF_NAME}" |
| 33 | + fi |
| 34 | + |
| 35 | + if [[ -z "${TAG}" ]]; then |
| 36 | + echo "Tag could not be resolved." >&2 |
| 37 | + exit 1 |
| 38 | + fi |
| 39 | + |
| 40 | + echo "value=${TAG}" >> "$GITHUB_OUTPUT" |
| 41 | + |
| 42 | + - name: Verify tag |
| 43 | + shell: bash |
| 44 | + run: | |
| 45 | + TAG="${{ steps.tag.outputs.value }}" |
| 46 | + if [[ ! "${TAG}" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 47 | + echo "Invalid tag format: ${TAG}. Expected v<major>.<minor>.<patch>" >&2 |
| 48 | + exit 1 |
| 49 | + fi |
| 50 | +
|
| 51 | + if [[ "${GITHUB_EVENT_NAME}" == "push" ]]; then |
| 52 | + PKG_VERSION="$(node -p "require('./packages/cli/package.json').version")" |
| 53 | + EXPECTED_TAG="v${PKG_VERSION}" |
| 54 | +
|
| 55 | + if [[ "${TAG}" != "${EXPECTED_TAG}" ]]; then |
| 56 | + echo "Tag/version mismatch on push: got ${TAG}, expected ${EXPECTED_TAG}" >&2 |
| 57 | + exit 1 |
| 58 | + fi |
| 59 | + else |
| 60 | + if ! git rev-parse -q --verify "refs/tags/${TAG}" >/dev/null; then |
| 61 | + echo "Tag not found in repository: ${TAG}" >&2 |
| 62 | + exit 1 |
| 63 | + fi |
| 64 | + fi |
| 65 | +
|
| 66 | + - name: Build release notes from CHANGELOG |
| 67 | + shell: bash |
| 68 | + run: | |
| 69 | + TAG="${{ steps.tag.outputs.value }}" |
| 70 | + VERSION="${TAG#v}" |
| 71 | +
|
| 72 | + awk -v version="${VERSION}" ' |
| 73 | + BEGIN { in_section=0 } |
| 74 | + $0 ~ "^## \\[" version "\\]" { in_section=1; print; next } |
| 75 | + in_section && $0 ~ "^## \\[" { exit } |
| 76 | + in_section { print } |
| 77 | + ' CHANGELOG.md > release_notes.md |
| 78 | +
|
| 79 | + if [[ ! -s release_notes.md ]]; then |
| 80 | + echo "## ${TAG}" > release_notes.md |
| 81 | + echo >> release_notes.md |
| 82 | + echo "See CHANGELOG.md for release details." >> release_notes.md |
| 83 | + fi |
| 84 | +
|
| 85 | + - name: Create or update GitHub Release |
| 86 | + uses: softprops/action-gh-release@v2 |
| 87 | + with: |
| 88 | + tag_name: ${{ steps.tag.outputs.value }} |
| 89 | + name: ${{ steps.tag.outputs.value }} |
| 90 | + body_path: release_notes.md |
| 91 | + generate_release_notes: true |
0 commit comments