Gitlab Runner 介绍
GitLab Runner是一个开源项目,用于运行您的作业并将结果发送回GitLab。它与GitLab CI一起使用,GitLab CI是GitLab随附的开源持续集成服务,用于协调作业。
GitLab Runner是用Go编写的,可以作为单个二进制文件运行,不需要语言特定的要求。
它旨在运行在GNU / Linux,macOS和Windows操作系统上。只要您可以在其上编译Go二进制文件,其他操作系统就可能正常工作。
如果要使用Docker,请确保v1.5.0至少安装了版本
官网文档:
https://docs.gitlab.com/runner/
安装(二进制手动安装方式)
服务器:192.168.31.31(centos7)
文档:https://docs.gitlab.com/runner/install/linux-manually.html
步骤:
#下载二进制文件,并设置可执行权限
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
sudo chmod +x /usr/local/bin/gitlab-runner
#安装docker,可选,如果执行器选择docker,则需要安装,如果可以,最好装个docker加速器
curl -sSL https://get.docker.com/ | sh
#添加一个普通用户权限,用来运行gitlab runner,然后运行gitlab runner
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
sudo gitlab-runner start
注册gitlab runner
打开gitlab,找到runner ,以及runner的认证网址和随机码
gitlab runner服务器执行
[root@localhost ~]# gitlab-runner register #注册runner到gitlab
Runtime platform arch=amd64 os=linux pid=12351 revision=d0b76032 version=12.0.2
Running in system-mode.
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/): #找到上图的gitlab服务器地址,也就是第二条
http://192.168.31.130/
Please enter the gitlab-ci token for this runner:#输入随机码
yx6oVQYxrLxczyazysF9
Please enter the gitlab-ci description for this runner: #runner 服务器的描述
[localhost.localdomain]: gitlab-runner-01
Please enter the gitlab-ci tags for this runner (comma separated): #runner打tag,在之后执行ci脚本可以通过tags选择runner
gitlab-runner-01
Registering runner... succeeded runner=yx6oVQYx
Please enter the executor: docker, docker-ssh, ssh, docker-ssh+machine, parallels, shell, virtualbox, docker+machine, kubernetes: #设置runner运行方式,这里我上一步安装了docker,我选择docker,也可以选择其他
docker
Please enter the default Docker image (e.g. ruby:2.6):#设置默认的镜像,也就是在ci里面没有写明image默认使用的镜像,这里我写的是centos:7
centos:7
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
[root@localhost ~]# gitlab-runner restart #重启下gitlab runner
Runtime platform arch=amd64 os=linux pid=12359 revision=d0b76032 version=12.0.2
[root@localhost ~]# gitlab-runner list #查看当前gitlab runner
Runtime platform arch=amd64 os=linux pid=12379 revision=d0b76032 version=12.0.2
Listing configured runners ConfigFile=/etc/gitlab-runner/config.toml
gitlab-runner-01 Executor=docker Token=KAsxjVbByKauYnNMSKHY URL=http://192.168.31.130/
备注:gitlab runner配置文件在/etc/gitlab-runner/config.toml
注册完成后,在gitlab上就能看到刚注册的runner
测试
在项目的根目录下新建.gitlab-ci.yaml
.gitlab-ci.yaml内容是(具体含义下次分析)
stages:
- build
- cleanup_build
- test
- deploy
- cleanup
build_job:
stage: build
tags:
- gitlab-runner-01 #这里配置的是gitlab runner注册时候写的tag
script:
- echo build_job
cleanup_build_job:
stage: cleanup_build
tags:
- gitlab-runner-01
script:
- echo cleanup build when failed
when: on_failure
test_job:
stage: test
tags:
- gitlab-runner-01
script:
- echo test
deploy_job:
stage: deploy
tags:
- gitlab-runner-01
script:
- echo deploy
when: manual
cleanup_job:
stage: cleanup
tags:
- gitlab-runner-01
script:
- echo cleanup_job
when: always
可以看到pipline执行完成了具体步骤可以点击jobs查看详细,如果手速快,还在在runner机器上docker ps看到执行过程