Jenkins用户身份validation细节如何被“传递”给使用Jenkins API创build作业的脚本?

我有一个脚本,通过curl HTTP调用删除和重新创build作业,我想摆脱任何硬编码的“用户名:密码”。 例如curl -X POST $url --user username:password

注意事项:

  • jenkinsCLI (可能不是一个选项)。 一个应该能够实现与Jenkins API(创build工作等)相同的CLI,但据我所知,jenkinsCLI不是一个很好的替代品,因为创build工作将只会出现在jenkins后重新启动或“重新加载从磁盘configuration“,这将取消任何其他正在运行的作业。

  • API令牌 。 找不到如何获取用户令牌,然后将其作为parameter passing给脚本,但这可能是一个解决scheme..

试试这个方法:(例如删除作业)

 curl --silent --show-error http://<username>:<api-token>@<jenkins-server>/job/<job-name>/doDelete 

api-token可以从http://<jenkins-server>/user/<username>/configure

这对我工作:

 curl -u $username:$api_token -FSubmit=Build 'http://<jenkins-server>/job/<job-name>/buildWithParameteres?environment=' 

API令牌可以从Jenkins用户configuration中获得。

  • 使用Jenkins CLI,您不必重新加载所有内容 – 只需加载作业( update-job命令)即可。 您不能使用CLI,AFAIK令牌 – 您必须使用密码或密码文件。

  • 用户的令牌名称可以通过http://<jenkins-server>/user/<username>/configure – 在“显示API令牌”button上获得。

  • 这里有一个关于如何使用API​​令牌的链接 (它使用wget ,但curl非常相似)。

我需要在CURL命令中明确地添加POST:

 curl -X POST http://<user>:<token>@<server>/safeRestart 

我还安装了SafeRestart插件 ,以防万一。

如果您想编写脚本来使用Jenkins API自动创build作业,则可以使用其中一个API客户端来执行此操作。 Jenkins的ruby客户端可在https://github.com/arangamani/jenkins_api_client

 gem install jenkins_api_client require "rubygems" require "jenkins_api_client" # Initialize the client by passing in the server information # and credentials to communicate with the server client = JenkinsApi::Client.new( :server_ip => "127.0.0.1", :username => "awesomeuser", :password => "awesomepassword" ) # The following block will create 10 jobs in Jenkins # test_job_0, test_job_1, test_job_2, ... 10.times do |num| client.job.create_freestyle(:name => "test_job_#{num}") end # The jobs in Jenkins can be listed using client.job.list_all 

API客户端可以用来执行很多操作。

API令牌与从APIangular度来看的密码相同,请参阅源代码使用令牌代替API的密码 。

在我的问题看到@coffeebreaks的相关答案python-jenkins或jenkinsapi jenkins远程访问API在python

其他在doc中描述使用http基本authentication模式

为了使用API​​令牌,用户必须从https://<jenkins-server>/me/configurehttps://<jenkins-server>/user/<user-name>/configure 。 作为脚本的作者,由您决定用户如何提供令牌给脚本。 例如,在Git仓库中交互式运行的Bourne Shell脚本中,其中.gitignore包含/.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