随着项目逐渐庞大,需求日益增长,我们开始考虑引入 CI ,帮助我们进行 代码的 review 以及 测试。
before_script:
- pwd
- export COVERAGE=true
# 这个配置可以让 私仓 代码可以正常 checkout
- git config --global url."http://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.gitlab.com/".insteadOf http://gitlab.gitlab.com/
# go 环境配置
- go env -w GO111MODULE=on
- go env -w GOPROXY="https://goproxy.cn,direct"
- go env -w GOPRIVATE="*.gitlab.com"
- go env -w GOINSECURE="*.gitlab.com"
stages:
- test
- lint
- build
# 测试
go_test:
tags:
- test
# 只在 MR 进行
only:
- merge_requests
# dev 以外的合并 不执行
except:
variables:
- $CI_MERGE_REQUEST_TARGET_BRANCH_NAME != "dev"
stage: test
image: golang:latest
# 定义 所需的 集成服务 在此处作为样例的是 ETCD 服务
services:
- name: bitnami/etcd:latest # 镜像名
alias: etcd # 别名用于在 测试代码中连接服务 例如 Dial("etcd:2379")
# 定义集成服务的 整体 环境变量
variables:
ALLOW_NONE_AUTHENTICATION: "yes" # 这个变量用于 ETCD 让其初始化时无需密码也能登录
script:
- go test -p 1 -race -v -timeout 30m ./... -v | grep -v 'no test files'
# 语法检查
go_lint:
tags:
- lint
only:
- merge_requests
except:
variables:
- $CI_MERGE_REQUEST_TARGET_BRANCH_NAME != "dev"
stage: lint
image: ccheers/reviewdog:latest
script:
- export GITLAB_API="http://gitlab.gitlab.com/api/v4"
- export REVIEWDOG_INSECURE_SKIP_VERIFY=true
# - golangci-lint run --out-format=line-number --timeout=5m ./...
- reviewdog -reporter=gitlab-mr-discussion
allow_failure: true
# 构建检查
go_build:
tags:
- build
only:
- merge_requests
except:
variables:
- $CI_MERGE_REQUEST_TARGET_BRANCH_NAME != "dev"
stage: build
image: golang:latest
script:
- go build -race ./...
? Automated code review tool integrated with any code analysis tools regardless of programming language
runner:
golangci:
cmd: golangci-lint run --out-format=line-number --timeout=20m ./...
errorformat:
- '%E%f:%l:%c: %m'
- '%E%f:%l: %m'
- '%C%.%#'
- "%f:%l:%c: %m"
level: warning
key: REVIEWDOG_GITLAB_API_TOKEN
Value: User Settings > Access Token
(配置CI环境变量)
(获取 API token)
issues:
# 排除的一些 lint
# 也可以在 go代码 上加注解
# eg://nolint:unused
# 表示 此行 跳过 unused lint 检查
exclude-rules:
- linters:
- staticcheck
text: "SA5001|SA6002"
- linters:
- errcheck
text: "defer"
linters:
disable-all: true
enable:
- deadcode
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- structcheck
- typecheck
- unused
- varcheck
- bodyclose
- misspell
- structcheck
- typecheck
- varcheck
- gofmt
- goimports
- unconvert
- exportloopref
linters-settings:
govet:
check-shadowing: true
check-unreachable: true
check-rangeloops: true
check-copylocks: true
好好学习,天天向上