高哲技术博客 高哲技术博客
首页
编程
爬虫
运维
硬件
收藏
归档
关于

嘉美伯爵

前途光明,无需畏惧
首页
编程
爬虫
运维
硬件
收藏
归档
关于
  • 方案

  • 数据库

  • 虚拟化

  • 自动化部署

    • CI

    • CD

    • k8s

    • 构建

      • 使用kaniko构建docker镜像
      • github和gitlab actions语法总结
      • gitlab ci如何拉取golang私有库
        • 方案一
        • 方案二
        • demo
        • 参考
      • 如何使用gitlab ci构建maven应用
  • 中间件

  • 可观测

  • 操作系统

  • 运维
  • 自动化部署
  • 构建
fovegage
2023-07-14
目录

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
  • 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

# 方案二

- 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

# 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

# 参考

  • Go + Docker技巧-私人仓库和Gitlab CI (opens new window)
  • 仓库镜像 (opens new window)
  • Docker安装Gitlab和Runner并实现CICD (opens new window)
上次更新: 2023-07-31 10:01:32
github和gitlab actions语法总结
如何使用gitlab ci构建maven应用

← github和gitlab actions语法总结 如何使用gitlab ci构建maven应用→

最近更新
01
token embed和postion embed
06-10
02
k8s pod日志排查问题
10-24
03
golang内部私服建设方案
10-21
更多文章>
Theme by Vdoing | Copyright © 2018-2025 嘉美伯爵 | 鲁ICP备20001560号-4
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式