为了测试 gitlab CI pipeline, 从而在家里的台式机搭建了测试环境。
为什么进行这次实验?因为自己平时只是用到了 gitlab,它对于我来说是一个黑盒。
为了搞清楚 CI job 里面的一些 function 用法,自己搭建一套环境。一是方便实验,二是自己不能在公司的环境上实验,担心造成破坏。
Update history
2021-01-09 初稿,妹子什么时候才能好好学习呀
Environment
默认技能:
- docker, 虚拟机上安装 docker 的过程就省略了
- DNS, 因为是自定义的域名,所以需要绑定在本地 hosts
最开始 Ubuntu 机器只有 2 cpu 4GB, 安装 gitlab 非常卡顿, load 高到 40+, 增加配置后,顺利安装
Name | Operating system | CPU | RAM | Disk | IP |
---|---|---|---|---|---|
gitlab-main | Ubuntu 18.04.5 LTS | 4 | 8G | 50G | 192.168.17.3 |
gitlab-runner | CentOS 7.8.2003 | 1 | 2G | 20G | 192.168.17.4 |
Install gitlab
gitlab
本次实验,为了快速安装选择了 docker 模式。 https://docs.gitlab.com/omnibus/docker/README.html
本地 22
端口被 SSH 占用了,所以改为了 2222
,这里需要注意的是,我用的是 社区版本 ce
1
2
3
4
5
6
7
8
9sudo docker run --detach \
--hostname gitlab.feiyang.com \
--publish 443:443 --publish 80:80 --publish 2222:22 \
--name gitlab \
--restart always \
--volume $GITLAB_HOME/config:/etc/gitlab \
--volume $GITLAB_HOME/logs:/var/log/gitlab \
--volume $GITLAB_HOME/data:/var/opt/gitlab \
gitlab/gitlab-ce:latest
如果还需要修改配置,可以 attach 到容器内部,进行修改。
例如修改 relative URL https://docs.gitlab.com/omnibus/settings/configuration.html#enable-relative-url-in-gitlab1
2
3
4
5
6
7
8
9
10
11docker exec -it gitlab bash
vi /etc/gitlab/gitlab.rb
# 指定host地址
external_url 'http://gitlab.feiyang.com'
# 重新加载配置文件并重启服务
gitlab-ctl reconfigure
gitlab-ctl restart
第一次登陆,一定要以 IP 地址登陆,而不是用 localhost。 否则设置 root 新密码就会遇到错误 8 errors prohibited this user from being saved
成功登陆网友以后,我创建了一个新用户,并且设置了 SSH Key,创建了一个 test repo
在这里需要的注意的是,如果没有设置域名
http://gitlab.feiyang.com
那在repo 页面上 git clong 的 url 可能不对,导致无法解析并下载。
runner
本次实验,为了快速安装选择了 docker 模式。 https://docs.gitlab.com/runner/install/docker.html
1 | docker volume create gitlab-runner-config |
最好先进入容器设置一下 extra_hosts = [“gitlab.feiyang.com:192.168.17.3”] (这里后文有详细介绍 config.toml 可以搜索关键词) 否则注册 runner 的时候,只能输入 IP
下一步就是注册,前提你需要去 gitlab 网页上 CI/CD section runner 详情页上获得 token
1 | [root@centos7 jy576]# docker run --rm -it -v gitlab-runner-config:/etc/gitlab-runner gitlab/gitlab-runner:latest register |
回到网页端,我们需要修改一下 runner 设置,勾选上 Run untagged jobs
因为我们是自定义域名,所以我们需要在 gitlab-runner 里面进行设置,否则 CI job 无法 pull1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17Running with gitlab-runner 13.7.0 (943fc252)
on centos7 FSX9yRiU
Preparing the "docker" executor
00:04
Using Docker executor with image python:3.9.1-slim-buster ...
Pulling docker image python:3.9.1-slim-buster ...
Using docker image sha256:b55839ea7a0e9bb534237d00558cb96dce4013bf7f1092966fe0e27e98f8179f for python:3.9.1-slim-buster with digest python@sha256:4d92968b26bb6b7b62d957244de86fc1054f03793577d49e85c00864eb03ca07 ...
Preparing environment
00:01
Running on runner-fsx9yriu-project-2-concurrent-0 via 926cd5798468...
Getting source from Git repository
00:00
Fetching changes with git depth set to 50...
Initialized empty Git repository in /builds/feiyang/test/.git/
Created fresh repository.
fatal: unable to access 'http://gitlab.feiyang.com/feiyang/test.git/': Could not resolve host: gitlab.feiyang.com
ERROR: Job failed: exit code 1
在这里,我们需要在 runner 里面定义好 domain gitlab.feiyang.com
参考文档: https://docs.gitlab.com/runner/configuration/advanced-configuration.html#the-runnersdocker-section
extra_hosts: Specify hosts that should be defined in container environment1
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[root@centos7 jy576]# docker exec -it gitlab-runner bash
root@926cd5798468:/# cat /etc/gitlab-runner/config.toml
concurrent = 1
check_interval = 0
[session_server]
session_timeout = 1800
[[runners]]
name = "centos7"
url = "http://192.168.17.3/"
token = "FSX9yRiUGxok94hMPYdt"
executor = "docker"
[runners.custom_build_dir]
[runners.cache]
[runners.cache.s3]
[runners.cache.gcs]
[runners.cache.azure]
[runners.docker]
tls_verify = false
image = "python:3.9.1-slim-buster"
privileged = false
disable_entrypoint_overwrite = false
oom_kill_disable = false
disable_cache = false
volumes = ["/cache"]
shm_size = 0
extra_hosts = ["gitlab.feiyang.com:192.168.17.3"]
Test CI
gitlab 和 runner 安装完成以后,我们就可以进行测试
Job artifacts
文档:https://docs.gitlab.com/ee/ci/pipelines/job_artifacts.html#defining-artifacts-in-gitlab-ciyml
test repo 目录结构如下
├── .gitlab-ci.yml
├── lint.py
└── README.md
lint.py1
2
3
4import os
os.chdir(os.path.dirname(__file__))
print("feiyang test CI")
print(os.getcwd())
.gitlab-ci.yml1
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
31stages:
- lint
- build
- deploy
image: python:3.9.1-slim-buster
check_path:
stage: lint
before_script:
- pwd
script:
- cd /builds/$CI_PROJECT_NAMESPACE/$CI_PROJECT_NAME && python lint.py
build_ami:
stage: build
before_script:
- apt update && apt install -y curl
script:
- echo $CI_JOB_ID >> ip.json && curl ipinfo.io >> ip.json
artifacts:
expire_in: 1 year
paths:
- ip.json
read_artifacts:
stage: deploy
before_script:
- pwd
script:
- cat ip.json
CI 结果
lint
1 | Running with gitlab-runner 13.7.0 (943fc252) |
build
1 | Running with gitlab-runner 13.7.0 (943fc252) |
deploy
1 | Running with gitlab-runner 13.7.0 (943fc252) |
总结 artifacts,前一个 stage 生存的文件,以 artifacts 保存下来,给下一个 stage 使用
注意事项
gitlab 自定义 SSH 端口 https://blog.csdn.net/skykingf/article/details/95212146
gitlab container registry authenticate https://docs.gitlab.com/ee/user/packages/container_registry/#authenticate-by-using-gitlab-cicd