gitlab ci如何拉取golang私有库
# 方案一
- 构建
# 内置变量:CI_JOB_TOKEN,会自动传入(写在gitlab-ci.yaml中)
docker build --build-arg CI_JOB_TOKEN=$CI_JOB_TOKEN -f Dockerfile -t $some_tag .
1
2
2
- docker
ARG CI_JOB_TOKEN
ENV CI_JOB_TOKEN=$CI_JOB_TOKEN
RUN git config --global url."https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.private/group".insteadOf "git@gitlab.private:group"
1
2
3
2
3
# 方案二
- go env -w GOPROXY=https://goproxy.io,direct
- go env -w GOPRIVATE=git.hongyuan.com/spider
- echo -e "machine git.hongyuan.com\nlogin $GITLAB_USERNAME \npassword $GITLAB_PASSWORD" > ~/.netrc
- chmod 600 ~/.netrc
- git config --global --add url."git@hongyuan.com:".insteadof "https://git.hongyuan.com/"
1
2
3
4
5
2
3
4
5
# demo
stages:
- check
- build
- cd
check_code:
stage: check
image: golang:1.19
except:
- main
- dev
before_script:
- go env -w GOPROXY=https://goproxy.io,direct
- go env -w GOPRIVATE=git.xxx.com/spider
# - go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.53.3
- echo -e "machine git.xxx.com\nlogin $GITLAB_USERNAME \npassword $GITLAB_PASSWORD" > ~/.netrc
- chmod 600 ~/.netrc
- git config --global --add url."git@xxx.com:".insteadof "https://git.xxx.com/"
script:
# - go mod vendor
# - golangci-lint run
- make all
after_script:
- export TZ='Asia/Shanghai'
- export build_time=$(date +"%Y-%m-%d %H:%M:%S")
- export repository_url=$(git config --get remote.origin.url)
- export commit_author=$(git log -1 --pretty=format:"%an")
- export commit_message=$(git log -1 --pretty=format:"%s")
- export REPO_NAME=$(basename -s .git "$repository_url")
- export build_logs_url="${CI_PROJECT_URL}/-/jobs/${CI_JOB_ID}"
- |
if [ $? -eq 0 ]; then
export check_status="Job Success"
export check_color="green"
else
export check_status="Job failed"
export check_color="red"
fi
- curl "https://open.feishu.cn/open-apis/bot/v2/hook/${LARK_TOKEN}" -H "Content-Type:application/json" -d "{\"msg_type\":\"interactive\",\"card\":{\"config\":{\"wide_screen_mode\":false,\"enable_forward\":true},\"header\":{\"title\":{\"content\":\"Gitlab Runner\",\"tag\":\"plain_text\"},\"template\":\"$check_color\"},\"elements\":[{\"tag\":\"markdown\",\"content\":\"**Repository:** $REPO_NAME\"},{\"tag\":\"markdown\",\"content\":\"**Commit Author:** $commit_author\"},{\"tag\":\"markdown\",\"content\":\"**Commit Message:** $commit_message\"},{\"tag\":\"markdown\",\"content\":\"**Check Status:** $check_status\"},{\"tag\":\"div\",\"text\":{\"tag\":\"lark_md\",\"content\":\"**Build Log:** [Please Click Me]($build_logs_url)\"}},{\"tag\":\"note\",\"elements\":[{\"tag\":\"plain_text\",\"content\":\"$build_time\"}]}]}}"
build_image:
stage: build
only:
- main
- dev
image: docker:stable
before_script:
- export IMAGE_TAG="registry.cn-beijing.aliyuncs.com/biyao/spider:${CI_COMMIT_SHA}"
script:
- echo $IMAGE_TAG
- echo "${ACR_PASSWORD}" | docker login registry.cn-beijing.aliyuncs.com -u ${ACR_USERNAME} --password-stdin
- docker build . --file Dockerfile --tag $IMAGE_TAG --build-arg "CI_JOB_TOKEN=${CI_JOB_TOKEN}" --build-arg "GITLAB_USERNAME=${GITLAB_USERNAME}" --build-arg "GITLAB_PASSWORD=${GITLAB_PASSWORD}"
- docker push $IMAGE_TAG
cd-job:
stage: cd
image: smartive/kustomize
only:
- main
- dev
before_script:
- export IMAGE_TAG="registry.cn-beijing.aliyuncs.com/biyao/spider:${CI_COMMIT_SHA}"
- git config --global user.name xxx
- git config --global user.email xxx@xxx.com
- git clone https://gitlab-cd:xxx@git.xxx.com/spider/BiyaoCD.git
script:
- |
export ARGO_APP=$(echo $CI_PROJECT_NAME | tr '[A-Z]' '[a-z]')
echo "$ARGO_APP"
echo "print CI_COMMIT_TITLE"
echo "$CI_COMMIT_TITLE"
if [ -d "BiyaoCD/${ARGO_APP}/overlays" ]; then
cd BiyaoCD/${ARGO_APP}/overlays
# 发布到测试环境
if [[ "$CI_COMMIT_REF_NAME" != "main" ]]; then
if echo "$CI_COMMIT_TITLE" | grep -q "passgateway"; then
cd passgateway/dev && kustomize edit set image k8s_image=${IMAGE_TAG} && cd ../../
elif echo "$CI_COMMIT_TITLE" | grep -q "signgateway"; then
cd signgateway/dev && kustomize edit set image k8s_image=${IMAGE_TAG} && cd ../../
elif echo "$CI_COMMIT_TITLE" | grep -q "modelgateway"; then
cd modelgateway/dev && kustomize edit set image k8s_image=${IMAGE_TAG} && cd ../../
else
echo "Please select branch";
exit 1;
fi
fi
# 发布到生产环境
if [ "$CI_COMMIT_REF_NAME" == "main" ]; then
if echo "$CI_COMMIT_TITLE" | grep -q "passgateway"; then
cd passgateway/prod && kustomize edit set image k8s_image=${IMAGE_TAG} && cd ../../
elif echo "$CI_COMMIT_TITLE" | grep -q "signgateway"; then
cd signgateway/dev && kustomize edit set image k8s_image=${IMAGE_TAG} && cd ../../
elif echo "$CI_COMMIT_TITLE" | grep -q "modelgateway"; then
cd modelgateway/dev && kustomize edit set image k8s_image=${IMAGE_TAG} && cd ../../
else
echo "Please select branch";
exit 1;
fi
fi
git add .
git commit -m "CD: update ${ARGO_APP}: ${IMAGE_TAG}"
git fetch && git rebase origin/main && git push
fi
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
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
# 参考
上次更新: 2023-07-31 10:01:32