在您自己的服务器上设置 GitLab CI Runner
从我们之前的文章中,我们介绍了如何在 GitLab 上设置基本 CI 流水线,并结合 GitLab CI 来协调您的作业,其中我们使用了共享运行器,该运行器在 GitLab 的基础架构上运行您的作业。
在 GitLab 中,您可以使用共享运行器,也可以运行自己的运行器,用于运行作业并将结果发送回 GitLab。
在本教程中,我们将在 Ubuntu 上使用 gitlab-runner 和 Docker 设置服务器,然后设置一个基本流水线来利用您的 Gitlab Runner。
相关帖子
- 在 GitLab 上搭建一个基本的 CI 流水线
- 设置 GitLab CI/CD 流水线,将您的 Python API 部署到 Heroku。
- 在您自己的服务器上设置 GitLab CI Runner(本文)
配置 Docker
安装 Docker:
$ sudo apt update && sudo apt upgrade -y
$ sudo apt-get install apt-transport-https ca-certificates curl software-properties-common -y
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
$ sudo apt update
$ sudo apt install docker-ce -y
$ docker run hello-world
安装和设置 GitLab Runner
此设置适用于 64 位 Linux 系统,对于其他发行版,请查阅其文档。
安装运行程序:
$ wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
$ chmod +x /usr/local/bin/gitlab-runner
$ useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
$ gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
$ gitlab-runner start
注册 Runner。Gitlab-CI Token 可在 UI 的 CI/CD 设置面板中找到:https://gitlab.com/<account>/<repo>/settings/ci_cd
$ gitlab-runner register
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
https://gitlab.com/
Please enter the gitlab-ci token for this runner:
__masked__
Please enter the gitlab-ci description for this runner:
[my-runner]: my-runner
Please enter the gitlab-ci tags for this runner (comma separated):
my-runner,foobar
Registering runner... succeeded runner=66m_339h
Please enter the executor: docker-ssh+machine, docker, docker-ssh, parallels, shell, ssh, virtualbox, docker+machine, kubernetes:
docker
Please enter the default Docker image (e.g. ruby:2.1):
alpine:latest
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
验证状态并检查 Docker 和 GitLab Runner 是否在启动时启用:
$ gitlab-runner status
Runtime platform arch=amd64 os=linux pid=30363 revision=7f00c780 version=11.5.1
gitlab-runner: Service is running!
$ systemctl is-enabled gitlab-runner
enabled
$ systemctl is-enabled docker
enabled
共享运行器的 GitLab-CI 配置
如果您想使用 GitLab 提供的共享运行器,.gitlab-ci.yml配置将如下所示:
stages:
- build
- test
build:
stage: build
script:
- echo "this is building"
- hostname
- mkdir builds
- touch builds/data.txt
- echo "true" > builds/data.txt
artifacts:
paths:
- builds/
test:
stage: test
script:
- echo "this is testing"
- hostname
- test -f builds/data.txt
- grep "true" builds/data.txt
适用于您自己的 GitLab Runner 的 GitLab-CI 配置
GitLab 会利用注册时指定的标签来确定作业的执行位置,更多信息请参阅其文档。
.gitlab-ci.ymlGitLab Runner 的配置:
stages:
- build
- test
build:
stage: build
tags:
- my-runner
script:
- echo "this is building"
- hostname
- mkdir builds
- touch builds/data.txt
- echo "true" > builds/data.txt
artifacts:
paths:
- builds/
test:
stage: test
tags:
- my-runner
script:
- echo "this is testing"
- hostname
- test -f builds/data.txt
- grep "true" builds/data.txt
触发并检查 Docker
将配置提交到主分支,让你的流水线运行其作业,完成后查看服务器上的 Docker 容器,了解作业运行的容器:
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
04292a78de0b c04b8be95e1e "gitlab-runner-cache.." About a minute ago Exited (0) About a minute ago runner-xx-project-xx-concurrent-0-cache-3cxx0
49b1b3c4adf9 c04b8be95e1e "gitlab-runner-cache.." About a minute ago Exited (0) About a minute ago runner-xx-project-xx-concurrent-0-cache-6cxxa
422b23191e8c hello-world "/hello" 24 minutes ago Exited (0) 24 minutes ago wizardly_meninsky
我们知道每个作业都在不同的容器中执行,从上面的输出可以看出,我们管道中指定的 2 个作业分别在 2 个不同的容器中执行。
资源:
谢谢
请告诉我你的想法。如果你喜欢我的内容,欢迎访问我的网站ruan.dev或在推特上关注我@ruanbekker。
文章来源:https://dev.to/ruanbekker/setup-a-gitlab-ci-runner-on-your-own-server-4p2j

