东西有点多,自己没有整理,我是通过下面几篇文章学习的,先了解了解
Docker搭建自己的Gitlab CI Runner
GitLab CI介绍——入门篇
GitLab CI/CD 流水线配置参考文档
docker容器搭建----gitlab-runner自动化部署
远哥推荐:gitlab-ci.yml 配置Gitlab pipeline以达到持续集成的方法
1.gitlab ci 介绍
CI:持续集成,我们通常使用CI来做一些自动化工作,比如程序的打包,单元测试,部署等,这种构建方式避免了打包环境差异引起的错误,提高了工作效率。Gitlab-CI是Gitlab官方提供的持续集成服务,我们可以在仓库的根目录下新建.gitlab-ci.yml文件,自己定义持续集成流程模板,并且在Gitlab中配置runner,在之后的每次提交合并中将会触发构建,并且可以通过Gitlab的hook, 在代码提交的各个环节自动地完成一系列的构建工作,总之对于一些非复杂性的集成需求,都是可以满足的。
2.环境软件准备
1.Ubuntu:18.04
2.gitlab: twang2218/gitlab-ce-zh:10.5.4
3.gitlab-runner: gitlab-runner:lastest
3.安装、注册并启动gitlab-runner(基于docker)
(1)拉取gitlab-runner镜像
sudo docker pull gitlab/gitlab-runner:latest
(2)添加gitlab-runner 容器(注意不要加–rm否则重启后会删掉容器)
xqkang@xqkang:~/docker$ docker run -t -i --name gitlab-runner -d -v /var/run/docker.sock:/var/run/docker.sock -v /srv/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner
14dfd1f6efe865fa0ca0bca4eb129716101e04bcdeec4e65fc57988850e84d37
(3)注册runner
xqkang@xqkang:~/docker$ docker exec -it gitlab-runner gitlab-runner register
Runtime platform arch=amd64 os=linux pid=24 revision=577f813d version=12.5.0
Running in system-mode.
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://192.168.10.46/
Please enter the gitlab-ci token for this runner:
btydk3dKPWRKNqDTFGbe
Please enter the gitlab-ci description for this runner:
[14dfd1f6efe8]: qd_api_runner
Please enter the gitlab-ci tags for this runner (comma separated):
kxq
Registering runner... succeeded runner=btydk3dK
Please enter the executor: docker-ssh+machine, custom, docker, docker-ssh, parallels, ssh, shell, virtualbox, docker+machine, kubernetes:
docker
Please enter the default Docker image (e.g. ruby:2.6):
maven:3-jdk-8
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
说明:
1、gitlab ci的地址以及token,从你要配置该runner到哪个项目,就去gitlab下该项目首页右侧设置—》CI/CD—》Runners下可以找到。
2、gitlab-ci tags这个很重要,在项目构建流程yaml文件里面指定tag,就是匹配使用哪个tag的runner,这里我定义了kxq,回头再配置文件里面就指定这个tag。
3、executor:执行者可以有很多种,这里我们使用docker,方便构建执行。
4、Docker image:构建Docker image时填写的image名称,根据项目代码语言不同,指定不同的镜像。我这里项目是java语言的,所以我使用官方maven:3-jdk-8镜像。
注册之后需要改一下配置文件:
sudo vim /srv/gitlab-runner/config
[[runners]]
name = "docker"
url = "http://192.168.10.46/"
token = "1abec55c039f2cf13e8f3c35186e9e"
executor = "docker"
[runners.custom_build_dir]
[runners.docker]
tls_verify = false
image = "docker"
privileged = false
disable_entrypoint_overwrite = false
oom_kill_disable = false
disable_cache = false
volumes = ["/cache","/var/run/docker.sock:/var/run/docker.sock"]
shm_size = 0
[runners.cache]
[runners.cache.s3]
[runners.cache.gcs]
volumes = ["/cache","/var/run/docker.sock:/var/run/docker.sock"]
为什么添加这行命令,因为gitlab-runner这个容器运行后会启动runner-work这个容器才能进行自动部署,而runner-work和外部通信需要物理机的/var/run/docker.sock
具体详解:https://blog.csdn.net/boling_cavalry/article/details/92846483
4.定义项目构建流程:
项目的构建流程是由项目根目录的.gitlab-ci.yml文件控制的,关于gitlab-ci详细的配置文档可以查看 官方文档, 以下是一个简单的Java Maven项目的例子.gitlab-ci.yml:
image: maven:3-jdk-8
test:
stage: test
script:
- mvn test
tags:
- kxq
only
- master
我是通过公司的原有项目拉取并上传的,所以直接在gitlab文件中修改的。
期间遇到了一些问题:
CI/CD流水线一直处于等待中…
所以现在需要在终端查看日志:
xqkang@xqkang:~/git_demo/inventory_control_java$ docker logs gitlab-runner
Runtime platform arch=amd64 os=linux pid=7 revision=577f813d version=12.5.0
Starting multi-runner from /etc/gitlab-runner/config.toml ... builds=0
Running in system-mode.
Configuration loaded builds=0
Locking configuration file builds=0 file=/etc/gitlab-runner/config.toml pid=7
listen_address not defined, metrics & debug endpoints disabled builds=0
[session_server].listen_address not defined, session endpoints disabled builds=0
Configuration loaded builds=0
Checking for jobs... received job=18 repo_url=http://192.168.10.46/root/third_project.git runner=bda9456a
WARNING: Job failed: exit code 1 duration=7m2.607598667s job=18 project=3 runner=bda9456a
WARNING: Failed to process runner builds=0 error=exit code 1 executor=docker runner=bda9456a
发现Job failed: exit code 1 , Failed to process runner
然后开启了debug模式:
xqkang@xqkang:~/git_demo/inventory_control_java$ docker exec -it gitlab-runner gitlab-runner --debug run
最后一行显示文件被锁:
FATAL: Could not handle configuration file locking builds=0 error=file "/etc/gitlab-runner/config.toml.lock" is locked by another process file=/etc/gitlab-runner/config.toml pid=47
于是删除lock锁的文件(通过docker inspect gitlab-runner 查找配置的地址):
cd /srv/gitlab-runner/config
rm -rf config.toml.lock
这次就可以用了,流水线顺利的通过了。哈哈哈哈哈哈。。。。
后来又出现被锁住了,然后在崔的指导下
修改了/srv/gitlab-runner/config/config.toml文件,修改如下:
volumes = ["/cache","/var/run/docker.sock:/var/run/docker.sock"]