|
| 1 | +name: Release |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + push: |
| 6 | + branches: |
| 7 | + - main |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: write |
| 11 | + |
| 12 | +jobs: |
| 13 | + release: |
| 14 | + if: startsWith(github.event.head_commit.message || '', 'RELEASE:') || startsWith(github.event.head_commit.message || '', 'release:') |
| 15 | + runs-on: ubuntu-latest |
| 16 | + steps: |
| 17 | + - name: Checkout code |
| 18 | + uses: actions/checkout@v4 |
| 19 | + with: |
| 20 | + fetch-depth: 0 |
| 21 | + |
| 22 | + - name: Extract tag from commit message |
| 23 | + id: extract_tag |
| 24 | + run: | |
| 25 | + COMMIT_MSG="${{ github.event.head_commit.message }}" |
| 26 | + FIRST_LINE="$(echo "$COMMIT_MSG" | head -n 1)" |
| 27 | + TAG="$(echo "$FIRST_LINE" | sed -E 's/^[Rr][Ee][Ll][Ee][Aa][Ss][Ee]: *//')" |
| 28 | +
|
| 29 | + if [ -z "$TAG" ]; then |
| 30 | + echo "Error: no tag found in commit message" |
| 31 | + exit 1 |
| 32 | + fi |
| 33 | +
|
| 34 | + if ! [[ "$TAG" =~ ^v?[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.]+)?$ ]]; then |
| 35 | + echo "Error: tag must be in format v0.0.0, v0.0.0-alpha, or v0.0.0-rc.1" |
| 36 | + exit 1 |
| 37 | + fi |
| 38 | +
|
| 39 | + if [[ ! "$TAG" =~ ^v ]]; then |
| 40 | + TAG="v$TAG" |
| 41 | + fi |
| 42 | +
|
| 43 | + echo "tag=$TAG" >> "$GITHUB_OUTPUT" |
| 44 | + echo "Creating release for tag: $TAG" |
| 45 | +
|
| 46 | + - name: Check if tag exists |
| 47 | + run: | |
| 48 | + TAG="${{ steps.extract_tag.outputs.tag }}" |
| 49 | + if git ls-remote --tags origin "refs/tags/${TAG}" | grep -q .; then |
| 50 | + echo "Error: tag ${TAG} already exists" |
| 51 | + exit 1 |
| 52 | + fi |
| 53 | +
|
| 54 | + - name: Create and push tag |
| 55 | + run: | |
| 56 | + TAG="${{ steps.extract_tag.outputs.tag }}" |
| 57 | + git config user.name "github-actions[bot]" |
| 58 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 59 | + git tag "$TAG" |
| 60 | + git push origin "$TAG" |
| 61 | +
|
| 62 | + - name: Get previous tag |
| 63 | + id: get_previous_tag |
| 64 | + run: | |
| 65 | + TAG="${{ steps.extract_tag.outputs.tag }}" |
| 66 | + PREVIOUS_TAG="$(git describe --tags --abbrev=0 "${TAG}^" 2>/dev/null || true)" |
| 67 | + echo "previous_tag=$PREVIOUS_TAG" >> "$GITHUB_OUTPUT" |
| 68 | + echo "Previous tag: $PREVIOUS_TAG" |
| 69 | +
|
| 70 | + - name: Generate release notes |
| 71 | + run: | |
| 72 | + TAG="${{ steps.extract_tag.outputs.tag }}" |
| 73 | + PREVIOUS_TAG="${{ steps.get_previous_tag.outputs.previous_tag }}" |
| 74 | +
|
| 75 | + if [ -n "$PREVIOUS_TAG" ]; then |
| 76 | + COMMITS="$(git log --pretty=format:'- %s (%h)' "${PREVIOUS_TAG}..${TAG}" 2>/dev/null || git log --pretty=format:'- %s (%h)' -20)" |
| 77 | + CHANGELOG_LINE="**Full Changelog**: ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/compare/${PREVIOUS_TAG}...${TAG}" |
| 78 | + else |
| 79 | + COMMITS="$(git log --pretty=format:'- %s (%h)' -20)" |
| 80 | + CHANGELOG_LINE="**Full Changelog**: ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}" |
| 81 | + fi |
| 82 | +
|
| 83 | + NOTES="## What's Changed |
| 84 | +
|
| 85 | + ${COMMITS} |
| 86 | +
|
| 87 | + ${CHANGELOG_LINE}" |
| 88 | +
|
| 89 | + echo "$NOTES" > release_notes.md |
| 90 | + echo "Generated release notes" |
| 91 | +
|
| 92 | + - name: Create GitHub release |
| 93 | + env: |
| 94 | + GITHUB_TOKEN: ${{ github.token }} |
| 95 | + run: | |
| 96 | + TAG="${{ steps.extract_tag.outputs.tag }}" |
| 97 | + NOTES="$(cat release_notes.md)" |
| 98 | + NOTES_ESCAPED="$(printf '%s' "$NOTES" | python3 -c 'import json,sys; print(json.dumps(sys.stdin.read()))')" |
| 99 | +
|
| 100 | + if [[ "$TAG" == *-* ]]; then |
| 101 | + PRERELEASE=true |
| 102 | + else |
| 103 | + PRERELEASE=false |
| 104 | + fi |
| 105 | +
|
| 106 | + curl --fail-with-body -X POST \ |
| 107 | + -H "Authorization: Bearer ${GITHUB_TOKEN}" \ |
| 108 | + -H "Accept: application/vnd.github+json" \ |
| 109 | + -H "Content-Type: application/json" \ |
| 110 | + "${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/releases" \ |
| 111 | + -d "{ |
| 112 | + \"tag_name\": \"${TAG}\", |
| 113 | + \"name\": \"${TAG}\", |
| 114 | + \"body\": ${NOTES_ESCAPED}, |
| 115 | + \"draft\": false, |
| 116 | + \"prerelease\": ${PRERELEASE} |
| 117 | + }" |
0 commit comments