-
Notifications
You must be signed in to change notification settings - Fork 5
309 lines (279 loc) · 10.2 KB
/
ci.yml
File metadata and controls
309 lines (279 loc) · 10.2 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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
name: CI
on:
push:
branches:
- '*'
pull_request:
types: [opened, reopened, edited, synchronize]
branches:
- 'main'
env:
DOCKER_IMAGE: 36node/auth
HARBOR_IMAGE: ${{ secrets.HARBOR_DOMAIN }}/36node/auth
SDK_TS: '@36node/auth-sdk'
SDK_TS_REPO: auth-sdk-ts
ORG: 36node
CLUSTER_DIR: ~/Workspace/z-ctyun-stage
TAG_NAME: auth_tag
DEPLOY: auth-${{ github.event.pull_request.number }}
TEND_UAT_PLAYBOOK: site-tend-uat.yaml
TEND_PR_PLAYBOOK: site-tend-uat.yaml
TEND_DOMAIN: auth.tend-core-${{ github.event.pull_request.number }}.36node.com
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
test:
runs-on: ubuntu-latest
services:
mongodb:
image: mongo
ports:
- 27017:27017
redis:
image: redis/redis-stack-server
ports:
- 6379:6379
outputs:
new_sdk: ${{ steps.check-new-sdk.outputs.new }}
sdk_version: ${{ steps.sdk-version.outputs.sdk_version }}
artifact_name: ${{ steps.artifact-name.outputs.artifact_name }}
steps:
- name: Extract env from pr body
id: pr-env
uses: 36node/action-pr-env@v1.0.0
with:
text: '${{ github.event.pull_request.body }}'
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: '0'
- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: 22
- uses: pnpm/action-setup@v4
name: Install pnpm
with:
version: 10.22.0
run_install: false
- name: Get pnpm store directory
id: pnpm-cache
run: echo "pnpm_cache_dir=$(pnpm store path)" >> $GITHUB_OUTPUT
- name: Setup pnpm cache
uses: actions/cache@v4
with:
path: ${{ steps.pnpm-cache.outputs.pnpm_cache_dir }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Install dependencies
run: pnpm install --frozen-lockfile
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Eslint
run: pnpm lint
# - name: Unit Test
# run: pnpm test
# env:
# NODE_ENV: test
- name: Integration Test
run: pnpm test:e2e
env:
NODE_ENV: test
- name: Generate openapi.json
run: |
pnpm build
NODE_ENV=development node ./bin/generate-swagger.js
- name: Check if openapi.json match the code
run: |
if git diff --name-only | grep openapi.json; then
echo "The file openapi.json does not matches the code."
exit 1
else
echo "The file openapi.json matches the code."
fi
- name: Set sdk version
id: sdk-version
run: |
CURRENT_VERSION=$(node -p "require('./package.json').version")
PR_NUMBER=${{ github.event.pull_request.number}}
OPENAPI_HASH=$(node -p "require('./openapi.json').hash")
SHORT_HASH=$(echo $OPENAPI_HASH | cut -c1-10)
SDK_VERSION="$CURRENT_VERSION-pr-$PR_NUMBER-$SHORT_HASH"
echo "sdk_version=$SDK_VERSION" >> $GITHUB_OUTPUT
- name: Check if a new sdk should to be generated
if: github.event_name == 'pull_request'
id: check-new-sdk
run: |
if git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} | grep 'openapi.json'; then
if npm view ${{env.SDK_TS }}@${{ steps.sdk-version.outputs.sdk_version }} time --json; then
echo "new=false" >> $GITHUB_OUTPUT
else
echo "new=true" >> $GITHUB_OUTPUT
fi
else
echo "new=false" >> $GITHUB_OUTPUT
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Set artifact name
if: steps.check-new-sdk.outputs.new == 'true'
id: artifact-name
run: |
SHORT_SHA=$(echo $(git rev-parse HEAD) | cut -c1-7)
PR_NUMBER=${{ github.event.pull_request.number}}
ARTIFACT_NAME="openapi-$PR_NUMBER-$SHORT_SHA"
echo "artifact_name=$ARTIFACT_NAME" >> $GITHUB_OUTPUT
- name: Upload artifact
uses: actions/upload-artifact@v4
if: steps.check-new-sdk.outputs.new == 'true'
id: upload-artifact
with:
name: ${{ steps.artifact-name.outputs.artifact_name }}
path: ./openapi.json
build:
needs: [test]
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: |
${{ env.DOCKER_IMAGE }}
${{ env.HARBOR_IMAGE }}
# generate Docker tags based on the following events/attributes
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=sha
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: '${{ secrets.DOCKERHUB_USERNAME }}'
password: '${{ secrets.DOCKERHUB_TOKEN }}'
- name: Login to Harbor
uses: docker/login-action@v3
with:
registry: ${{ secrets.HARBOR_DOMAIN }}
username: ${{ secrets.HARBOR_USERNAME }}
password: ${{ secrets.HARBOR_PASSWORD }}
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/amd64
gen-sdk-ts:
needs: [test]
if: github.event_name == 'pull_request' && needs.test.outputs.new_sdk == 'true'
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Trigger ts sdk generation workflow and wait
uses: convictional/trigger-workflow-and-wait@v1.6.1
id: sdk-ts-generation
with:
owner: ${{ env.ORG }}
repo: ${{ env.SDK_TS_REPO }}
github_token: ${{ secrets.DISPATCH_TOKEN }}
workflow_file_name: generate-sdk.yml
ref: refs/heads/main
client_payload: '{"artifactName": "${{ needs.test.outputs.artifact_name }}","runId": "${{ github.run_id }}","version": "${{ needs.test.outputs.sdk_version }}","prNumber": "${{ github.event.pull_request.number }}","prTitle": "${{ github.event.pull_request.title }}","prHtmlUrl": "${{ github.event.pull_request.html_url }}"}'
- name: Comment PR
uses: thollander/actions-comment-pull-request@v2
with:
message: |
sdk version ${{ needs.test.outputs.sdk_version }}
```shell
## for js/ts project
pnpm add ${{ env.SDK_TS }}@${{ needs.test.outputs.sdk_version }}
```
reactions: eyes, rocket
# deploy-tend-pr:
# needs: [build]
# if: github.event_name == 'pull_request'
# runs-on: ubuntu-latest
# steps:
# - name: Extract env from pr body
# id: pr-env
# uses: 36node/action-pr-env@v1.0.0
# with:
# text: '${{ github.event.pull_request.body }}'
# - name: Deploy
# uses: appleboy/ssh-action@v1.0.3
# with:
# host: ${{ secrets.DEPLOY_HOST }}
# username: ${{ secrets.DEPLOY_USERNAME }}
# key: ${{ secrets.DEPLOY_KEY }}
# port: ${{ secrets.DEPLOY_PORT }}
# script: |
# cd ${{ env.CLUSTER_DIR }}
# source .envrc
# EXPORT_COMMANDS=$(echo '${{ toJson(steps.pr-env.outputs.env) }}' | jq -r 'to_entries | .[] | "export \(.key)=\(.value)"')
# echo "$EXPORT_COMMANDS"
# eval "$EXPORT_COMMANDS"
# tag=$(echo sha-${{ github.sha }} | cut -c1-11)
# ansible-playbook ${{ env.TEND_PR_PLAYBOOK }} -e "${{ env.TAG_NAME }}=${tag} deploy=${{ env.DEPLOY }}"
# - name: Comment PR
# uses: thollander/actions-comment-pull-request@v2
# with:
# message: |
# Deployed to: https://${{ env.TEND_DOMAIN }}
# reactions: eyes, rocket
# - name: Feishu Robot Nessage
# run: |
# curl -X POST -H "Content-Type: application/json" \
# -d '
# {
# "msg_type": "post",
# "content": {
# "post": {
# "zh_cn": {
# "title": "请查阅: Auth PR ${{ github.event.pull_request.number }} 已发布/更新",
# "content": [
# [{
# "tag": "a",
# "text": "${{ github.event.pull_request.title }}\n",
# "href": "${{ github.event.pull_request.html_url }}"
# },{
# "tag": "text",
# "text": "Deploy to https://${{ env.TEND_DOMAIN }}\n"
# }]
# ]
# }
# }
# }
# }' \
# https://open.feishu.cn/open-apis/bot/v2/hook/${{ secrets.TEND_FEISHU_ROBOT_TOKEN }}
# deploy-tend-uat:
# needs: [build]
# if: ${{ github.ref == 'refs/heads/main' && github.event_name == 'push' }}
# runs-on: ubuntu-latest
# steps:
# - name: Deploy
# uses: appleboy/ssh-action@v1.0.3
# with:
# host: ${{ secrets.DEPLOY_HOST }}
# username: ${{ secrets.DEPLOY_USERNAME }}
# key: ${{ secrets.DEPLOY_KEY }}
# port: ${{ secrets.DEPLOY_PORT }}
# script: |
# cd ${{ env.CLUSTER_DIR }}
# source .envrc
# tag=$(echo sha-${{ github.sha }} | cut -c1-11)
# ansible-playbook ${{ env.TEND_UAT_PLAYBOOK }} -e "${{ env.TAG_NAME }}=${tag}"