-
Notifications
You must be signed in to change notification settings - Fork 9
283 lines (239 loc) · 9.6 KB
/
cd.yaml
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
name: CD
on:
push:
branches-ignore:
- '**'
tags:
- 'release-prod-*'
- 'release-pr-*'
env:
GITHUB_API_URL_BASE: https://api.github.com/repos/${{ github.repository }}
# Node.js
NODE_VERSION: 'lts/*'
PNPM_VERSION: '9'
NPM_REGISTRY_URL: 'https://registry.npmjs.org'
NODE_AUTH_TOKEN: ${{ secrets.READ_ONLY_NPM_TOKEN }}
HUSKY: 0
# Slack notifications
SLACK_WEBHOOK: ${{ secrets.GHA_NOTIFICATIONS_WEBHOOK_URL }}
SLACK_CHANNEL: '#triple-web-dev-notifications' # 메시지 보낼 채널
SLACK_USERNAME: 'Triple Frontend' # 메시지를 보내는 계정 이름
SLACK_ICON_EMOJI: ':triple_new:'
SLACK_DETAIL_URL: 'https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}'
SLACK_GITHUB_REPOSITORY: ${{ github.repository }}
SLACK_AUTHOR_NAME: ${{ github.event.sender.login }}
SLACK_AUTHOR_ICON: ${{ github.event.sender.avatar_url }}
SLACK_FOOTER: ${{ github.repository }}
SLACK_GITHUB_REF: ${{ github.event.ref }} # 메시지에 ref로 표시되는 값
SLACK_GITHUB_EVENT_NAME: ${{ github.event_name }} # 메시지에 Event로 표시되는 값
# Nx Cloud
NX_CLOUD_ACCESS_TOKEN: ${{ secrets.NX_CLOUD_ACCESS_TOKEN }}
jobs:
wait-for-ci:
runs-on: [self-hosted, linux, x64]
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v3
with:
version: ${{ env.PNPM_VERSION }}
- name: Use Node.js ${{ env.NODE_VERSION }}
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
registry-url: ${{ env.NPM_REGISTRY_URL }}
# cache: 'pnpm'
- name: Wait for CI
uses: fountainhead/[email protected]
id: wait-for-ci
with:
token: ${{ secrets.GITHUB_TOKEN }}
checkName: build
- name: Notify checking CI timeout
if: steps.wait-for-ci.outputs.conclusion == 'timed_out'
env:
SLACK_COLOR: fail
SLACK_TITLE: ':pleading: CI Waiting timeout'
SLACK_TOPIC: 'CI workflow가 시작되었는지, 기다리는 job의 이름이 CI job과 일치하는지, 확인해주세요.'
run: pnpm dlx @titicaca/gha-tools notify
- name: Notify checking CI failure
if: steps.wait-for-ci.outputs.conclusion == 'failure'
env:
SLACK_COLOR: fail
SLACK_TITLE: ':pleading: CI FAILURE'
SLACK_TOPIC: '배포를 중단합니다.'
run: pnpm dlx @titicaca/gha-tools notify
- name: Shutdown workflow
if: steps.wait-for-ci.outputs.conclusion != 'success'
run: node -e 'process.exit(1)'
release:
needs: wait-for-ci
if: startsWith(github.event.ref, 'refs/tags/release-prod-')
runs-on: [self-hosted, linux, x64]
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v3
with:
version: ${{ env.PNPM_VERSION }}
- name: Use Node.js ${{ env.NODE_VERSION }}
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
registry-url: ${{ env.NPM_REGISTRY_URL }}
# cache: 'pnpm'
- name: Get release version
run: echo "DEPLOY_VERSION=v$(cat ./lerna.json | jq -r '.version')" >> $GITHUB_ENV
- name: Notify deploy start to slack
env:
SLACK_COLOR: gray
SLACK_TITLE: ':rocket: Release WORKING'
SLACK_TOPIC: ${{ env.DEPLOY_VERSION }}
run: pnpm dlx @titicaca/gha-tools notify
- name: Install dependencies
env:
NODE_AUTH_TOKEN: ${{ secrets.READ_ONLY_NPM_TOKEN }}
run: pnpm install
- run: pnpm run build
- name: Release
env:
NODE_AUTH_TOKEN: ${{ secrets.READ_WRITE_NPM_TOKEN }}
run: pnpm -r publish --no-git-checks
- name: Notify deploy success to slack
if: success()
env:
SLACK_COLOR: success
SLACK_TITLE: ':tada: Release SUCCESS'
SLACK_TOPIC: ${{ env.DEPLOY_VERSION }}
run: pnpm dlx @titicaca/gha-tools notify
- name: Notify deploy fail to slack
if: failure()
env:
SLACK_COLOR: fail
SLACK_TITLE: ':pleading: Release FAILURE'
SLACK_TOPIC: ${{ env.DEPLOY_VERSION }}
run: pnpm dlx @titicaca/gha-tools notify
tag:
needs: release
if: success()
runs-on: [self-hosted, linux, x64]
steps:
- uses: actions/checkout@v4
- name: Get released version
run: echo "TAG_NAME=v$(cat ./lerna.json | jq -r '.version')" >> $GITHUB_ENV
- name: Create tag object
id: create-tag-object
# https://docs.github.com/en/free-pro-team@latest/rest/reference/git#create-a-tag-object
run: |
curl --url $GITHUB_API_URL_BASE/git/tags \
-f \
--request POST \
-H 'Authorization: token ${{ secrets.TRIPLE_BOT_GITHUB_TOKEN }}' \
-H 'Content-Type: application/json' \
-d "{\"tag\":\"$TAG_NAME\",\"message\":\"released at \`${{ github.event.updated_at }}\`\",\"object\":\"${GITHUB_SHA}\",\"type\":\"commit\"}" \
> tag.json
echo "::set-output name=tag-sha::$(node -p -e 'require(`./tag.json`).sha')"
- name: Check tag ref exist
id: check-tag-ref
# https://docs.github.com/en/free-pro-team@latest/rest/reference/git#get-a-reference
run: |
curl --url $GITHUB_API_URL_BASE/git/refs/tags/$TAG_NAME \
-sI \
-o /dev/null \
-w "%{http_code}" \
-H 'Authorization: token ${{ secrets.TRIPLE_BOT_GITHUB_TOKEN }}' \
> status
echo "::set-output name=status::$(cat status)"
- name: Create new tag ref
if: ${{ steps.check-tag-ref.outputs.status != '200' }}
env:
TAG_SHA: ${{ steps.create-tag-object.outputs.tag-sha }}
# https://docs.github.com/en/free-pro-team@latest/rest/reference/git#create-a-reference
run: |
curl --url $GITHUB_API_URL_BASE/git/refs \
-f \
--request POST \
-H 'Authorization: token ${{ secrets.TRIPLE_BOT_GITHUB_TOKEN }}' \
-H 'Content-Type: application/json' \
-d "{\"ref\":\"refs/tags/$TAG_NAME\",\"sha\":\"$TAG_SHA\"}"
- name: Update tag ref
if: ${{ steps.check-tag-ref.outputs.status == '200' }}
env:
TAG_SHA: ${{ steps.create-tag-object.outputs.tag-sha }}
# https://docs.github.com/en/free-pro-team@latest/rest/reference/git#update-a-reference
run: |
curl --url $GITHUB_API_URL_BASE/git/refs/tags/$TAG_NAME \
-f \
--request PATCH \
-H 'Authorization: token ${{ secrets.TRIPLE_BOT_GITHUB_TOKEN }}' \
-H 'Content-Type: application/json' \
-d "{\"force\":true,\"sha\":\"${TAG_SHA}\"}"
canary-release:
needs: wait-for-ci
if: startsWith(github.event.ref, 'refs/tags/release-pr-')
runs-on: [self-hosted, linux, x64]
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: pnpm/action-setup@v3
with:
version: ${{ env.PNPM_VERSION }}
- name: Use Node.js ${{ env.NODE_VERSION }}
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
registry-url: ${{ env.NPM_REGISTRY_URL }}
# cache: 'pnpm'
- name: Get release version
env:
TAG_NAME: ${{ github.event.ref }}
run: |
NEXT_VERSION=$(node -e "const [major, minor, patch] = require('./lerna.json').version.split('.');console.log(['v'+major, minor, parseInt(patch, 10) + 1].join('.'))")
PR_NUMBER=${TAG_NAME:21} # refs/tags/release-pr-<num>에서 <num>만 추출
REF_COUNT=$(node -p -e "Math.max(0, parseInt((\"$(git describe --always --long --dirty --match "v*.*.*")\".match(/^(?:.*@)?.*-(\d+)-.*?$/) || ['0', '0'])[1], 10) - 1)")
echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
echo "DEPLOY_VERSION=$NEXT_VERSION-pr-$PR_NUMBER.$REF_COUNT" >> $GITHUB_ENV
- name: Notify release start to Slack
env:
SLACK_COLOR: gray
SLACK_TITLE: ':rocket: Release WORKING'
SLACK_TOPIC: ${{ env.DEPLOY_VERSION }}
run: pnpm dlx @titicaca/gha-tools notify
- name: Install dependencies
env:
NODE_AUTH_TOKEN: ${{ secrets.READ_ONLY_NPM_TOKEN }}
run: pnpm install
- run: pnpm run build
- run: |
pnpm exec lerna version $DEPLOY_VERSION \
--amend \
--force-publish \
--ignore-scripts \
--no-git-tag-version \
--preid "pr-$PR_NUMBER" \
--yes
- name: Publish as canary
env:
NODE_AUTH_TOKEN: ${{ secrets.READ_WRITE_NPM_TOKEN }}
run: pnpm -r publish --tag canary --no-git-checks
- name: Notify released version on pull request
run: |
curl \
--url $GITHUB_API_URL_BASE/issues/${{ env.PR_NUMBER }}/comments \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Content-Type: application/json" \
-f --request POST \
-d "{\"body\":\"${{ env.DEPLOY_VERSION }} has been published!\"}"
- name: Notify release success to Slack
if: success()
env:
SLACK_COLOR: success
SLACK_TITLE: ':tada: Release SUCCESS'
SLACK_TOPIC: ${{ env.DEPLOY_VERSION }}
run: pnpm dlx @titicaca/gha-tools notify
- name: Notify release failure to Slack
if: failure()
env:
SLACK_COLOR: fail
SLACK_TITLE: ':pleading: Release FAILURE'
SLACK_TOPIC: ${{ env.DEPLOY_VERSION }}
run: pnpm dlx @titicaca/gha-tools notify