-
安装配置 GitLab Runner
在安装配置Runner之前,这里有必要介绍一些名词:
1. Gitlab
GitLab是一个利用Ruby on Rails开发的开源应用程序,实现一个自托管的Git项目仓库,可通过Web界面进行访问公开的或者私人项目。
它拥有与GitHub类似的功能,能够浏览源代码,管理缺陷和注释。可以管理团队对仓库的访问,它非常易于浏览提交过的版本并提供一个文件历史库。团队成员可以利用内置的简单聊天程序(Wall)进行交流。它还提供一个代码片段收集功能可以轻松实现代码复用,便于日后有需要的时候进行查找。
2. Gitlab-CI
Gitlab-CI是GitLab Continuous Integration(Gitlab持续集成)的简称。
从Gitlab的8.0版本开始,gitlab就全面集成了Gitlab-CI,并且对所有项目默认开启。
只要在项目仓库的根目录添加.gitlab-ci.yml
文件,并且配置了Runner(运行器),那么每一次合并请求(MR)或者push都会触发CI pipeline。
3. Gitlab-runner
Gitlab-runner是.gitlab-ci.yml
脚本的运行器,Gitlab-runner是基于Gitlab-CI的API进行构建的相互隔离的机器(或虚拟机)。GitLab Runner 不需要和Gitlab安装在同一台机器上,但是考虑到GitLab Runner的资源消耗问题和安全问题,也不建议这两者安装在同一台机器上。
Gitlab Runner分为两种,Shared runners和Specific runners。
Specific runners只能被指定的项目使用,Shared runners则可以运行所有开启Allow shared runners
选项的项目。
4. Pipelines
Pipelines是定义于.gitlab-ci.yml
中的不同阶段的不同任务。
我把Pipelines理解为流水线,流水线包含有多个阶段(stages),每个阶段包含有一个或多个工序(jobs),比如先购料、组装、测试、包装再上线销售,每一次push或者MR都要经过流水线之后才可以合格出厂。而.gitlab-ci.yml
正是定义了这条流水线有哪些阶段,每个阶段要做什么事。
5. Badges
徽章,当Pipelines执行完成,会生成徽章,你可以将这些徽章加入到你的README.md文件或者你的网站。
在了解了这些名词后,我们开始安装 Runner:
# For Debian/Ubuntu/Mint
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash
# For RHEL/CentOS/Fedora
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | sudo bash
yum替换国内镜像:
[runner_gitlab-runner]
name=runner_gitlab-runner
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-runner/yum/el7
repo_gpgcheck=1
gpgcheck=1
enabled=1
gpgkey=https://packages.gitlab.com/runner/gitlab-runner/gpgkey
https://packages.gitlab.com/runner/gitlab-runner/gpgkey/runner-gitlab-runner-366915F31B487241.pub.gpg
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
[runner_gitlab-runner-source]
name=runner_gitlab-runner-source
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-runner/yum/el7
repo_gpgcheck=1
gpgcheck=1
enabled=1
gpgkey=https://packages.gitlab.com/runner/gitlab-runner/gpgkey
https://packages.gitlab.com/runner/gitlab-runner/gpgkey/runner-gitlab-runner-366915F31B487241.pub.gpg
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
生成缓存:
sudo yum makecache
安装:
# For Debian/Ubuntu/Mint
sudo apt-get install gitlab-runner
# For RHEL/CentOS/Fedora
sudo yum install gitlab-runner
安装完成后,还需要把 Runner 注册到 gitlab 上,才可以正常使用:
#输入下行命令:
sudo gitlab-runner register
#输入你的 gitlab 实例地址
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com )
https://gitlab.com
#输入 gitlab-ci token
#token是为了确定你这个Runner是所有工程都能够使用的Shared Runner
#还是具体某一个工程才能使用的Specific Runner
#如果要注册Shared Runner,你需要到管理界面的Runners页面里面去找注册token。
Please enter the gitlab-ci token for this runner
xxx
#输入runner的描述,你可以稍后在GitLab的UI中修改:
Please enter the gitlab-ci description for this runner
[hostname] my-runner
#输入与Runner相关的标签,你可以稍后在GitLab的UI中修改:
Please enter the gitlab-ci tags for this runner (comma separated):
my-tag,another-tag
#输入Runner的执行器类型
Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
kubernetes
#配置成功
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
#或者直接一条命令:
sudo gitlab-runner register \
--non-interactive \
--url "http://10.30.80.105:8088/" \
--registration-token "xTxZeHWv9QMXznUTwsZP" \
--executor "shell" \
--description "shell-runner" \
--run-untagged \
--locked="false" \
-- tag "java"
在gitlab上可以看到Runner创建成功:
全部安装完成后,需要将docker服务,添加给用户:
usermod -aG docker gitlab-runner
sudo service docker restart
如果你需要删除runner或者解绑runner:
#解绑所有runner
sudo gitlab-runner unregister --all-runners
# 如果你在gitlab上删除了 runner,需要手动逐个删除runner:
sudo gitlab-runner list
gitlab-runner verify --delete --token Token --url URL
如果出现添加的runner 没有连接上gitlab,last contact 一栏出现never的情况:
网友评论