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

相关推荐
木亦汐丫2 小时前
Docker 镜像版本Alpine、Slim、Bookworm、Bullseye、Stretch、Jessie
运维·docker·容器·debian·alpine·slim·bullseye
中草药z4 小时前
【Docker】零基础上手:原理+Ubuntu/Windows GUI 安装 + 镜像源 / 目录优化
运维·ubuntu·docker·容器·gui·安装·cgroups
勇往直前plus4 小时前
如何利用docker部署springboot应用
spring boot·docker·容器
一张假钞6 小时前
Jenkins 项目迁移
ci/cd·jenkins
有谁看见我的剑了?7 小时前
k8s storageclasses nfs-provisioner 部署
docker·容器·kubernetes
忧郁的橙子.8 小时前
安装插件遇到报错时间错误导致签证失败 Errors during downloading metadata for repository ‘baseos‘:
docker
超龄超能程序猿8 小时前
Docker 镜像的导出和还原操作
运维·docker·容器
维尔切8 小时前
Docker 监控平台部署
运维·docker·容器·grafana·prometheus
温柔一只鬼.9 小时前
Docker快速入门——第三章Docker环境安装
运维·docker·容器
红尘客栈29 小时前
k8s的ymal文件
docker·容器·kubernetes