使用gitlab-ci自动构建代码
Lei Chu Lv4

使用docker 运行 gitlab runner

运行 gitlab runner 有很多方式, 当前我们只使用docker 的方式运行runner.

1
2
3
4
5
$ docker run -d --name gitlab-runner --restart always \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /srv/gitlab-runner/config:/etc/gitlab-runner \
-v /etc/gitlab-runner/certs:/etc/gitlab-runner/certs \
gitlab/gitlab-runner:latest

这个地方需要注意的是 公司使用的是自签名证书或自定义证书颁发机构所以在使用的过程中会遇到这个问题

1
2
Couldn't execute POST against https://hostname.tld/api/v4/jobs/request:
Post https://hostname.tld/api/v4/jobs/request: x509: certificate signed by unknown authority

解决方法:

您可以使用openssl客户端将 GitLab 实例的证书下载到/etc/gitlab-runner/certs,因为使用的是docker 所以我们需要将证书挂载到
/etc/gitlab-runner/certs

1
$ openssl s_client -showcerts -connect gitlab.example.com:443 < /dev/null 2>/dev/null | openssl x509 -outform PEM > /etc/gitlab-runner/certs/gitlab.example.com.crt

查看日志发现缺少config.toml 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ docker logs -f gitlab-runner
Runtime platform arch=amd64 os=linux pid=8 revision=98daeee0 version=14.7.0
Starting multi-runner from /etc/gitlab-runner/config.toml... builds=0
Running in system-mode.

Configuration loaded builds=0
listen_address not defined, metrics & debug endpoints disabled builds=0
[session_server].listen_address not defined, session endpoints disabled builds=0
ERROR: Failed to load config stat /etc/gitlab-runner/config.toml: no such file or directory builds=0
ERROR: Failed to load config stat /etc/gitlab-runner/config.toml: no such file or directory builds=0
ERROR: Failed to load config stat /etc/gitlab-runner/config.toml: no such file or directory builds=0
ERROR: Failed to load config stat /etc/gitlab-runner/config.toml: no such file or directory builds=0
ERROR: Failed to load config stat /etc/gitlab-runner/config.toml: no such file or directory builds=0
ERROR: Failed to load config stat /etc/gitlab-runner/config.toml: no such file or directory builds=0
ERROR: Failed to load config stat /etc/gitlab-runner/config.toml: no such file or directory builds=0
ERROR: Failed to load config stat /etc/gitlab-runner/config.toml: no such file or directory builds=0
ERROR: Failed to load config stat /etc/gitlab-runner/config.toml: no such file or directory builds=0

解决方法:

直接执行 gitlab-runner register,并填写URL、token和描述即可,tags选填(参考设置tags)。
executor如果不知道怎么选,就选shell吧。 直接执行shell命令,简单有效。

执行完成后,gitlab-runner会自动修改/etc/gitlab-runner/config.toml文件,并重启daemon程序。

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
# 进入 gitlab-runner 容器
$ docker exec -it gitlab-runner

$ gitlab-ci-multi-runner register
root@10c9e0311f06:/# gitlab-ci-multi-runner register
Runtime platform arch=amd64 os=linux pid=78 revision=98daeee0 version=14.7.0
Running in system-mode.

Enter the GitLab instance URL (for example, https://gitlab.com/):
https://gitlab.example.com/
Enter the registration token:
xxxxxxxxxxx
Enter a description for the runner:
[10c9e0311f06]: xxxxxxxx
Enter tags for the runner (comma-separated):
xx
Registering runner... succeeded runner=xxxxx
Enter an executor: kubernetes, docker-ssh, virtualbox, parallels, shell, ssh, docker+machine, docker-ssh+machine, custom, docker:
ssh
Enter the SSH server address (for example, my.server.com):
127.0.0.1
Enter the SSH server port (for example, 22):
22
Enter the SSH user (for example, root):
root
Enter the SSH password (for example, docker.io):
password
Enter the path to the SSH identity file (for example, /home/user/.ssh/id_rsa):

Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

编写.gitlab-ci.yaml 文件

参考文档.gitlab-ci 参考

把当前文件放在项目的根目录 .gitlab-ci.yaml 或者使用

例如:

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
stages:
- build
- test

default:
# 指定runner 的 tags
tags:
- demo

before_script:
- echo "Hello"
variables:
CI_COMMIT_REF_SLUG: "master"

cache:
key: $CI_COMMIT_REF_SLUG
paths:
- vendor/

job A:
stage: build
tags:
- solarmesh-dev
script:
- mkdir -p vendor
- echo "build" > vendor/hello.txt
cache:
key: $CI_COMMIT_REF_SLUG
paths:
- vendor/
after_script:
- echo "World"

job B:
stage: test
# needs:
# - job A
tags:
- solarmesh-dev
cache:
key: $CI_COMMIT_REF_SLUG
paths:
- vendor/
script:
- cat vendor/hello.txt

job C:
stage: test
tags:
- solarmesh-dev
script:
# 触发更新
- |-
result=$(kubectl set image deployment nginx *=nginx:alpine -n default)
if [[ "$(echo $result | grep updated)" != "" ]]; then
# 镜像地址更新了,会自动重新部署
echo 'UPDATED'
else
# 镜像地址与之前相同,需要触发重新部署
echo 'NOT UPDATE'
kubectl rollout restart deployment nginx -n default
fi

踩坑

在测试的过程中上传artifact 报错 issues,
主要更新虚拟机上面的gitlab-runner 版本即可。

1
2
3
4
5
6
7
8
9
10
11
Uploading artifacts for failed job
00:00
mesg: ttyname failed: Inappropriate ioctl for device
Uploading artifacts...
Incorrect Usage: flag provided but not defined: -artifact-format
NAME:
gitlab-runner artifacts-uploader - create and upload build artifacts (internal)
USAGE:
gitlab-runner artifacts-uploader [command options] [arguments...]
OPTIONS:
FATAL: flag provided but not defined: -artifact-format