-
Notifications
You must be signed in to change notification settings - Fork 0
243 lines (213 loc) · 8.17 KB
/
release.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
name: Release
on:
repository_dispatch:
types:
- trigger_release
workflow_dispatch:
inputs:
method:
description: |
Which number to increment in the semantic versioning.
Set 'major', 'minor' or 'patch'.
required: true
type: choice
options:
- "patch"
- "minor"
- "major"
default: "patch"
release:
description: |
Whether is this new release or just only build for development.
Set 'build-only' or 'release'.
required: true
type: choice
options:
- "build-only"
- "release"
default: "release"
permissions:
contents: "read"
id-token: "write"
env:
GITHUB_UID: ${{ secrets.CMS_GITHUB_UID }}
GITHUB_PAT: ${{ secrets.CMS_GITHUB_PAT }}
GITHUB_DOCKER_REGISTRY: "ghcr.io/lantron-ltd"
GOLANG_VERSION: 1.19
CONTAINER_IMAGE_NAME: "npsg"
CONTAINER_SPIDER_FILE: "./Dockerfile"
DEVELOPMENT_VERSION: development
jobs:
setup:
name: Setup
runs-on: ubuntu-latest
steps:
- name: Setup
id: setup
timeout-minutes: 1
run: |
URI="https://api.github.com"
API_HEADER="Accept: application/vnd.github.v3+json"
AUTH_HEADER="Authorization: token ${{ secrets.CMS_GITHUB_PAT }}"
body=$(curl -sSL -H "${AUTH_HEADER}" -H "$API_HEADER" "$URI/repos/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}/jobs")
url=$(echo "${body}" | jq .jobs | jq .[-1] | jq .html_url)
id=$(echo "${body}" | jq .jobs | jq .[-1] | jq .id)
sha=$(echo "${body}" | jq .jobs | jq .[-1] | jq -r .head_sha)
echo "WORKFLOW_URL=${url}" >> ${GITHUB_OUTPUT}
echo "body =>$body"
echo "url =>$url"
echo "id =>$id"
echo "sha =>$sha"
echo "WORKFLOW_URL =>$WORKFLOW_URL"
outputs:
WORKFLOW_URL: ${{ steps.setup.outputs.WORKFLOW_URL }}
release:
name: Release
runs-on: ubuntu-latest
needs: [setup]
steps:
# 1. checkout 代码
- name: 拉取最新代码
uses: actions/checkout@v3
timeout-minutes: 1
with:
fetch-depth: 0
# 2. 读取用户选择的输入数据
- name: 配置环境
id: configure_settings
timeout-minutes: 1
env:
RELEASE: ${{inputs.release}}
run: |
echo "ERROR_TEXT=Failed to configure settings." >> ${GITHUB_ENV}
case "${{ env.RELEASE }}" in
'build-only' | '' ) RELEASE='build-only' ;;
'release') RELEASE='release' ;;
esac
set +e
CURRENT_TAG=$(git describe --exact-match --tags $(git log -n1 --pretty='%h') 2>/dev/null)
set -e
if [ -n "${CURRENT_TAG}" ] && [ "${RELEASE}" == "release" ]; then
echo "::error::Release \"${CURRENT_TAG}\" is already existing at current SHA. Does not create and build as new release again."
echo "ERROR_TEXT=Failed to configure settings. Release \"${CURRENT_TAG}\" is already existing at current SHA." >> ${GITHUB_ENV}
echo "STATUS=failure" >> ${GITHUB_OUTPUT}
else
echo "RELEASE=${RELEASE}" >> ${GITHUB_OUTPUT}
echo "STATUS=success" >> ${GITHUB_OUTPUT}
fi
# 把用户选择的方选写入 ${GITHUB_ENV}
- name: 把用户选择的方选写入 ${GITHUB_ENV}
id: find_semantic_version_method
timeout-minutes: 1
run: |
echo "ERROR_TEXT=Failed to find semantic version method." >> ${GITHUB_ENV}
METHOD="${{inputs.method}}"
if [ -z "${{inputs.method}}" ]; then
METHOD="patch"
fi
echo "METHOD=${METHOD}" >> ${GITHUB_OUTPUT}
# 设置nodejs环境
- name: setup nodejs
uses: actions/setup-node@v3
with:
node-version: 16
# 安装task
- name: 安装task
uses: arduino/setup-task@v1
with:
version: 2.x
repo-token: ${{ secrets.GITHUB_TOKEN }}
# 设置golang环境
- name: Setup Golang
uses: actions/setup-go@v3
timeout-minutes: 25
with:
go-version: ${{ env.GOLANG_VERSION }}
check-latest: true
cache: false
# 安装git-vertag
- name: 安装git-vertag
run: go install github.com/kyoh86/git-vertag@latest
# 设置git tag
- name: Bump-up Semantic Version
env:
METHOD: ${{ steps.find_semantic_version_method.outputs.METHOD }}
run: $(go env GOPATH)/bin/git-vertag ${{ env.METHOD }}
# 查找最新tag
- name: 查找最新tag
id: find_new_tag
if: ${{ steps.configure_settings.outputs.STATUS == 'success' }}
timeout-minutes: 1
run: |
echo "ERROR_TEXT=Failed to find new tag." >> ${GITHUB_ENV}
TAG=$(git describe --exact-match --tags $(git log -n1 --pretty='%h'))
echo "New version is: ${TAG}"
echo "TAG=${TAG}" >> ${GITHUB_OUTPUT}
cp ./package.json /tmp/package.json
sleep 1
cat /tmp/package.json | jq ".version = \"${TAG}\"" > ./package.json
rm /tmp/package.json
# 安装nodejs依赖
- name: 安装nodejs依赖
id: initialize_nodejs_environment
env:
REMOTE_CONTAINERS: "true"
GITHUB_UID: ${{ env.GITHUB_UID }}
GITHUB_PAT: ${{ env.GITHUB_PAT }}
timeout-minutes: 10
run: yarn
# Release
- name: Create a Release
id: create_release
if: ${{ steps.configure_settings.outputs.STATUS == 'success' && steps.configure_settings.outputs.RELEASE == 'release' }}
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ env.GITHUB_PAT }}
with:
tag_name: ${{ steps.find_new_tag.outputs.TAG }}
release_name: ${{ steps.find_new_tag.outputs.TAG }}
# body_path: ${{ steps.release_body.outputs.RELEASE_BODY_FILE_PATH }}
draft: false
prerelease: false
- name: 设置镜像tag
id: set_container_image_tags
if: ${{ steps.configure_settings.outputs.STATUS == 'success' }}
timeout-minutes: 5
env:
GITHUB_PAT: ${{ env.GITHUB_PAT }}
run: |
echo "GITHUB_IMAGE_DEV=${{ env.GITHUB_DOCKER_REGISTRY }}/${{ env.CONTAINER_IMAGE_NAME }}:${{ env.DEVELOPMENT_VERSION }}" >> ${GITHUB_OUTPUT}
echo "GITHUB_IMAGE_PROD=${{ env.GITHUB_DOCKER_REGISTRY }}/${{ env.CONTAINER_IMAGE_NAME }}:${{ steps.find_new_tag.outputs.TAG }}" >> ${GITHUB_OUTPUT}
echo "GITHUB_IMAGE_LATEST=${{ env.GITHUB_DOCKER_REGISTRY }}/${{ env.CONTAINER_IMAGE_NAME }}:latest" >> ${GITHUB_OUTPUT}
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
# 登陆到github docker
- name: 登陆到github docker
uses: docker/login-action@v2
with:
registry: ${{ env.GITHUB_DOCKER_REGISTRY }}
username: ${{ env.GITHUB_UID }}
password: ${{ env.GITHUB_PAT }}
- name: 打包npsg镜像并推送
uses: docker/build-push-action@v3
if: ${{ steps.configure_settings.outputs.STATUS == 'success' && steps.configure_settings.outputs.RELEASE == 'release' }}
env:
GITHUB_IMAGE_PROD: ${{ steps.set_container_image_tags.outputs.GITHUB_IMAGE_PROD }}
GITHUB_IMAGE_LATEST: ${{ steps.set_container_image_tags.outputs.GITHUB_IMAGE_LATEST }}
DATABASE_URL: ${{ secrets.DATABASE_URL }}
with:
# platforms: linux/amd64,linux/arm64
tags: |
${{ env.GITHUB_IMAGE_PROD }},${{ env.GITHUB_IMAGE_LATEST }}
push: true
# - name: 推送npsg镜像到github
# uses: redhat-actions/push-to-registry@v2
# if: ${{ steps.configure_settings.outputs.STATUS == 'success' && steps.configure_settings.outputs.RELEASE == 'release' }}
# with:
# image: ${{ env.CONTAINER_IMAGE_NAME }}
# tags: ${{ steps.find_new_tag.outputs.TAG }} latest
# registry: ${{ env.GITHUB_DOCKER_REGISTRY }}
# username: ${{ env.GITHUB_UID }}
# password: ${{ env.GITHUB_PAT }}