如何从authentication_token获取GitLab runner ID?

问题描述 投票:0回答:2

当我注册跑步者时,我可以通过以下方式列出我的跑步者详细信息:

# gitlab-runner list
Runtime platform               arch=amd64 os=linux pid=103997 revision=7a6612da version=13.12.0
Listing configured runners     ConfigFile=/etc/gitlab-runner/config.toml
runner-myproject               Executor=docker Token=xxx URL=https://gitlab.example.com

所以我在该响应中有authentication_token(也可以在我的config.toml中找到)。 从该跑步者令牌中,我如何确定跑步者 ID?

我想要一个 cron 来检查运行程序是否繁忙,如果不忙,它将从自动缩放组中取消注册运行程序:

  1. 检查跑步者是否有活跃的工作
  2. 如果有 0 个活动作业,则暂停运行器(可能会确保它已经空闲了一段时间)。
  3. 从 GitLab 取消注册运行器。
  4. 从 AWS 自动缩放组取消注册运行程序实例。

但是,API 仅适用于跑步者 ID,我对跑步者的了解只有令牌。

gitlab gitlab-ci gitlab-ci-runner gitlab-api
2个回答
1
投票

您可以从 API 获取此信息。

首先,您可以获取某个实例或特定项目(取决于您的要求)的所有运行者,如下所示:

# For the instance:
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/runners/all"

# For a specific project id
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/:project_id:/runners"

结果将类似于:

[
    {
        "active": true,
        "description": "test-2-20150125",
        "id": 8,
        "ip_address": "127.0.0.1",
        "is_shared": false,
        "runner_type": "project_type",
        "name": null,
        "online": false,
        "status": "offline"
    },
    {
        "active": true,
        "description": "development_runner",
        "id": 5,
        "ip_address": "127.0.0.1",
        "is_shared": true,
        "runner_type": "instance_type",
        "name": null,
        "online": true,
        "status": "paused"
    }
]

然后,您可以循环遍历运行程序,获取

id
,并使用您已找到的其他 API 调用获取该运行程序的所有作业。 这里是文档,了解此 API 的更多详细信息和选项:


0
投票

这是我自己解决这个问题的方法,有点复杂但有效:

我抓取了

gitlab-runner
日志来查找最新作业的 ID,然后调用 Gitlab
jobs
API 来获取有关该作业的信息,其中包括运行者的 id:

JOB_ID=$(tail -1 /var/log/gitlab-runner.log | jq -c 'select( .job != null ) | .job')
RUNNER_ID=$(curl http://gitlab.example.com/api/v4/projects/$PROJECT_ID/jobs/$JOB_ID | \
   jq -c '.runner.id')

这仅在以下情况下有效:

  1. 运行者的主机至少运行过一项作业
  2. 你知道
    PROJECT_ID

就我而言,如果运行者从未完成过一项工作,并且每个运行者仅运行一个项目的作业,那么失败是可以接受的。希望这对某人有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.