为了使用 API 令牌,用户必须从https://<jenkins-server>/me/configure
或获取他们自己的令牌https://<jenkins-server>/user/<user-name>/configure
。作为脚本的作者,您可以决定用户如何向脚本提供令牌。例如,在 Git 存储库中以交互方式运行的 Bourne Shell 脚本中,其中.gitignore
contains /.jenkins_api_token
,您可能会执行以下操作:
api_token_file="$(git rev-parse --show-cdup).jenkins_api_token"
api_token=$(cat "$api_token_file" || true)
if [ -z "$api_token" ]; then
echo
echo "Obtain your API token from $JENKINS_URL/user/$user/configure"
echo "After entering here, it will be saved in $api_token_file; keep it safe!"
read -p "Enter your Jenkins API token: " api_token
echo $api_token > "$api_token_file"
fi
curl -u $user:$api_token $JENKINS_URL/someCommand