|
5 | 5 | tags: |
6 | 6 | - "v*" |
7 | 7 | workflow_dispatch: |
| 8 | + inputs: |
| 9 | + bump: |
| 10 | + description: "Semantic version bump" |
| 11 | + required: true |
| 12 | + default: patch |
| 13 | + type: choice |
| 14 | + options: |
| 15 | + - patch |
| 16 | + - minor |
| 17 | + - major |
8 | 18 |
|
9 | 19 | concurrency: |
10 | 20 | group: release-${{ github.ref }} |
@@ -57,12 +67,89 @@ jobs: |
57 | 67 | GOWORK: off |
58 | 68 | run: go test ./... |
59 | 69 |
|
| 70 | + tag-release: |
| 71 | + name: Create release tag |
| 72 | + runs-on: ubuntu-latest |
| 73 | + needs: verify |
| 74 | + if: github.event_name == 'workflow_dispatch' |
| 75 | + outputs: |
| 76 | + tag_name: ${{ steps.tag.outputs.tag_name }} |
| 77 | + steps: |
| 78 | + - name: Checkout |
| 79 | + uses: actions/checkout@v4 |
| 80 | + with: |
| 81 | + fetch-depth: 0 |
| 82 | + |
| 83 | + - name: Configure git |
| 84 | + run: | |
| 85 | + git config user.name "github-actions[bot]" |
| 86 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 87 | +
|
| 88 | + - name: Create and push tag |
| 89 | + id: tag |
| 90 | + env: |
| 91 | + BUMP: ${{ inputs.bump }} |
| 92 | + run: | |
| 93 | + set -euo pipefail |
| 94 | +
|
| 95 | + latest_tag="$(git tag --list 'v[0-9]*.[0-9]*.[0-9]*' --sort=-version:refname | head -n 1)" |
| 96 | + if [ -z "$latest_tag" ]; then |
| 97 | + latest_tag="v0.0.0" |
| 98 | + fi |
| 99 | +
|
| 100 | + version="${latest_tag#v}" |
| 101 | + IFS='.' read -r major minor patch <<< "$version" |
| 102 | + if ! [[ "$major" =~ ^[0-9]+$ && "$minor" =~ ^[0-9]+$ && "$patch" =~ ^[0-9]+$ ]]; then |
| 103 | + echo "could not parse semver from '$latest_tag'" >&2 |
| 104 | + exit 1 |
| 105 | + fi |
| 106 | +
|
| 107 | + case "$BUMP" in |
| 108 | + major) |
| 109 | + major=$((major + 1)) |
| 110 | + minor=0 |
| 111 | + patch=0 |
| 112 | + ;; |
| 113 | + minor) |
| 114 | + minor=$((minor + 1)) |
| 115 | + patch=0 |
| 116 | + ;; |
| 117 | + patch) |
| 118 | + patch=$((patch + 1)) |
| 119 | + ;; |
| 120 | + *) |
| 121 | + echo "unsupported bump: $BUMP" >&2 |
| 122 | + exit 1 |
| 123 | + ;; |
| 124 | + esac |
| 125 | +
|
| 126 | + tag_name="v${major}.${minor}.${patch}" |
| 127 | + if git rev-parse -q --verify "refs/tags/$tag_name" >/dev/null; then |
| 128 | + echo "tag $tag_name already exists locally" >&2 |
| 129 | + exit 1 |
| 130 | + fi |
| 131 | + if git ls-remote --exit-code --tags origin "refs/tags/$tag_name" >/dev/null 2>&1; then |
| 132 | + echo "tag $tag_name already exists on origin" >&2 |
| 133 | + exit 1 |
| 134 | + fi |
| 135 | + git tag "$tag_name" |
| 136 | + git push origin "$tag_name" |
| 137 | + echo "tag_name=$tag_name" >> "$GITHUB_OUTPUT" |
| 138 | +
|
60 | 139 | github-release: |
61 | 140 | name: Publish GitHub release |
62 | 141 | runs-on: ubuntu-latest |
63 | | - needs: verify |
| 142 | + needs: |
| 143 | + - verify |
| 144 | + - tag-release |
| 145 | + if: | |
| 146 | + !failure() && !cancelled() |
| 147 | + && needs.verify.result == 'success' |
| 148 | + && (needs.tag-release.result == 'success' || needs.tag-release.result == 'skipped') |
| 149 | + && (startsWith(github.ref, 'refs/tags/v') || github.event_name == 'workflow_dispatch') |
64 | 150 | steps: |
65 | 151 | - name: Create GitHub release |
66 | 152 | uses: softprops/action-gh-release@v2 |
67 | 153 | with: |
| 154 | + tag_name: ${{ github.event_name == 'workflow_dispatch' && needs.tag-release.outputs.tag_name || github.ref_name }} |
68 | 155 | generate_release_notes: true |
0 commit comments