2

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.

Adam Marshall
  • 6,369
  • 1
  • 29
  • 45
DavidGamba
  • 3,503
  • 2
  • 30
  • 46
  • Did you ever figure this out? I have a similar need - trying to pause the runner if the EC2 instance is scaling in, but I need the runner's ID to make the API call to Gitlab. – John Nimis Aug 29 '23 at 19:33

1 Answers1

1

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:

Adam Marshall
  • 6,369
  • 1
  • 29
  • 45
  • how can I correlate the ID you have from that response to the runner token? Basically need to get the ID of the runner from within the runner and that is where I am at a lost. – DavidGamba Sep 09 '21 at 17:42
  • If you need the ID from inside a running Pipeline Job, you can use the `CI_RUNNER_ID` [Predefined Variable](https://docs.gitlab.com/ee/ci/variables/predefined_variables.html). – Adam Marshall Sep 09 '21 at 17:58