|
| 1 | +name: Release |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + types: [closed] |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: write |
| 9 | + |
| 10 | +concurrency: |
| 11 | + group: release-${{ github.event.pull_request.base.ref }} |
| 12 | + cancel-in-progress: false |
| 13 | + |
| 14 | +jobs: |
| 15 | + release: |
| 16 | + if: > |
| 17 | + github.event.pull_request.merged == true && |
| 18 | + (github.event.pull_request.base.ref == 'main' || github.event.pull_request.base.ref == 'master') |
| 19 | + runs-on: ubuntu-latest |
| 20 | + |
| 21 | + steps: |
| 22 | + - name: Check out merged commit |
| 23 | + uses: actions/checkout@v4 |
| 24 | + with: |
| 25 | + ref: ${{ github.event.pull_request.merge_commit_sha }} |
| 26 | + fetch-depth: 0 |
| 27 | + |
| 28 | + - name: Fetch tags |
| 29 | + run: git fetch --force --tags origin |
| 30 | + |
| 31 | + - name: Determine CalVer tag |
| 32 | + id: calver |
| 33 | + env: |
| 34 | + MERGE_COMMIT_SHA: ${{ github.event.pull_request.merge_commit_sha }} |
| 35 | + MERGED_AT: ${{ github.event.pull_request.merged_at }} |
| 36 | + run: | |
| 37 | + set -euo pipefail |
| 38 | +
|
| 39 | + existing_tag="$( |
| 40 | + git tag --points-at "$MERGE_COMMIT_SHA" | |
| 41 | + grep -E '^v[0-9]{2}\.[0-9]{2}\.[0-9]{2}\.[0-9]+$' | |
| 42 | + sort -V | |
| 43 | + tail -n 1 || true |
| 44 | + )" |
| 45 | +
|
| 46 | + if [ -n "$existing_tag" ]; then |
| 47 | + tag="$existing_tag" |
| 48 | + else |
| 49 | + base="$(date -u -d "$MERGED_AT" +'%y.%m.%d')" |
| 50 | + max_suffix=0 |
| 51 | +
|
| 52 | + while IFS= read -r existing; do |
| 53 | + suffix="${existing#v$base.}" |
| 54 | + if ! printf '%s' "$suffix" | grep -Eq '^[0-9]+$'; then |
| 55 | + continue |
| 56 | + fi |
| 57 | +
|
| 58 | + if [ "$suffix" -gt "$max_suffix" ]; then |
| 59 | + max_suffix="$suffix" |
| 60 | + fi |
| 61 | + done < <(git tag -l "v$base.*" | sort -V) |
| 62 | +
|
| 63 | + tag="v$base.$((max_suffix + 1))" |
| 64 | + fi |
| 65 | +
|
| 66 | + echo "tag=$tag" >> "$GITHUB_OUTPUT" |
| 67 | +
|
| 68 | + - name: Create and push tag |
| 69 | + env: |
| 70 | + MERGE_COMMIT_SHA: ${{ github.event.pull_request.merge_commit_sha }} |
| 71 | + TAG: ${{ steps.calver.outputs.tag }} |
| 72 | + run: | |
| 73 | + set -euo pipefail |
| 74 | +
|
| 75 | + if git ls-remote --exit-code --tags origin "refs/tags/$TAG" >/dev/null 2>&1; then |
| 76 | + remote_sha="$(git rev-list -n 1 "$TAG")" |
| 77 | + if [ "$remote_sha" = "$MERGE_COMMIT_SHA" ]; then |
| 78 | + echo "Tag $TAG already exists on origin and points to the expected commit" |
| 79 | + exit 0 |
| 80 | + fi |
| 81 | +
|
| 82 | + echo "Error: Tag $TAG already exists on origin but points to $remote_sha instead of $MERGE_COMMIT_SHA" |
| 83 | + exit 1 |
| 84 | + fi |
| 85 | +
|
| 86 | + git config user.name "github-actions[bot]" |
| 87 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 88 | + git tag -a "$TAG" "$MERGE_COMMIT_SHA" -m "Release $TAG" |
| 89 | + git push origin "refs/tags/$TAG" |
| 90 | +
|
| 91 | + - name: Create GitHub release |
| 92 | + env: |
| 93 | + GH_TOKEN: ${{ github.token }} |
| 94 | + MERGE_COMMIT_SHA: ${{ github.event.pull_request.merge_commit_sha }} |
| 95 | + TAG: ${{ steps.calver.outputs.tag }} |
| 96 | + run: | |
| 97 | + set -euo pipefail |
| 98 | +
|
| 99 | + if gh release view "$TAG" >/dev/null 2>&1; then |
| 100 | + echo "Release $TAG already exists" |
| 101 | + exit 0 |
| 102 | + fi |
| 103 | +
|
| 104 | + gh release create "$TAG" \ |
| 105 | + --target "$MERGE_COMMIT_SHA" \ |
| 106 | + --generate-notes \ |
| 107 | + --title "$TAG" |
0 commit comments