约 3 分钟阅读
DevOps

Gitlab Runner详解

Gitlab Runner的安装,使用,FAQ,更新等

Gitlab Runner详解
SYSTEM更新于 May 17, 2024
220 次阅读约 3 分钟阅读
专栏系列Gitlab 指南
本文目录大纲

Gitlab Runner说明

1. 安装

Linux amd64为例

PLAINTEXT
# 下载二进制文件
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64

# 赋权
sudo chmod +x /usr/local/bin/gitlab-runner

# 创建gitlab-runner用户
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash

# 安装成一个服务
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
sudo gitlab-runner start

gitlab runner的配置文件在/etc/gitlab-runner/config.toml下

TOML
concurrent = 10	# 并发运行的数量,这里修改为10,0不代表无限制,必须为数值
check_interval = 0

[session_server]
  session_timeout = 1800

[[runners]]
  name = "centos8.3 runner"	# runner名字
  url = "https://gitlab.xxx.com/"	# gitlab服务器地址
  token = "gJNJCpRp_-Zobf19BDA5"	# token
  executor = "docker"
  [runners.custom_build_dir]
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
    [runners.cache.azure]
  [runners.docker]
    tls_verify = false
    image = "docker:20.10.5"	# 不指定时默认的docker镜像
    privileged = false
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/var/run/docker.sock:/var/run/docker.sock", "/cache"]	# 配置使用的宿主机的docker以让容器内部可以直接使用docker命令
    shm_size = 0

2. Gitlab Runner使用详解

Runner的执行时序图

sequenceDiagram
	participant GitLab
	participant GitLabRunner
	participant Executor
	opt registration
		GitLabRunner->>GitLab: POST /api/v4/runners with registration_token
		activate GitLab
		GitLab-->>GitLabRunner: Registered with runner_token
		deactivate GitLab
	end
	loop job requesting and handling
		GitLabRunner->>GitLab: POST /api/v4/jobs/request with runner_token
		GitLab-->>GitLabRunner: job payload with job_token
		activate GitLabRunner
			GitLabRunner->>Executor: Job payload
			activate Executor
			Executor->>GitLab: clone sources with job token
			Executor->>GitLab: download artifacts with job token
			Executor-->>GitLabRunner: return job output and status
			deactivate Executor
		GitLabRunner-->>GitLab: updating job output and status with job_token
		deactivate GitLabRunner
	end

2.1 Executor

executor类型大概有下面几种

ExecutorSSHShellVirtualBoxParallelsDockerKubernetesCustom
每次构建清理构建环境✗✗✓✓✓✓conditional(4)
重复使用之前的clone✓✓✗✗✓✗conditional(4)
Runner的文件系统访问受保护✓✗✓✓✓✓conditional
转移Runner的机器✗✗部分部分✓✓✓
零配置支持并发构建✗✗(1)✓✓✓✓conditional(4)
复杂构建环境✗✗(2)✗(3)✗(3)✓✓✓
调试构建问题easyeasyhardhardmediummediummedium
  1. It’s possible, but in most cases it is problematic if the build uses services installed on the build machine
  2. It requires to install all dependencies by hand
  3. For example using Vagrant
  4. Dependent on what kind of environment you are provisioning. It can be completely isolated or shared between each build.
  5. When a runner’s file system access is not protected, jobs can access the entire system including the runner’s token, and the cache and code of other jobs. Executors marked ✓ don’t allow the runner to access the file system by default. However, security flaws or certain configurations could allow jobs to break out of their container and access the file system hosting runner.

3. FAQ

  1. sudo gitlab-runner install提示command not found

    由于gitlab安装在/usr/local/bin下面,而sudo环境变量下不存在改地址,所以这个需要把改路径加到环境变量中
    打开/etc/sudoers,修改如下内容

    PLAINTEXT
    Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin

    增加

    PLAINTEXT
    Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

4. 更新gitlab-runner

  1. 停止服务

    BASH
    sudo gitlab-runner stop
  2. 下载最新脚本替换当前的

    BASH
    sudo curl -L --output /usr/local/bin/gitlab-runner "https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64"
  3. 给脚本赋权

    BASH
    sudo chmod +x /usr/local/bin/gitlab-runner
  4. 启动服务

    BASH
    sudo gitlab-runner start

评论交流

暂无评论,欢迎分享您的见解