概述
Gitlab-Runner是Gitlab CI/CD的执行者,gitlab-runner又有很多种类型(就是所谓的executor):
- SSH
- Shell
- Parallels
- VirtualBox
- Docker
- Docker Machine (auto-scaling)
- Kubernetes
- Custom
既然有这么多类型,为什么选docker呢???
Executor | SSH | Shell | VirtualBox | Parallels | Docker | Kubernetes | Custom |
---|---|---|---|---|---|---|---|
Clean build environment for every build | ✗ | ✗ | ✓ | ✓ | ✓ | ✓ | conditional (4) |
Reuse previous clone if it exists | ✓ | ✓ | ✗ | ✗ | ✓ | ✗ | conditional (4) |
Runner file system access protected (5) | ✓ | ✗ | ✓ | ✓ | ✓ | ✓ | conditional |
Migrate runner machine | ✗ | ✗ | partial | partial | ✓ | ✓ | ✓ |
Zero-configuration support for concurrent builds | ✗ | ✗ (1) | ✓ | ✓ | ✓ | ✓ | conditional (4) |
Complicated build environments | ✗ | ✗ (2) | ✓ (3) | ✓ (3) | ✓ | ✓ | ✓ |
Debugging build problems | easy | easy | hard | hard | medium | medium | medium |
如上表所示,docker无疑是最佳选择。
其实是之前一直用的shell模式,想试试docker好用不,用过之后真香
安装
1. 准备镜像
根据gitlab server 的版本pull对应版本的gitlab-runner的镜像,镜像版本号查看地址
// 镜像版本根据自己的gitlab版本而定
sudo docker pull gitlab/gitlab-runner:v13.10.0
// docker in docker 的镜像,根据本地的docker版本来拉去相应的版本
sudo docker pull docker:20.10.5-dind
2. 运行容器
// 创建文件夹来放配置文件以及公共的缓存文件,到时候会映射进去
sudo mkdir /var/gitlab-runner /var/gitlab-runner/config /var/gitlab-runner/.nuget /var/gitlab-runner/.m2 /var/gitlab-runner/.npm
// 运行gitlab-runner
sudo docker run -d --name gitlab-runner --restart always \
-v /var/gitlab-runner/config:/etc/gitlab-runner \
-v /var/run/docker.sock:/var/run/docker.sock \
gitlab/gitlab-runner:v13.10.0
3. 注册gitlab-runner
// 运行注册容器
sudo docker run --rm -it -v /var/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner register -n \
// gitlab 地址
--url https://gitlab.com/ \
// token
--registration-token REGISTRATION_TOKEN \
// executor 固定是docker
--executor docker \
// runner的描述
--description "auto build image" \
// docker in docker模式中使用的docker的镜像,根据本地的docker的版本选择,我本地的docker版本是20.10.5所以docker-image选的是docker:20.10.5-dind
--docker-image "docker:20.10.5-dind" \
--docker-volumes /var/run/docker.sock:/var/run/docker.sock
// .net nuget 包全局缓存
--docker-volumes /var/gitlab-runner/.nuget:/root/.nuget
// java maven 包全局缓存
--docker-volumes /var/gitlab-runner/.m2:/root/.m2
// js npm 包全局缓存
--docker-volumes /var/gitlab-runner/.npm:/root/.npm
执行完成后关闭容器即可。
4. 重启gitlab runner
sudo docker restart gitlab-runner
成功后会在/var/gitlab-runner/config
目录下生成一个config.toml
,并且在gitlab的Admin Area -> Runners界面看到注册成功的runner.
大功告成!!!
参考网址: