简体   繁体   English

如何从命令行获取 Gitlab 跑步者注册令牌?

[英]How to get Gitlab runner registration token from command line?

I'm trying to deploy a Gitlab instance and runners ready with Terraform.我正在尝试部署 Gitlab 实例和准备好 Terraform 的跑步者。 The script creates both Gitlab and runners without any problem, but I don't know how to register the runners automatically after the creation.该脚本创建 Gitlab 和跑步者没有任何问题,但我不知道如何在创建后自动注册跑步者。

Is there any way to get the registration token from command line?有没有办法从命令行获取注册令牌? If it's possible I can register just calling external data source using Terraform.如果可能的话,我可以使用 Terraform 注册调用外部数据源。

Theprojects API endpoint response contains the runners_token key. 项目 API 端点响应包含runners_token键。 You can use this to automatically fetch the runner tokens for any project.您可以使用它来自动获取任何项目的运行器令牌。

You can then use that in a few ways.然后,您可以通过几种方式使用它。 One way would be to have your runner registration script fetch the runner token itself such as with this example:一种方法是让您的跑步者注册脚本获取跑步者令牌本身,例如以下示例:

curl --fail --silent --header "Private-Token: ${GITLAB_API_TOKEN}" "https://$GITLAB_URL/api/v4/projects/${PROJECT}"

Or you could use the Gitlab Terraform provider 's gitlab_project data source to fetch this from whatever is running Terraform and then inject it into the thing that runs the registration script such as a templated file:或者您可以使用Gitlab Terraform 提供者gitlab_project数据源从运行 Terraform 的任何内容中获取此内容,然后将其注入到运行模板文件的注册脚本中:

data "gitlab_project" "example" {
  id = 30
}

locals {
  runner_config = {
    runner_token = data.gitlab_project.example.runners_token
  }
}

output "example" {
  value = templatefile("${path.module}/register-runners.sh.tpl", local.runner_config)
}

Yes, you can.是的你可以。

The command has to be run on the server hosting your Gitlab instance.该命令必须在托管您的 Gitlab 实例的服务器上运行。 The line below will output the current shared runner token.下面的行将 output 当前共享的跑步者令牌。

sudo gitlab-rails runner -e production "puts Gitlab::CurrentSettings.current_application_settings.runners_registration_token"

As others have mentioned, there is not API endpoint that currently allows this (there has been discussion over this for quite some time here . However, I find this solution satisfactory for my needs.正如其他人所提到的,目前没有 API 端点允许这样做(这里已经讨论了很长时间。但是,我发现这个解决方案可以满足我的需求。

Credits for this answer go to MxNxPx .此答案的积分 go 到MxNxPx This script used to work (for me) two days ago: 这个脚本两天前曾经(对我来说)工作:

GITUSER="root"
GITURL="http://127.0.0.1"
GITROOTPWD="mysupersecretgitlabrootuserpassword"

# 1. curl for the login page to get a session cookie and the sources with the auth tokens
body_header=$(curl -k -c gitlab-cookies.txt -i "${GITURL}/users/sign_in" -sS)

# grep the auth token for the user login for
#   not sure whether another token on the page will work, too - there are 3 of them
csrf_token=$(echo $body_header | perl -ne 'print "$1\n" if /new_user.*?authenticity_token"[[:blank:]]value="(.+?)"/' | sed -n 1p)

# 2. send login credentials with curl, using cookies and token from previous request
curl -sS -k -b gitlab-cookies.txt -c gitlab-cookies.txt "${GITURL}/users/sign_in" \
    --data "user[login]=${GITUSER}&user[password]=${GITROOTPWD}" \
    --data-urlencode "authenticity_token=${csrf_token}"  -o /dev/null

# 3. send curl GET request to gitlab runners page to get registration token
body_header=$(curl -sS -k -H 'user-agent: curl' -b gitlab-cookies.txt "${GITURL}/admin/runners" -o gitlab-header.txt)
reg_token=$(cat gitlab-header.txt | perl -ne 'print "$1\n" if /code id="registration_token">(.+?)</' | sed -n 1p)
echo $reg_token

However, as of today it stopped working.但是,截至今天,它停止工作。 I noticed the second body_header variable is empty.我注意到第二个body_header变量是空的。 Upon inspecting the gitlab-header.txt file, I noticed it contained:检查gitlab-header.txt文件后,我注意到它包含:

You are being redirected . 您正在被重定向

Whereas I would expect it to be signed in at that point, with a gitlab-header.txt file that contains the respective runner registration token .而我希望它会在那时登录,并使用包含相应runner registration tokengitlab-header.txt文件。 I expect I am doing something wrong, however, perhaps there has been an update to the gitlab/gitlab-ce:latest package such that a change to the script is required.我希望我做错了什么,但是,也许gitlab/gitlab-ce:latest package 已经更新,因此需要更改脚本。

Disclaimer, I am involved in creating that code Here is a horrible but working Python boiler plate code that gets the runner token and exports it to a parent repository: https://github.com/at-0/get-gitlab-runner-registration-token .免责声明,我参与创建该代码 是一个可怕但有效的 Python 样板代码,它获取运行器令牌并将其导出到父存储库: https://github.com/at-0/get-gitlab-runner-注册令牌

Independent usage独立使用

It requires a few manual steps to set up, and then gets the GitLab runner registration token automatically (from the CLI with:).它需要几个手动步骤来设置,然后自动获取 GitLab 跑步者注册令牌(从 CLI 使用:)。 It requires Conda and Python however, and downloads a browser controller.然而,它需要 Conda 和 Python,并下载浏览器 controller。 So it is most likely wiser to look a bit better into the curl commands instead.因此,最好对 curl 命令进行更好的观察。

Integrated in parent [bash] repository集成在父 [bash] 存储库中

First install the conda environment, then activate it.先安装conda环境,然后激活。 After that, you can execute the function below automatically from the CLI (if you put that function in a file at path parent_repo/src/get_gitlab_server_runner_token.sh , assuming you have the credentials etc as specified in the Readme), with:之后,您可以从 CLI 自动执行下面的 function(如果您将 function 放在路径parent_repo/src/get_gitlab_server_runner_token.sh的文件中,假设您具有自述文件中指定的凭据等)

cd parent_repo
source src/get_gitlab_server_runner_token.sh && get_registration_token_with_python

This bash function gets the token:这个 bash function 得到令牌:

get_registration_token_with_python() {
    # delete the runner registration token file if it exist
    if [ -f "$RUNNER_REGISTRATION_TOKEN_FILEPATH" ] ; then
        rm "$RUNNER_REGISTRATION_TOKEN_FILEPATH"
    fi
    
    
    git clone https://github.com/a-t-0/get-gitlab-runner-registration-token.git &&
    set +e
    cd get-gitlab-runner-registration-token && python -m code.project1.src
    cd ..
}

And here is a BATS test that verifies the token is retrieved:这是一个 BATS 测试,用于验证是否已检索到令牌:

#!./test/libs/bats/bin/bats

load 'libs/bats-support/load'
load 'libs/bats-assert/load'
load 'libs/bats-file/load'

source src/get_gitlab_server_runner_token.sh
source src/hardcoded_variables.txt

@test "Checking if the gitlab runner registration token is obtained correctly." {
    
    get_registration_token_with_python
    actual_result=$(cat $RUNNER_REGISTRATION_TOKEN_FILEPATH)
    EXPECTED_OUTPUT="somecode"

    assert_file_exist $RUNNER_REGISTRATION_TOKEN_FILEPATH
    assert_equal ${#actual_result}   20
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM