-
Notifications
You must be signed in to change notification settings - Fork 3
218 lines (187 loc) · 7.89 KB
/
Copy pathcreate-release-note.yml
File metadata and controls
218 lines (187 loc) · 7.89 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
name: Create production release
on:
status:
permissions:
contents: write
pull-requests: read
concurrency:
group: production-release-${{ github.event.sha }}-${{ github.event.context }}-${{ github.event.state }}
cancel-in-progress: false
jobs:
create-release:
if: >-
github.event.context == 'Vercel' &&
github.event.state == 'success' &&
github.event.sender.login == 'vercel[bot]' &&
startsWith(github.event.target_url, 'https://vercel.com/hm1n/nbread/')
runs-on: ubuntu-latest
env:
EVENT_SHA: ${{ github.event.sha }}
GH_TOKEN: ${{ github.token }}
steps:
- name: Checkout main
uses: actions/checkout@v6
with:
fetch-depth: 0
ref: main
- name: Verify latest main deployment
id: main
shell: bash
run: |
set -euo pipefail
git fetch origin main --tags
MAIN_SHA="$(git rev-parse origin/main)"
if [[ "$MAIN_SHA" != "$EVENT_SHA" ]]; then
echo "현재 main 최신 커밋의 배포가 아니므로 릴리즈를 생성하지 않습니다."
echo "event_sha=$EVENT_SHA"
echo "main_sha=$MAIN_SHA"
echo "eligible=false" >> "$GITHUB_OUTPUT"
exit 0
fi
echo "Vercel 성공 상태가 현재 main 최신 커밋과 일치합니다: $EVENT_SHA"
echo "eligible=true" >> "$GITHUB_OUTPUT"
- name: Resolve release pull request
if: steps.main.outputs.eligible == 'true'
id: release
shell: bash
run: |
set -euo pipefail
PULLS_JSON="$(
gh api \
-H "Accept: application/vnd.github+json" \
"repos/${GITHUB_REPOSITORY}/commits/${EVENT_SHA}/pulls?per_page=100"
)"
MATCHING_PULLS="$(
jq \
--arg repository "$GITHUB_REPOSITORY" \
'[
.[]
| select(
.merged_at != null
and .base.ref == "main"
and .head.ref == "develop"
and .head.repo.full_name == $repository
and (.title | test("^\\[release\\] v[0-9]+\\.[0-9]+\\.[0-9]+$"))
)
]' \
<<< "$PULLS_JSON"
)"
PULL_COUNT="$(jq 'length' <<< "$MATCHING_PULLS")"
if [[ "$PULL_COUNT" -ne 1 ]]; then
echo "배포 SHA에 연결된 유효한 릴리즈 PR은 정확히 1개여야 합니다. 발견: $PULL_COUNT" >&2
jq -r '.[] | "- #\(.number) \(.title) (\(.head.ref) -> \(.base.ref))"' <<< "$MATCHING_PULLS" >&2
exit 1
fi
PR_NUMBER="$(jq -r '.[0].number' <<< "$MATCHING_PULLS")"
PR_TITLE="$(jq -r '.[0].title' <<< "$MATCHING_PULLS")"
PR_BODY="$(jq -r '.[0].body // ""' <<< "$MATCHING_PULLS")"
if [[ "$PR_TITLE" =~ ^\[release\]\ (v[0-9]+\.[0-9]+\.[0-9]+)$ ]]; then
VERSION="${BASH_REMATCH[1]}"
else
echo "릴리즈 PR 제목 형식이 올바르지 않습니다: $PR_TITLE" >&2
exit 1
fi
BODY_VERSION="$(
awk '
/^## 릴리즈 버전[[:space:]]*$/ { in_version_section = 1; next }
in_version_section && /^## / { exit }
in_version_section && match($0, /`v[0-9]+\.[0-9]+\.[0-9]+`/) {
print substr($0, RSTART + 1, RLENGTH - 2)
exit
}
' <<< "$PR_BODY"
)"
if [[ "$BODY_VERSION" != "$VERSION" ]]; then
echo "릴리즈 PR 제목과 본문의 버전이 일치하지 않습니다." >&2
echo "title_version=$VERSION" >&2
echo "body_version=${BODY_VERSION:-missing}" >&2
exit 1
fi
START_COUNT="$(grep -cF '<!-- release-note:start -->' <<< "$PR_BODY" || true)"
END_COUNT="$(grep -cF '<!-- release-note:end -->' <<< "$PR_BODY" || true)"
if [[ "$START_COUNT" -ne 1 || "$END_COUNT" -ne 1 ]]; then
echo "릴리즈 노트 시작/종료 마커가 각각 정확히 1개 필요합니다." >&2
echo "start_count=$START_COUNT" >&2
echo "end_count=$END_COUNT" >&2
exit 1
fi
START_LINE="$(grep -nF '<!-- release-note:start -->' <<< "$PR_BODY" | cut -d: -f1)"
END_LINE="$(grep -nF '<!-- release-note:end -->' <<< "$PR_BODY" | cut -d: -f1)"
if [[ "$START_LINE" -ge "$END_LINE" ]]; then
echo "릴리즈 노트 마커의 순서가 올바르지 않습니다." >&2
exit 1
fi
if [[ "$END_LINE" -le "$((START_LINE + 1))" ]]; then
echo "릴리즈 노트 마커 사이에 본문이 없습니다." >&2
exit 1
fi
sed -n "$((START_LINE + 1)),$((END_LINE - 1))p" <<< "$PR_BODY" > release-notes.md
if ! grep -q '[^[:space:]]' release-notes.md; then
echo "릴리즈 PR에서 추출한 릴리즈 노트에 비공백 내용이 없습니다." >&2
exit 1
fi
echo "릴리즈 PR을 확인했습니다: #$PR_NUMBER $PR_TITLE"
echo "pr_number=$PR_NUMBER" >> "$GITHUB_OUTPUT"
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
- name: Create or recover production release
if: steps.main.outputs.eligible == 'true'
shell: bash
env:
PR_NUMBER: ${{ steps.release.outputs.pr_number }}
VERSION: ${{ steps.release.outputs.version }}
run: |
set -euo pipefail
TAG_REF="$(git ls-remote --tags origin "refs/tags/${VERSION}")"
if [[ -n "$TAG_REF" ]]; then
TAG_EXISTS=true
else
TAG_EXISTS=false
fi
if gh api --silent "repos/${GITHUB_REPOSITORY}/releases/tags/${VERSION}" 2> release-error.txt; then
RELEASE_EXISTS=true
elif grep -q 'HTTP 404' release-error.txt; then
RELEASE_EXISTS=false
else
echo "GitHub Release 존재 여부를 확인하지 못했습니다." >&2
cat release-error.txt >&2
exit 1
fi
if [[ "$TAG_EXISTS" == true ]]; then
git fetch origin "refs/tags/${VERSION}:refs/tags/${VERSION}"
TAG_SHA="$(git rev-list -n 1 "$VERSION")"
if [[ "$TAG_SHA" != "$EVENT_SHA" ]]; then
echo "기존 태그가 현재 배포 SHA와 다른 커밋을 가리킵니다." >&2
echo "version=$VERSION" >&2
echo "tag_sha=$TAG_SHA" >&2
echo "event_sha=$EVENT_SHA" >&2
exit 1
fi
fi
if [[ "$TAG_EXISTS" == true && "$RELEASE_EXISTS" == true ]]; then
echo "태그와 GitHub Release가 이미 존재합니다: $VERSION"
exit 0
fi
if [[ "$TAG_EXISTS" == false && "$RELEASE_EXISTS" == true ]]; then
echo "GitHub Release는 존재하지만 대응하는 Git 태그가 없습니다: $VERSION" >&2
exit 1
fi
if [[ "$TAG_EXISTS" == false ]]; then
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git tag -a "$VERSION" "$EVENT_SHA" -m "$VERSION"
git push origin "refs/tags/${VERSION}"
echo "배포 SHA에 태그를 생성했습니다: $VERSION -> $EVENT_SHA"
else
echo "기존 태그를 사용해 GitHub Release 생성을 재시도합니다: $VERSION"
fi
gh release create "$VERSION" \
--repo "$GITHUB_REPOSITORY" \
--title "$VERSION" \
--notes-file release-notes.md \
--verify-tag
{
echo "### Production release"
echo "- Version: \`$VERSION\`"
echo "- Commit: \`$EVENT_SHA\`"
echo "- Pull Request: #$PR_NUMBER"
} >> "$GITHUB_STEP_SUMMARY"