CI/CD中的基本概念可以参考其他博文,主要包括jobs, pipeline, stages等。本文介绍完整的ci/cd搭建环境。
- 安装、注册和启动runner,docs.gitlab.com/runner/inst… ,以GNU/linux中安装runner为例:
//下载rpm,centos/red hat
]# curl -LJO "https://gitlab-runner-downloads.s3.amazonaws.com/latest/rpm/gitlab-runner_amd64.rpm"
//安装
]# rpm -i gitlab-runner_amd64.rpm
//注册runner
]# gitlab-runner register
Enter tags for the runner (comma-separated):
wx-haha-go
Registering runner... succeeded runner=_9zz4yAQ
Enter an executor: custom, docker-ssh, parallels, shell, ssh, docker+machine, kubernetes, docker, virtualbox, docker-ssh+machine:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
//启动runner
]# gitlab-runner start
Runtime platform arch=amd64 os=linux pid=11797 revision=2ebc4dc4 version=13.9.0
executor是runner在哪里执行构建,比如shell或者docker。
- 编写自动集成脚本 .gitlab-ci.yml ,并放在项目根目录
stages:
- build
- test
- push
- deploy
build_job:
stage: build
script:
- make docker-build
test_job:
stage: test
script:
- make test
push_job:
stage: push
script:
- make docker-push
deploy-thanos-lab:
stage: deploy
when: manual
script:
- echo "deploy-thanos-lab job start..."
- make deploy-test
- echo "deploy-thanos-lab job end!"
deploy-thanos:
stage: deploy
when: manual
only:
- master
script:
- echo "deploy-thanos job start..."
- make deploy-prod
- echo "deploy-thanos job end!"
这样在git ci提交master分支变更时,会在gitlab CI/CD中看到自动集成的过程,并可以手动部署到测试环境或线上环境。注意:如果不需要shared runner, 需要 disable shared runner for this project。
错误整理
- 在pipeline的build阶段,报错:
$ make docker-build
TAG=2021.03.01.45c222b docker-compose -f docker-compose.yml build
[31948] Failed to execute script docker-compose
Traceback (most recent call last):
File "urllib3/connectionpool.py", line 677, in urlopen
File "urllib3/connectionpool.py", line 392, in _make_request
File "http/client.py", line 1252, in request
File "http/client.py", line 1298, in _send_request
File "http/client.py", line 1247, in endheaders
File "http/client.py", line 1026, in _send_output
File "http/client.py", line 966, in send
File "docker/transport/unixconn.py", line 43, in connect
PermissionError: [Errno 13] Permission denied
以root用户运行gitlab-runner
gitlab-runner uninstall # 删除gitlab-runner
gitlab-runner install --working-directory /home/gitlab-runner --user root # 安装并设置--user(设置为root)
service gitlab-runner restart # 重启gitlab-runner
ps aux|grep gitlab-runner # 查看当前runner用户
- 执行本地make docker-build发现报错如下,
Get https://registry-1.docker.io/v2/library/golang/manifests/sha256:94c4191a12b9df4ebe8af4115acc8d9ae067dea5069904908584eb8f4cef2e5d: dial tcp: lookup registry-1.docker.io on 10.160.0.1:53: read udp 10.160.31.195:48884->10.160.0.1:53: i/o timeout
ERROR: Service 'server' failed to build
make: *** [docker-build] Error 1
配置docker使用阿里云镜像,编辑/etc/docker/daemon.json
{
"registry-mirrors":["https://6kx4zyno.mirror.aliyuncs.com"]
}
重启docker
[@hbhly_31_195 wx-haha-go]# systemctl daemon-reload
[@hbhly_31_195 wx-haha-go]# systemctl restart docker
- 在pipeline的test阶段,报错
internal/pkg/config/config.go:6:2: stat /search/nginx/html/go_project/pkg/mod/github.com/fsnotify/fsnotify@v1.4.9/fen.go: permission denied
router/router.go:5:2: stat /search/nginx/html/go_project/pkg/mod/github.com/gin-contrib/pprof@v1.3.0/pprof.go: permission denied
internal/pkg/log/acccess.go:7:2: stat /search/nginx/html/go_project/pkg/mod/github.com/gin-gonic/gin@v1.6.3/path.go: permission denied
internal/pkg/myredis/myredis.go:5:2: stat /search/nginx/html/go_project/pkg/mod/github.com/go-redis/redis/v8@v8.4.4/pool_test.go: permission denied
同1