DavidGamba
DavidGamba

Reputation: 3613

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.

Upvotes: 2

Views: 4630

Answers (2)

John Nimis
John Nimis

Reputation: 606

Here's how I solved this problem for myself, it's a little convoluted but works:

I scraped the gitlab-runner logs to find the ID of the latest job, then called the Gitlab jobs API to get info about that job, which includes the runner's 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')

This will only work if:

  1. the runner's host has run at least one job
  2. you know the PROJECT_ID

In my case, it was OK to fail if the runner had never done a job, and each runner only runs jobs for one project. Hopefully this helps someone!

Upvotes: 0

Adam Marshall
Adam Marshall

Reputation: 7695

You can get this information from the API.

First, you can get all the runners for an instance or for a specific project (depending on your requirements) like so:

# 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"

The result will look something like:

[
    {
        "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"
    }
]

You can then loop through the runners, grab the id, and get all jobs for that runner with the other API call you found already. Here's the docs for more details and options for this API:

Upvotes: 1

Related Questions