极狐 GitLab

教程:创建、注册并运行自己的项目 runner

本教程向您展示如何在极狐GitLab 中配置和运行您的第一个 runner。

runner 是极狐GitLab Runner 应用程序中的一个代理,用于在极狐GitLab CI/CD 流水线中运行作业。 作业在 .gitlab-ci.yml 文件中定义,并分配给可用的 runner。

极狐GitLab 有三种类型的 runner:

  • 共享型:对极狐GitLab 实例中的所有群组和项目可用。
  • 群组型:对群组中的所有项目和子群组可用。
  • 项目型:与特定项目关联。通常,项目 runner 一次只供一个项目使用。

在本教程中,您将创建一个项目 runner 来运行基础流水线配置中定义的作业:

  1. 创建空白项目
  2. 创建项目流水线
  3. 创建并注册项目 runner
  4. 触发流水线来运行您的 runner

准备工作#

在创建、注册和运行 runner 之前,您必须在本地计算机上安装极狐GitLab Runner

创建空白项目#

首先,创建一个空白项目,您可以在其中创建 CI/CD 流水线和 runner。

要创建空白项目:

  1. 在右上角,选择 新建 () 和 新项目/仓库
  2. 选择 创建空白项目
  3. 输入项目详情:
    • 项目名称 字段中,输入您的项目名称。名称必须以小写或大写字母 (a-zA-Z)、数字 (0-9)、表情符号或下划线 (_) 开头。还可以包含点 (.)、加号 (+)、破折号 (-) 或空格。
    • 项目路径 字段中,输入项目的路径。极狐GitLab 实例会使用该路径作为项目的 URL 路径。要更改路径,请先输入项目名称,然后更改路径。
  4. 选择 创建项目

创建项目流水线#

接下来,为您的项目创建一个 .gitlab-ci.yml 文件。这是一个 YAML 文件,用于指定极狐GitLab CI/CD 的指令。

在此文件中,您定义:

  • runner 应执行的作业结构和顺序。
  • runner 在遇到特定情况时应做出的决策。
  1. 在顶部导航栏中,选择 搜索或跳转到 并找到您的项目或群组。

  2. 选择 项目概览

  3. 选择加号图标 (

    ),然后选择 新文件

  4. 文件名 字段中,输入 .gitlab-ci.yml

  5. 在大文本框中,粘贴以下示例配置:

    yaml
    1stages: 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 运行的作业:构建作业和测试作业。

  6. 选择 提交变更

创建并注册项目 runner#

接下来,创建项目 runner 并进行注册。您必须注册 runner 以将其链接到极狐GitLab,以便它可以从项目流水线中获取作业。

要创建项目 runner:

  1. 在顶部导航栏中,选择 搜索或跳转到 并找到您的项目。

  2. 在左侧边栏中,选择 设置 > CI/CD

  3. 展开 Runners 部分。

  4. 选择 创建项目 runner

  5. 标签 部分,选中 运行未标记 复选框。标签 指定 runner 可以运行哪些作业,此步骤为可选。

  6. 选择 创建 runner

  7. 选择您的操作系统。

  8. 按照屏幕上的说明从命令行注册 runner。当提示时:

    • 对于 执行器,因为您的 runner 将直接在主机上运行,请输入 shell执行器 是 runner 执行作业的环境。
    • 对于 极狐GitLab 实例 URL,使用您的极狐GitLab 实例的 URL。例如,如果您的项目托管在 gitlab.example.com/yourname/yourproject,那么您的极狐GitLab 实例 URL 是 https://gitlab.example.com。如果您的项目托管在 JihuLab.com 上,则 URL 是 https://jihulab.com
  9. 启动您的 runner:

    shell
    gitlab-runner run

检查 runner 配置文件#

注册 runner 后,配置和 runner 认证令牌会保存到您的 config.toml 文件中。runner 使用该令牌在从作业队列中获取作业时向极狐GitLab 认证。

您可以使用 config.toml 定义更多高级 runner 配置

注册并启动 runner 后,您的 config.toml 应如下所示:

toml
1[[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 执行作业。

  1. 在顶部导航栏中,选择 搜索或跳转到 并找到您的项目。

  2. 在左侧边栏中,选择 构建 > 流水线

  3. 选择 新流水线

  4. 选择一个作业以查看作业日志。输出应类似于以下示例,显示您的 runner 成功执行了作业:

    shell
    1 Running with gitlab-runner 18.0.0 (d7f2cea7) 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!