Gitlab-Runner配置

原理

Gitlab-Runner是一个非常强大的CI/CD工具。它可以帮助我们自动化执行各种任务,如构建、测试和部署等。Gitlab-Runner和Gitlab通过API通信,接收作业并提交到执行队列,Gitlab-Runner从队列中获取作业,并允许在不同环境下进行作业。

安装和注册

  1. 安装:
shell 复制代码
yum install gitlab-runner
  1. 注册:

首先获取gitlab的token,以供gitlab-runner注册时使用,Gitlab runner 分 3 类,注册在不同位置runner权限也不同:

    1. Share Runner(推荐使用):所有 group 的所有 project 都能使用;
    1. Group Runner: 该 Group 内的所有 Project 都可以使用;
    1. Specific Runner:一个 Project 独享的 Runner,其他项目默认不能使用;

token位置gitlab → group/project → settings → cicd → runners

注册runner,根据提示依次配置以下参数

shell 复制代码
gitlab-runner register 

Please enter the gitlab-ci coordinator URL (e.g. <https://gitlab.com/>): 
https\://****** 

Please enter the gitlab-ci token for this runner: 
VyaV8t******  

Please enter the gitlab-ci description for this runner: 
test runner 

Please enter the gitlab-ci tags for this runner (comma separated): 
test 

Whether to run untagged builds \[true/false]: \[false]: 
true 

Whether to lock the Runner to current project \[true/false]: \[true]: 
true 

Registering runner... succeeded runner=qtWs5Db6 

Please enter the executor: shell, ssh, docker+machine, docker, docker-ssh, parallels, virtualbox, docker-ssh+machine, docker: 
shell 

Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded! 
  • 第一个参数输入gitlab-url
  • 第二个参数输入token
  • 第三个参数输入描述
  • 第四个参数输入标签tag
  • 第五个参数选择执行的命令之类的,可以根据自身需求选择。

*executor执行器

在注册runner的最后一步提示了选择所需的执行器,不同执行器对ci的影响很大,简单介绍一下runner的执行器

执行器 作业运行 说明
shell 默认执行器 意味着所有job都在runner上运行
docker docker容器 job将在docker容器运行
docker-windows Windows Docker 容器
docker-ssh Docker 容器,使用 SSH 连接
ssh 远程SSH
parallels Parallels VM,使用 SSH 连接 一种虚机
virtualbox VirtualBox VM,但使用 SSH 连接 一种虚机
docker+machine 类似docker,但使用自动缩放的 Docker 机器
docker-ssh+machine 类似docker-ssh,但使用自动缩放的 Docker 机器
kubernetes Kubernetes pods

.gitlab-ci.yml配置

举个简单的例子,主要为了展示.gitlab-ci.yml如何让gitlab与runner联动

shell 复制代码
# This file is a template, and might need editing before it works on your project. 

stages: 
    - hello 
    - build 
    
sayhello: 
    tags: 
        - test 
    stage: hello 
    script: 
        - echo hello world 
    
compile: 
    tags: 
        - test 
    stage: build 
    script: 
        - bash build.sh

以上配置文件设置了hello和build两个步骤,tags指定用哪个runner运行ci。git接收到代码push,根据ci规则建立Pipelines向runner发起job,runner接收job开始根据设置的步骤依此执行。

第一步输出 hello world

第二步执行项目中的build.sh脚本

相关推荐
亲持红叶1 小时前
open-webui安装
docker·open-webui
伪装成塔的小兵1 小时前
Windows使用docker部署fastgpt出现的一些问题
windows·docker·容器·oneapi·fastgpt
深情不及里子5 小时前
纯手工搭建整套CI/CD流水线指南
ci/cd·gitlab·jenkins·harbor
AirtestProject5 小时前
Airtest与持续集成(CI)工具的集成实操案例
自动化测试·ci/cd·自动化·网易airtest
转身後 默落5 小时前
11.Docker 之分布式仓库 Harbor
分布式·docker·容器
菩提云5 小时前
Deepseek存算分离安全部署手册
人工智能·深度学习·安全·docker·容器
c无序12 小时前
Docker-技术架构演进之路
docker
努力的小T16 小时前
使用 Docker 部署 Apache Spark 集群教程
linux·运维·服务器·docker·容器·spark·云计算
AliCloudROS17 小时前
阿里云ACK+GitLab企业级部署实战教程
k8s·gitlab·helm·ack·计算巢
东风微鸣17 小时前
TTRSS 迁移实战
docker·云原生·kubernetes·可观察性