[英]How to get Gitlab runner registration token from command line?
我正在嘗試部署 Gitlab 實例和准備好 Terraform 的跑步者。 該腳本創建 Gitlab 和跑步者沒有任何問題,但我不知道如何在創建后自動注冊跑步者。
有沒有辦法從命令行獲取注冊令牌? 如果可能的話,我可以使用 Terraform 注冊調用外部數據源。
項目 API 端點響應包含runners_token
鍵。 您可以使用它來自動獲取任何項目的運行器令牌。
然后,您可以通過幾種方式使用它。 一種方法是讓您的跑步者注冊腳本獲取跑步者令牌本身,例如以下示例:
curl --fail --silent --header "Private-Token: ${GITLAB_API_TOKEN}" "https://$GITLAB_URL/api/v4/projects/${PROJECT}"
或者您可以使用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)
}
是的你可以。
該命令必須在托管您的 Gitlab 實例的服務器上運行。 下面的行將 output 當前共享的跑步者令牌。
sudo gitlab-rails runner -e production "puts Gitlab::CurrentSettings.current_application_settings.runners_registration_token"
正如其他人所提到的,目前沒有 API 端點允許這樣做(這里已經討論了很長時間。但是,我發現這個解決方案可以滿足我的需求。
此答案的積分 go 到MxNxPx 。 這個腳本兩天前曾經(對我來說)工作:
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
但是,截至今天,它停止工作。 我注意到第二個body_header
變量是空的。 檢查gitlab-header.txt
文件后,我注意到它包含:
您正在被重定向。
而我希望它會在那時登錄,並使用包含相應runner registration token
的gitlab-header.txt
文件。 我希望我做錯了什么,但是,也許gitlab/gitlab-ce:latest
package 已經更新,因此需要更改腳本。
免責聲明,我參與創建該代碼 這是一個可怕但有效的 Python 樣板代碼,它獲取運行器令牌並將其導出到父存儲庫: https://github.com/at-0/get-gitlab-runner-注冊令牌。
它需要幾個手動步驟來設置,然后自動獲取 GitLab 跑步者注冊令牌(從 CLI 使用:)。 然而,它需要 Conda 和 Python,並下載瀏覽器 controller。 因此,最好對 curl 命令進行更好的觀察。
先安裝conda環境,然后激活。 之后,您可以從 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
這個 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 ..
}
這是一個 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.