首页 > 解决方案 > How to obtain the GitLab runner ID from the authentication_token?

问题描述

When I register a runner, I can list my runner details with:

# 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

So I have the authentication_token in that response (also available in my config.toml). From that runner token, how can I determine the runner ID?

I would like a cron that checks if the runner is busy and if not, it deregisters the runner from the autoscaling group:

  1. Checks if the runner has active jobs
  2. Pause the runner if there are 0 active jobs (would probably ensure that it has been idle for a while).
  3. Deregister the runner from GitLab.
  4. Deregister the runner instance from the AWS autoscaling group.

However, the API only works with the runner ID and all I know about the runner is the Token.

标签: gitlabgitlab-ci-runnergitlab-api

解决方案


您可以从 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 的更多详细信息和选项的文档:


推荐阅读