教程:创建、注册并运行您的项目 runner
这篇教程将向您展示如何在极狐 GitLab 配置并运行您的第一个 runner。
runner 是极狐 GitLab Runner 应用程序中的一个代理,用于在极狐 GitLab CI/CD 流水线中运行作业。作业在 .gitlab-ci.yml 文件中定义,并分配给可用的 runners。
极狐 GitLab 有三种类型的 runners:
- Shared: 在极狐 GitLab 实例中的所有群组和项目中可用。
- Group: 在群组中的所有项目和子群组中可用。
- Project: 与特定项目相关联。通常,项目 runner 一次由一个项目使用。
在本教程中,您将创建一个项目 runner 来运行在基础流水线配置中定义的作业:
在开始之前
在您可以创建、注册和运行 runner 之前,极狐 GitLab Runner 必须安装在本地计算机上。
创建一个空白项目
首先,创建一个空白项目,您可以在其中创建您的 CI/CD 流水线和 runner。
要创建一个空白项目:
- 在左侧边栏顶部,选择 创建新的 () 和 新项目/仓库。
- 选择 创建空白项目。
- 输入项目详情:
- 在 项目名称 字段中,输入您的项目名称。名称必须以小写或大写字母 (a-zA-Z)、数字 (0-9)、表情符号或下划线 (_) 开头。它还可以包含点 (.)、加号 (+)、破折号 (-) 或空格。
- 在 项目 slug 字段中,输入您的项目路径。极狐 GitLab 实例使用 slug 作为项目的 URL 路径。要更改 slug,请先输入项目名称,然后更改 slug。
- 选择 创建项目。
创建一个项目流水线
接下来,为您的项目创建一个 .gitlab-ci.yml 文件。这是一个 YAML 文件,您可以在其中为极狐 GitLab CI/CD 指定指令。
在此文件中,您定义:
- runner 应执行的作业的结构和顺序。
- runner 在遇到特定条件时应做出的决策。
-
在左侧边栏,选择 搜索或进入 并找到您的项目或群组。
-
选择 项目概览。
-
选择加号图标 (
),然后选择 新文件。 -
在 文件名 字段中,输入 .gitlab-ci.yml。
-
在大文本框中,粘贴此示例配置:
yaml1stages: 2 - build 3 - test 4 5job_build: 6 stage: build 7 script: 8 - echo "Building the project" 9 10job_test: 11 stage: test 12 script: 13 - echo "Running tests"在此配置中有两个作业,runner 运行:一个构建作业和一个测试作业。
-
选择 提交更改。
创建并注册一个项目 runner
接下来,创建并注册一个项目 runner。您必须注册 runner 以将其链接到极狐 GitLab,以便它可以从项目流水线中提取作业。
要创建一个项目 runner:
-
在左侧边栏,选择 搜索或进入 并找到您的项目。
-
选择 设置 > CI/CD。
-
展开 Runners 部分。
-
选择 新项目 runner。
-
选择您的操作系统。
-
在 标签 部分,选择 运行未标记 复选框。标签 指定 runner 可以运行哪些作业,并且是可选的。
-
选择 创建 runner。
-
按照屏幕上的说明从命令行注册 runner。在提示时:
- 对于 executor,因为您的 runner 将直接在主机计算机上运行,请输入 shell。executor 是 runner 执行作业的环境。
- 对于 极狐 GitLab 实例 URL,使用您的极狐 GitLab 实例的 URL。例如,如果您的项目托管在 gitlab.example.com/yourname/yourproject,那么您的极狐 GitLab 实例 URL 是 https://gitlab.example.com。如果您的项目托管在 JihuLab.com,URL 是 https://jihulab.com。
-
启动您的 runner:
shellgitlab-runner run
检查 runner 配置文件
注册 runner 后,配置和 runner 认证令牌将保存到您的 config.toml 中。runner 使用该令牌在从作业队列中提取作业时与极狐 GitLab 进行认证。
您可以使用 config.toml 定义更多高级 runner 配置。
这是您注册并启动 runner 后 config.toml 应该的样子:
toml1 [[runners]] 2 name = "my-project-runner1" 3 url = "http://127.0.0.1:3000" 4 id = 38 5 token = "glrt-TOKEN" 6 token_obtained_at = 2023-07-05T08:56:33Z 7 token_expires_at = 0001-01-01T00:00:00Z 8 executor = "shell"
触发流水线以运行您的 runner
接下来,触发项目中的流水线,以便您可以查看您的 runner 执行作业。
-
在左侧边栏,选择 搜索或进入 并找到您的项目。
-
选择 构建 > 流水线。
-
选择 新流水线。
-
选择一个作业以查看作业日志。输出应类似于此示例,显示您的 runner 成功执行作业:
shell1 Running with gitlab-runner 16.2.0 (782e15da) 2 on my-project-runner TOKEN, system ID: SYSTEM ID 3 Preparing the "shell" executor 4 00:00 5 Using Shell (bash) executor... 6 Preparing environment 7 00:00 8 /Users/username/.bash_profile: line 9: setopt: command not found 9 Running on MACHINE-NAME... 10 Getting source from Git repository 11 00:01 12 /Users/username/.bash_profile: line 9: setopt: command not found 13 Fetching changes with git depth set to 20... 14 Reinitialized existing Git repository in /Users/username/project-repository 15 Checking out 7226fc70 as detached HEAD (ref is main)... 16 Skipping object checkout, Git LFS is not installed for this repository. 17 Consider installing it with 'git lfs install'. 18 Skipping Git submodules setup 19 Executing "step_script" stage of the job script 20 00:00 21 /Users/username/.bash_profile: line 9: setopt: command not found 22 $ echo "Building the project" 23 Building the project 24 Job succeeded 25
您现在已成功创建、注册并运行了您的第一个 runner!