如何通过 curl 禁用 Jenkins 作业?

How to disable a Jenkins job via curl?

我想通过向 Jenkins 发送 post curl 请求来禁用 Jenkins 作业。

我试过使用:

  1. curl -X POST http://<server>:8080/<jobname>/disable
  2. curl -X POST http://<server>:8080/<jobname>/disable?token=<token>
  3. curl -u <username>:<token> POST http://<server>:8080/<jobname>/disable

但每次都失败了。我得到的错误是:

403 no valid crumb was included in the request

这个问题有基于 curl 的好的解决方案吗?

以下对我有用

curl -X POST http://<servername>/job/jobname/disable

确保用户有权执行此操作。

crumb 错误表明您正在为 "Trigger parameterized build with curl and crumb" 使用 CSRF Protection. You need to include a proper crumb header in your request. The crumb can be obtained from the Jenkins API as described on the Jenkins wiki page linked above. The answer 显示了在 curl 请求中添加 crumb header 的语法。

没有有效的碎屑意味着您的 Jenkins 安装启用了安全选项,该选项阻止以标准方式发送请求以避免 one-click attacks. You can't use Jenkins CLI either, because it doesn't work yet

以下是使用 curl 的步骤(将 localhost 替换为您的 Jenkins 地址):

  1. 记下您的用户 API 令牌(来自 /user/USER/configure)。
  2. 得到你的面包屑:

    CRUMB=$(curl -s 'http://USER:TOKEN@localhost:8080/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)')
    
  3. 现在您可以通过在 headers:

    中发送面包屑来禁用该作业
    curl -X POST -H "$CRUMB" http://USER:TOKEN@localhost:8080/<jobname>/disable
    

    如果上述方法由于某些原因无法正常工作,您可以尝试使用 -u USER:TOKEN

我发现 的第一部分对我有用,即获取面包屑,但对于第二部分,curl 不喜欢这种语法,它说:

curl: (6) Couldn't resolve host 'http:'

所以我不得不使用以下有效的语法:

curl -H $CRUMB http://localhost:8080/<jobname>/disable -u USER:TOKEN

设置詹金斯的"global security settings": 取消选中 "Prevent Cross Site Request Forgery exploits"