通过PHP远程运行jenkins脚本


run jenkins script remotely via PHP

我写了一个PHP脚本来运行Jenkins build。它工作得很好,但现在我应用了一些安全性并创建了可以运行jenkins构建的用户。

<?php
   echo "Run Jenkins'n";
echo shell_exec("curl -X POST http://198.0.0.21:8080/job/test/build");
echo "'n'n";
?>

现在我有一个用户,例如test,密码为test。我如何重写我的PHP脚本使用这个用户作为认证来运行构建

当我运行上面的脚本时,我得到以下输出:

Authentication required
<!--
You are authenticated as: anonymous
Groups that you are in:
Permission you need to have (but didn't): hudson.model.Hudson.Read
 ... which is implied by: hudson.security.Permission.GenericRead
 ... which is implied by: hudson.model.Hudson.Administer
-->

我找到了解决方案:

$u="test";
$p="test";
echo shell_exec("curl -X POST http://$u:$p@198.0.0.21:8080/job/test/build");

如果您正在运行的jenkins版本大于1.426,您可以生成一个令牌来代替密码,这样您就不会以明文形式泄露您的密码。

见https://wiki.jenkins-ci.org/display/JENKINS/Authenticating +的+客户