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脚本

相关推荐
B1nna2 小时前
Docker学习
学习·docker·容器
缘友一世2 小时前
网安系列【15】之Docker未授权访问漏洞
docker·容器
慕木兮人可9 小时前
Docker部署MySQL镜像
spring boot·后端·mysql·docker·ecs服务器
armcsdn13 小时前
基于Docker Compose部署Traccar容器与主机MySQL的完整指南
mysql·docker·容器
凌晨两点的菜鸡13 小时前
前端部署-docker
前端·docker
LuckyLay14 小时前
使用 Docker 搭建 Go Web 应用开发环境——AI教你学Docker
前端·docker·golang
杨浦老苏15 小时前
IntelliSSH:AI驱动的远程服务器管理软件
人工智能·docker·ai·ssh·群晖
铅笔侠_小龙虾15 小时前
Docker 实战 -- Mysql
mysql·docker·容器
IvanCodes15 小时前
三、Docker常用命令
docker·容器
海星船长丶17 小时前
基于docker进行渗透测试环境的快速搭建(在ubantu中docker设置代理)
运维·docker·容器