Gitlab

简要概述

通过 gitlab ci 服务,进行流水线构建,流水线语法参考

Pipeline 模版

文件路径

.gitlab/workflows/grpc-kit.yml

因 gitlab-ci.yml、github action 中的 yaml 文件使用后缀 .yml ,所以其他以 yaml 编写的流水线均按照此规范。

模版内容

# 默认全局配置
default:
  # TODO;根据具体情况选择运行的 runner 标签
  tags:
    - grpc-kit
  # TODO; 依赖文件注意使用缓存,避免每次下载
  #cache:
  #  paths:
  #    - /go/pkg/mod/
  # 框架使用的构建镜像
  image: ccr.ccs.tencentyun.com/grpc-kit/cli:0.3.1

# 默认全局变量
variables:
  CGO_ENABLED: "0"
  GIT_SSL_NO_VERIFY: "true"
  GO111MODULE: "on"
  GOPROXY: "https://goproxy.cn"
  GOSUMDB: "sum.golang.google.cn"
  GOPRIVATE: "https://git.lmq.io"

# 流水线各阶段
stages:
  - pre
  - test
  - build
  - deploy
  - production

# 代码风格、格式检测
go-lint:
  stage: pre
  script:
    - make lint

# 依赖的相关依赖的组件
check-dep:
  stage: pre
  script:
    - which go
    - which protoc
    - which protoc-gen-go
    - which protoc-gen-go-grpc
    - which protoc-gen-grpc-gateway
    - which protoc-gen-openapiv2

# 业务单元测试
unit-tests:
  stage: test
  needs:
    - go-lint
    - check-dep
  script:
    - make test

# 代码覆盖率
coverage:
  stage: test
  script:
    - go test ./... -coverprofile=coverage.txt -covermode count
    - cat coverage.txt

# 生成发送测试报告
reports:
  stage: test
  needs:
    - unit-tests
    - coverage
  script:
    - echo "pass"

# 编译二进制文件
binary-local:
  stage: build
  needs:
    - reports
  script:
    - make build
  artifacts:
    paths:
      - build/
    expire_in: 24h
  when: manual
  allow_failure: false

# 发布容器至默认镜像中心
container-registry:
  stage: build
  needs:
    - binary-local
  script:
    - source scripts/env
    - export VERSION=$(cat VERSION)
    - echo ${CI_REGISTRY_PASSWORD} | docker login ${CI_REGISTRY} -u ${CI_REGISTRY_USER} --password-stdin
    - /kaniko/executor --dockerfile ${CI_PROJECT_DIR}/Dockerfile --context ${CI_PROJECT_DIR} --destination ${CI_REGISTRY_IMAGE}:${VERSION}

# 打成各种安装包,如:tar、rpm、deb
release-package:
  stage: build
  needs:
    - binary-local
  script:
    - echo "package tar"
    - echo "package rpm"
    - echo "package deb"
  artifacts:
    paths:
      - build/
    expire_in: 24h

# 部署测试环境
env-test:
  stage: deploy
  needs:
    - release-package
    - container-registry
  script:
    - echo "deploy test"

# 部署准线上环境
env-staging:
  stage: production
  needs:
    - env-test
  script:
    - echo "deploy staging"

# 部署正式环境,手工确认
env-prod:
  stage: production
  needs:
    - env-staging
  script:
    - echo "deploy production"
  only:
    - main
  when: manual
  allow_failure: false

Pipeline 解析

需提前在 gitlab 仓库内添加以下几个变量值,web 添加地址:${gitlab}/${repository}/-/settings/ci_cd

变量 说明 示列
CI_REGISTRY 镜像服务地址 https://ccr.ccs.tencentyun.com
CI_REGISTRY_IMAGE 容器镜像名称 ccr.ccs.tencentyun.com/opsaid/test1
CI_REGISTRY_USER 镜像服务授权 110110
CI_REGISTRY_PASSWORD 镜像服务授权 password