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

相关推荐
努力买辣条3 小时前
基于 Docker 的高可用 WordPress 集群部署:分布式 Nginx + Keepalived、MySQL 主从复制与 ProxySQL 读写分离
分布式·nginx·docker
CTRA王大大7 小时前
【golang】制作linux环境+golang的Dockerfile | 如何下载golang镜像源
linux·开发语言·docker·golang
@寄居蟹11 小时前
Docker 命令大全
docker·容器·eureka
运维开发王义杰12 小时前
GitLab CI:Auto DevOps 全解析,告别繁琐配置,拥抱自动化未来
ci/cd·gitlab·devops
运维开发王义杰12 小时前
GitLab CI:深度解析 Runner的Shell与Docker执行器
ci/cd·docker·gitlab
qq_3643717213 小时前
Docker 常见命令
运维·docker·容器
小白不想白a19 小时前
【K8s】整体认识K8s之Docker篇
docker·容器·kubernetes
Agome9919 小时前
Docker之nginx安装
java·nginx·docker
努力买辣条1 天前
基于Docker的高可用WordPress集群部署:Nginx负载均衡+Mysql主从复制+ProxySQL读写分离
nginx·docker·负载均衡
记忆不曾留1 天前
unbuntu 20.04 docker 部署wordpress
运维·docker·容器·wordpress·独立站建站