1. 拉取Gitlab-Runner镜像
docker pull gitlab/gitlab-runner
2. 创建数据目录
mkdir -p /home/gitlab-runner/config
3. 运行Gitlab-Runner
docker run -d \
--name gitlab-runner \
--restart always \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /home/gitlab-runner/config:/etc/gitlab-runner \
gitlab/gitlab-runner
4. 在Gitlab创建项目gitlab-runner-test
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HuYQj0CM-1635321526440)(https://i.niupic.com/images/2020/10/27/8Vcn.jpg)]
在Settings->CI/CD->Runner
一栏复制token下面要用
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-a5f3rupg-1635321526459)(https://i.niupic.com/images/2020/10/27/8Vcr.jpg)]
5. 注册和初始化Runner
docker exec -it gitlab-runner gitlab-ci-multi-runner register
[root@jfsoft home]# docker exec -it gitlab-runner gitlab-ci-multi-runner register
Runtime platform arch=amd64 os=linux pid=17 revision=ece86343 version=13.5.0
Running in system-mode.
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://10.0.2.104
Please enter the gitlab-ci token for this runner:
piUT51CZSpi8RxefuUzV
Please enter the gitlab-ci description for this runner:
[2a25a0e3d679]: gitlab-runner-demo
Please enter the gitlab-ci tags for this runner (comma separated):
cxc
Registering runner... succeeded runner=piUT51CZ
Please enter the executor: docker, docker-ssh, shell, ssh, virtualbox, docker+machine, docker-ssh+machine, custom, kubernetes, parallels:
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!
注册成功后在Settings->CI/CD->Runner
一栏就可以看到刚才注册的Runner了
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eMvPb7zM-1635321526463)(https://i.niupic.com/images/2020/10/27/8Vcs.jpg)]
6. 定义项目构建流程
java maven项目实例(.gitlab-ci.yml)
image: maven:3-jdk-8
stages:
- build
- test
- deploy
job_build:
stage: build
script:
- mvn clean install
only:
- develop
tags:
- cxc
job_test:
stage: test
script:
- mvn test
tags:
- cxc
job_deploy:
stage: deploy
script:
- echo "deploy over..."
tags:
- cxc
修改gitlab-runner配置文件,使其优先使用本地maven仓库,不然每次构建都会重新下载依赖
[[runners]]
...
[runners.docker]
...
volumes = ["/cache","/home/maven-repo:/root/.m2"]
pull_policy = "if-not-present"
...
重启gitlab-runner
docker restart gitlab-runner