Gitlab Runner安装和简单使用
1. 安装和配置
-
GitLab-CI
GitLab-CI就是一套配合GitLab使用的持续集成系统(当然,还有其它的持续集成系统,同样可以配合GitLab使用,比如Jenkins)。而且GitLab8.0以后的版本是默认集成了GitLab-CI并且默认启用的。
-
GitLab-Runner
GitLab-Runner是配合GitLab-CI进行使用的。一般地,GitLab里面的每一个工程都会定义一个属于这个工程的软件集成脚本,用来自动化地完成一些软件集成工作。当这个工程的仓库代码发生变动时,比如有人push了代码,GitLab就会将这个变动通知GitLab-CI。这时GitLab-CI会找出与这个工程相关联的Runner,并通知这些Runner把代码更新到本地并执行预定义好的执行脚本。
所以,GitLab-Runner就是一个用来执行软件集成脚本的东西。你可以想象一下:Runner就像一个个的工人,而GitLab-CI就是这些工人的一个管理中心,所有工人都要在GitLab-CI里面登记注册,并且表明自己是为哪个工程服务的。当相应的工程发生变化时,GitLab-CI就会通知相应的工人执行软件集成脚本。如下图所示: -
Runner类型
GitLab-Runner可以分类两种类型:Shared Runner(共享型)和Specific Runner(指定型)。
Shared Runner:这种Runner是所有工程都能够用的。只有系统管理员能够创建Shared Runner。
Specific Runner:这种Runner只能为指定的工程服务。拥有该工程访问权限的人都能够为该工程创建Shared Runner。 -
CentOs中GitLab-Runner的安装与使用
[root@middleware ~]# curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.rpm.sh | sudo bash
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 7012 100 7012 0 0 4636 0 0:00:01 0:00:01 --:--:-- 4640
Detected operating system as centos/7.
Checking for curl...
Detected curl...
Downloading repository file: https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/config_file.repo?os=centos&dist=7&source=script
done.
Installing pygpgme to verify GPG signatures...
已加载插件:fastestmirror
Determining fastest mirrors
* base: mirrors.163.com
* extras: ftp.sjtu.edu.cn
* updates: ftp.sjtu.edu.cn
base | 3.6 kB 00:00:00
docker-ce-stable | 3.5 kB 00:00:00
extras | 2.9 kB 00:00:00
runner_gitlab-ci-multi-runner-source/signature | 862 B 00:00:00
从 https://packages.gitlab.com/runner/gitlab-ci-multi-runner/gpgkey 检索密钥
导入 GPG key 0x51312F3F:
用户ID : "GitLab B.V. (package repository signing key) <packages@gitlab.com>"
指纹 : f640 3f65 44a3 8863 daa0 b6e0 3f01 618a 5131 2f3f
来自 : https://packages.gitlab.com/runner/gitlab-ci-multi-runner/gpgkey
runner_gitlab-ci-multi-runner-source/signature | 951 B 00:00:00 !!!
updates | 2.9 kB 00:00:00
(1/3): extras/7/x86_64/primary_db | 236 kB 00:00:00
(2/3): docker-ce-stable/x86_64/primary_db | 60 kB 00:00:00
(3/3): updates/7/x86_64/primary_db | 8.0 MB 00:00:01
runner_gitlab-ci-multi-runner-source/primary | 175 B 00:00:02
软件包 pygpgme-0.3-9.el7.x86_64 已安装并且是最新版本
无须任何处理
Installing yum-utils...
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.163.com
* extras: ftp.sjtu.edu.cn
* updates: ftp.sjtu.edu.cn
软件包 yum-utils-1.1.31-54.el7_8.noarch 已安装并且是最新版本
无须任何处理
Generating yum cache for runner_gitlab-ci-multi-runner...
导入 GPG key 0x51312F3F:
用户ID : "GitLab B.V. (package repository signing key) <packages@gitlab.com>"
指纹 : f640 3f65 44a3 8863 daa0 b6e0 3f01 618a 5131 2f3f
来自 : https://packages.gitlab.com/runner/gitlab-ci-multi-runner/gpgkey
https://packages.gitlab.com/runner/gitlab-ci-multi-runner/el/7/x86_64/repodata/42a81582ab8f41cfc311ccaae009dd552d2e372e-primary.xml.gz: [Errno 12] Timeout on https://d20rj4el6vkp4c.cloudfront.net/8/10/el/7/x86_64/repodata/42a81582ab8f41cfc311ccaae009dd552d2e372e-primary.xml.gz?t=1619768494_ee846eb88bd7cd72a2983cae76f24034b201ca4b: (28, 'Operation timed out after 30790 milliseconds with 0 out of 0 bytes received')
正在尝试其它镜像。
Generating yum cache for runner_gitlab-ci-multi-runner-source...
The repository is setup! You can now install packages.
[root@middleware ~]# yum install gitlab-ci-multi-runner
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.163.com
* extras: ftp.sjtu.edu.cn
* updates: ftp.sjtu.edu.cn
正在解决依赖关系
--> 正在检查事务
---> 软件包 gitlab-ci-multi-runner.x86_64.0.9.5.1-1 将被 安装
--> 解决依赖关系完成
依赖关系解决
==================================================================================================================================================================================================
Package 架构 版本 源 大小
==================================================================================================================================================================================================
正在安装:
gitlab-ci-multi-runner x86_64 9.5.1-1 runner_gitlab-ci-multi-runner 24 M
事务概要
==================================================================================================================================================================================================
安装 1 软件包
总下载量:24 M
安装大小:46 M
Is this ok [y/d/N]: y
Downloading packages:
gitlab-ci-multi-runner-9.5.1-1.x86_64.rpm | 24 MB 00:00:08
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : gitlab-ci-multi-runner-9.5.1-1.x86_64 1/1
GitLab Runner: creating gitlab-runner...
验证中 : gitlab-ci-multi-runner-9.5.1-1.x86_64 1/1
已安装:
gitlab-ci-multi-runner.x86_64 0:9.5.1-1
完毕!
[root@middleware ~]# gitlab-ci-multi-runner register
Running in system-mode.
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://192.168.1.106:82/
Please enter the gitlab-ci token for this runner:
CdW2YBWzzDZseoybq89Y
Please enter the gitlab-ci description for this runner:
[middleware]: dev
Please enter the gitlab-ci tags for this runner (comma separated):
dev
Whether to run untagged builds [true/false]:
[false]: false
Whether to lock Runner to current project [true/false]:
[false]: true
Registering runner... succeeded runner=CdW2YBWz
Please enter the executor: docker, shell, ssh, kubernetes, docker-ssh, parallels, virtualbox, docker+machine, docker-ssh+machine:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
[root@middleware ~]# gitlab-runner list
Listing configured runners ConfigFile=/etc/gitlab-runner/config.toml
dev Executor=shell Token=9RWCR_KAQs7wZAPA7uyp URL=http://192.168.1.106:82/
[root@middleware ~]# gitlab-runner list
Listing configured runners ConfigFile=/etc/gitlab-runner/config.toml
dev Executor=shell Token=9RWCR_KAQs7wZAPA7uyp URL=http://192.168.1.106:82/
[root@middleware ~]#
其中有一个步骤要输入项目的token。
配置完成后,runners里面会出现刚才配置的选项:
2. 使用
项目根目录添加文件:.gitlab-ci.yml
内容如下:
stages:
- dev
dev-job:
stage: dev
tags:
- dev
only:
- master
script:
- echo 'hello runner'
提交后,会自动触发部署脚本
部署日志如下: