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

相关推荐
不吃饭的猪33 分钟前
记一次spark在docker本地启动报错
大数据·docker·spark
藥瓿亭2 小时前
2024 CKA模拟系统制作 | Step-By-Step | 16、题目搭建-sidecar 代理容器日志
linux·运维·docker·云原生·容器·kubernetes·cka
Jay_275 小时前
python项目如何创建docker环境
开发语言·python·docker
---wzy---6 小时前
docker生命周期
java·docker·容器
@t.t.7 小时前
使用Swarm工具搭建docker集群
docker·微服务·容器
局外人LZ8 小时前
Docker轻松搭建Neo4j+APOC环境
docker·容器·neo4j
大数据0018 小时前
Docker慢慢学
mysql·docker·kafka·n8n
唐可盐18 小时前
解决docker运行zentao 报错:ln: failed to create symbolic link ‘/opt/zbox/tmp/mysq
运维·docker·容器
头疼的程序员20 小时前
Jenkins的学习与使用(CI/CD)
ci/cd·jenkins
试剂界的爱马仕21 小时前
软珊瑚成分 CI-A:靶向口腔癌细胞的 “氧化利剑” 与 ERK 密码
网络·人工智能·科技·机器学习·ci/cd·ai写作