Gitlab runner持续集成CI/CD
- 环境查看
系统环境
# cat /etc/redhat-release
Rocky Linux release 9.4 (Blue Onyx)
软件环境
# git version
git version 2.43.5
- 安装gitlab-runner
安装runner
在客户端安装
点击此处获取安装帮助
# Download the binary for your system
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
# Give it permission to execute
sudo chmod +x /usr/local/bin/gitlab-runner
# Create a GitLab Runner user
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
# Install and run as a service
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
sudo gitlab-runner start
在一个项目中
创建runner
设置-CI/CD-Runner
需要输入以下信息
查看配置
# cat /etc/gitlab-runner/config.toml
concurrent = 1
check_interval = 0
connection_max_age = "15m0s"
shutdown_timeout = 0
[session_server]
session_timeout = 1800
[[runners]]
name = "testci"
url = "http://192.168.3.81"
id = 1
token = "glrt-VLQtayr8CevuWjg1-D3r"
token_obtained_at = 2024-10-17T07:21:36Z
token_expires_at = 0001-01-01T00:00:00Z
executor = "shell"
[runners.custom_build_dir]
[runners.cache]
MaxUploadedArchiveSize = 0
[runners.cache.s3]
[runners.cache.gcs]
[runners.cache.azure]
设置成功在gitlab查看
取消注册
获取名称或者token
# gitlab-runner list
Runtime platform arch=amd64 os=linux pid=2453371 revision=b92ee590 version=17.4.0
Listing configured runners ConfigFile=/etc/gitlab-runner/config.toml
testci Executor=shell Token=glrt-ajwZ3Tgfxjy6tXbA2hgd URL=http://192.168.3.215
通过名称或者token取消注册
# 通过name取消注册
# gitlab-runner unregister --name testci
# 通过token取消注册
# gitlab-runner unregister --token glrt-ajwZ3Tgfxjy6tXbA2hgd
- 测试
在项目跟目录下创建流水线配置文件
# cat .gitlab-ci.yml
stages:
- build
- test
- deploy
build:
stage: build
script:
- echo "Build"
test:
stage: test
script:
- echo Test
deploy:
stage: deploy
script:
- echo Deploy
提交
# git add .gitlab-ci.yml
# git commit -m "add gitlab-ci file"
# git push
提交后运行会阻塞
因为没有标签
设置不需要标签运行
勾选此处
查看流水线已经运行了
查看运行作业情况
排错
如果有以下情况则会出现鉴权错误导致流水线无法运行
gitlab服务器端反向代理https但是gitlab外网地址设置成了http而不是https
解决方法
- gitlab外网地址设置为https
该方法会导致外网无法下载大文件 参考https://www.cnblogs.com/minseo/p/18294618 - 修改hosts使用http访问
- 群组runner
以上设置为项目runner需要一个个项目进行设置,下面设置群组runner可以在群组运行
在浏览器打开管理中心,可以在gitlab地址后加admin进入管理中心
设置步骤和前面项目内设置步骤一致
CI/CD-Runner-新建实例Runner
在项目查看共享runner
编辑.gitlab-ci.yml 测试步骤和上面项目测试步骤是一样的