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