在 github 上托管 go 开源项目,CI 使用 travis 的服务感觉很方便,几乎不需要额外的设置,但是涉及到公司的项目,通过 gitlab-ci 来跑 golang 项目的 CI,需要一点技巧。
思路大致是这样:
- 创建项目在 GOPATH 中的目录的 symbolic link ,指向 gitlab-runner 在该项目上执行 CI 时的工作目录
- 切换到这个链接目录,执行每个 stage 的任务
具体的 .gitlab-ci.yaml
文件内容如下:
before_script: - export GO_PROJECT_NAMESPACE="$GOPATH/src/gitexample.com/$CI_PROJECT_NAMESPACE" - echo $GO_PROJECT_NAMESPACE - mkdir -p $GO_PROJECT_NAMESPACE - ln -srf $(pwd) $GO_PROJECT_NAMESPACE - export GO_PROJECT_PATH="$GO_PROJECT_NAMESPACE/$CI_PROJECT_NAME" - echo $GO_PROJECT_PATH - cd $GO_PROJECT_PATHstages: - build - test - releasebuild: stage: build script: - go build test: stage: test script: - go test -v ./...release: stage: release script: - make goreleaser ...... when: on_success allow_failure: true only: - tags
这里注意两个问题
-
ln
命令一定要加上-f
选项覆盖之前创建的link文件,避免报错退出CI。 - 本项目 vendor 目录里的代码已经提交到远程仓库中。
ref: