如何使用Github Action完成持续集成
# 说明
1. github action 根据分支判断 根据ci.yaml 完成ci
2. VpnBookCD 结合 argocd 完成 CD 操作
3. argocd 拉取 CR 镜像仓库完成部署
1
2
3
4
5
2
3
4
5
# cd
argocd 部署
1
# 在步骤之间共享数据
https://qiwihui.com/qiwihui-blog-98/
1
# ci.yaml
name: Image
on:
push:
branches:
- main
- dev
paths:
- '**.go'
- 'go.mod'
- '**.yaml'
tags: v*-release
jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.18
- name: Granting private modules access
run: git config --global url."https://ghp_xxxxxxMdA5yfdiH8L0F32Dl:x-oauth-basic@github.com/".insteadOf "https://github.com/"
- name: Build
run: make all
- name: Generate Dockerfile
run: |
cat > Dockerfile << __EOF__
FROM alibaba-cloud-linux-3-registry.cn-hangzhou.cr.aliyuncs.com/alinux3/alinux3
ADD releases /usr/bin/
EXPOSE 6000
__EOF__
- name: Log into registry
run: echo "xxxxxx" | docker login registry.cn-shanghai.aliyuncs.com -u sdgaozhe@qq.com --password-stdin
- name: Push image
run: |
IMAGE_ID=registry.cn-shanghai.aliyuncs.com/vpnbook/github
# Change all uppercase to lowercase
IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]')
# Strip git ref prefix from version
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
# Strip "v" prefix from tag name
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
[[ "${{ github.ref }}" == "refs/heads/"* ]] && VERSION=$(echo "${{ github.sha }}" | cut -c 1-12)
# Use Docker `latest` tag convention
[ "$VERSION" == "main" ] && VERSION=latest
echo IMAGE_ID=$IMAGE_ID
echo VERSION=$VERSION
docker build . --file Dockerfile --tag $IMAGE_ID:$VERSION
docker push $IMAGE_ID:$VERSION
curl "https://open.larksuite.com/open-apis/bot/v2/hook/dce0db65-7d9a-4002-a3dd-d797f914cabc" -H "Content-Type:application/json" -d "{ \"msg_type\": \"interactive\", \"card\": { \"config\": { \"wide_screen_mode\": false, \"enable_forward\": true }, \"header\": { \"title\": { \"content\": \"Github Action\", \"tag\": \"plain_text\" } }, \"elements\": [ { \"tag\": \"markdown\", \"content\": \"--------------\n**Repo**\n$GITHUB_REPOSITORY\n\n**Author**\n${{ github.event.commits[0].author.name }}\n\n**Commit**\n${{ github.event.commits[0].message }}\n\n**Docker Image**\n$IMAGE_ID:$VERSION\" }, { \"actions\": [ { \"tag\": \"button\", \"text\": { \"tag\": \"lark_md\", \"content\": \"View Build Logs\", \"lines\": 1 }, \"url\": \"$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID\", \"type\": \"primary\", \"value\": {} } ], \"tag\": \"action\", \"layout\": \"bisected\" }, { \"tag\": \"note\", \"elements\": [ { \"tag\": \"plain_text\", \"content\": \"$(date)\" } ] } ] } }"
echo "IMAGE_ID=$IMAGE_ID" >> $GITHUB_ENV
echo "VERSION=$VERSION" >> $GITHUB_ENV
- name: Checkout VpnBookCD
if: github.ref == 'refs/heads/main'
uses: actions/checkout@v2
with:
repository: PanGuData/VpnBookCD
ref: main
token: ghp_xxxxxxMdA5yfdiH8L0F32Dl
- name: Setup kustomize
uses: imranismail/setup-kustomize@v1
- name: Deploy
if: github.ref == 'refs/heads/main'
run: |
ARGO_APP=$(echo ${{ github.event.repository.name }} | tr '[A-Z]' '[a-z]' | tr '_' '-')
if [ ! -d $ARGO_APP ]; then
cp -r spider-workflows $ARGO_APP
# replace deployment name
sed -i "s#__deploy_name__#$ARGO_APP#g" $ARGO_APP/base/*.yml $ARGO_APP/overlays/product/*.yml
# append custom args
sed -i 's#__extra_args__##g' $ARGO_APP/overlays/product/*.yml
fi
cd $ARGO_APP/overlays/product && kustomize edit set image k8s_image=${{ env.IMAGE_ID }}:${{ env.VERSION }} && cd ../../../ || exit 1
git config user.name github-actions
git config user.email github-actions@github.com
git add .
git commit -m "CD: update tag id ---> ${{ env.VERSION }}"
git fetch && git rebase origin/main && git push origin main
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
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
上次更新: 2023-07-16 12:05:32