Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I delete/unregister a GitLab runner

I have registered a personal GitLab runner several months ago, which I no longer use. How do I completely delete it so that it does not show up on my GitLab CI/CD settings page?

like image 817
ostrokach Avatar asked Mar 13 '21 16:03

ostrokach


People also ask

How do I remove GitLab runner from Windows?

To do this, open the Command Prompt as shown below. Browse to the following location where the executable resides: C:\GitLab-Runner . – Run the command GitLab-runner stop (This will ensure the service stops running). As you can see above, you can now choose to delete the folder in which the GitLab-Runner resided.

How do you disabling shared runners for your project in GitLab?

To disable shared runners for a project: Go to the project's Settings > CI/CD and expand the Runners section. In the Shared runners area, select Enable shared runners for this project so the toggle is grayed-out.


3 Answers

Get your runner token and id

First, go to the GitLab settings page and find the token (e.g. 250cff81 in the image below) and the id (e.g. 354472 in the image below) of the GitLab runner which you wish to delete.

enter image description here

Use the gitlab-runner CLI to unregister the runner

If you have access to the machine which was used to register the GitLab runner, you can unregister the runner using the following command, where you replace {TOKEN} with the token of your GitLab runner (e.g. 250cff81 in the example above).

gitlab-runner unregister --url https://gitlab.org/ --token {TOKEN}

Use the GitLab API to unregister the runner

If you no longer have access to the machine which was used to register the runner, or if the runner is associated with multiple projects, you can use the following Python script. Set RUNNER_ID to the id of your runner (e.g. 354472 in the example above) and GITLAB_AUTH_TOKEN to a GitLab token which you can generate from your profile page.

import os
import requests

GITLAB_AUTH_TOKEN = ...
RUNNER_ID = ...

headers = {"PRIVATE-TOKEN": GITLAB_AUTH_TOKEN}

r = requests.get(f"https://gitlab.com/api/v4/runners/{RUNNER_ID}", headers=headers)
runner_data = r.json()

for project in runner_data.get("projects", []):
    r = requests.delete(
        f"https://gitlab.com/api/v4/projects/{project['id']}/runners/{RUNNER_ID}",
        headers=headers,
    )
    if not r.ok:
        print("Encountered an error deleting runner from project:", r.json())

r = requests.delete(f"https://gitlab.com/api/v4/runners/{RUNNER_ID}", headers=headers)
if not r.ok:
    print("Encountered an error deleting runner:", r.json())
like image 97
ostrokach Avatar answered Oct 09 '22 11:10

ostrokach


  1. List runners to get their tokens and URLs:

    sudo gitlab-runner list
    
  2. Verify with delete option specifying runner's token and URL:

    sudo gitlab-runner verify --delete -t YMsSCHnjGssdmz1JRoxx -u http://git.xxxx.com/
    
like image 16
Serge Iroshnikov Avatar answered Oct 09 '22 11:10

Serge Iroshnikov


Here's one-liner to remove offline runners (for GitLab 14.5):

curl --header "PRIVATE-TOKEN: <private_token>" "https://<your-instance-address>/api/v4/runners/all?scope=offline&per_page=100" | jq '.[].id' | xargs -I runner_id curl --request DELETE --header "PRIVATE-TOKEN: <private_token>" "https://<your-instance-address>/api/v4/runners/runner_id"

You might run this more than once if you have more than 100 offline runners (per_page=100).

like image 5
swietyy Avatar answered Oct 09 '22 09:10

swietyy