本教程介绍如何在 GitLab 中配置并运行第一个 Runner。Runner 是 GitLab Runner 应用中的执行代理,负责运行 GitLab CI/CD 流水线中的作业。作业定义在 .gitlab-ci.yml 文件中,并分配给可用的 Runner。
GitLab 有三类 Runner:
- 共享 Runner:可供 GitLab 实例中的所有群组和项目使用。
- 群组 Runner:可供某个群组内的所有项目和子群组使用。
- 项目 Runner:关联到特定项目,通常一次供一个项目使用。
本教程将创建一个项目 Runner,用于运行基础流水线配置中的作业,依次完成以下步骤:
- 创建空白项目。
- 创建项目流水线。
- 创建并注册项目 Runner。
- 触发流水线,让 Runner 运行。
开始之前
在创建、注册并运行 Runner 之前,必须先在本地计算机上安装 GitLab Runner。
创建空白项目
首先创建一个空白项目,用来建立 CI/CD 流水线和 Runner。
- 在右上角选择“新建”(加号),然后选择“新建项目/仓库”。
- 选择“创建空白项目”。
- 填写项目详细信息。
- 在“项目名称”字段输入名称。名称必须以大小写字母(a-zA-Z)、数字(0-9)、表情符号或下划线(_)开头,也可以包含点(.)、加号(+)、连字符(-)或空格。
- 在“项目路径”字段输入项目路径。GitLab 实例会将这个 slug 用作项目 URL 中的路径。若要修改 slug,先输入项目名称,再更改 slug。
- 选择“创建项目”。
创建项目流水线
接下来,为项目创建 .gitlab-ci.yml。这是一个 YAML 文件,用于指定 GitLab CI/CD 的指令。在其中定义:
- Runner 应执行的作业结构和顺序。
- Runner 遇到特定条件时应作出的选择。
操作步骤如下:
- 在顶部栏选择“搜索或转到”,找到项目或群组。
- 选择“项目概览”。
- 选择加号图标,再选择“新建文件”。
- 在“文件名”字段输入
.gitlab-ci.yml。 - 在大的文本框中粘贴以下示例配置:
stages:
- build
- test
job_build:
stage: build
script:
- echo "Building the project"
job_test:
stage: test
script:
- echo "Running tests"
该配置包含两个由 Runner 执行的作业:一个构建作业和一个测试作业。
- 选择“提交更改”。
创建并注册项目 Runner
接下来创建项目 Runner 并注册。注册将 Runner 与 GitLab 关联起来,使其能够领取项目流水线中的作业。
- 在顶部栏选择“搜索或转到”,找到项目。
- 在左侧栏选择“设置 > CI/CD”。
- 展开“Runners”区域。
- 选择“创建项目 Runner”。
- 在“标签”区域勾选“运行无标签作业”。标签用于指定 Runner 可以执行哪些作业,是可选项。
- 选择“创建 Runner”。
- 选择操作系统。
- 按照屏幕提示,通过命令行注册 Runner。出现提示时:
- 对于 executor,因为 Runner 会直接在主机上运行,请输入
shell。Executor 是 Runner 执行作业的环境。 - 对于 GitLab 实例 URL,使用你的 GitLab 实例地址。例如,项目位于
gitlab.example.com/yourname/yourproject,实例 URL 就是https://gitlab.example.com;项目托管在 GitLab.com 时,URL 是https://gitlab.com。
- 对于 executor,因为 Runner 会直接在主机上运行,请输入
- 启动 Runner:
gitlab-runner run
检查 Runner 配置文件
注册 Runner 后,配置及 Runner 身份验证令牌会保存在 config.toml 中。Runner 从作业队列领取任务时,会使用此令牌向 GitLab 验证身份。
还可以通过 config.toml 定义更高级的 Runner 配置。注册并启动 Runner 后,该文件应类似以下内容:
[[runners]]
name = "my-project-runner1"
url = "http://127.0.0.1:3000"
id = 38
token = "glrt-TOKEN"
token_obtained_at = 2023-07-05T08:56:33Z
token_expires_at = 0001-01-01T00:00:00Z
executor = "shell"
触发流水线,让 Runner 执行作业
接下来在项目中触发流水线,查看 Runner 执行作业的过程。
- 在顶部栏选择“搜索或转到”,找到项目。
- 在左侧栏选择“构建 > 流水线”。
- 选择“新建流水线”。
- 选择一个作业,查看作业日志。输出应类似下面的示例,表明 Runner 成功执行了作业:
Running with gitlab-runner 18.0.0 (d7f2cea7)
on my-project-runner TOKEN, system ID: SYSTEM ID
Preparing the "shell" executor
00:00
Using Shell (bash) executor...
Preparing environment
00:00
/Users/username/.bash_profile: line 9: setopt: command not found
Running on MACHINE-NAME...
Getting source from Git repository
00:01
/Users/username/.bash_profile: line 9: setopt: command not found
Fetching changes with git depth set to 20...
Reinitialized existing Git repository in /Users/username/project-repository
Checking out 7226fc70 as detached HEAD (ref is main)...
Skipping object checkout, Git LFS is not installed for this repository.
Consider installing it with 'git lfs install'.
Skipping Git submodules setup
Executing "step_script" stage of the job script
00:00
/Users/username/.bash_profile: line 9: setopt: command not found
$ echo "Building the project"
Building the project
Job succeeded
至此,你已成功创建、注册并运行了第一个 Runner!
原文:Tutorial: Create, register, and run your own project runner。作者/维护方:GitLab 文档维护者。本文为中文翻译,代码及命令保留原文。











暂无评论内容