2

我必须使用 PHP 创建网页,用户可以在其中从下拉框中选择参数并在 jenkins 上远程创建/构建作业。

我已经使用 CURL 成功登录到 jenkins,但我不知道如何创建作业或从网页配置 config.xml。
有什么建议么?

代码

<---登录.php--->

<form action="login_jenkins.php" method="post">
<fieldset>
    <input type="text" name="username">
    <input name="password">
    <button>
    Login
    </button>           
</fieldset>

<---login_jenkins.php--->

<?php
$url="http://jenkinurl/";
$username=$_POST['username'];
$password=$_POST['password'];
$cookies = '/tmp/cookies.txt';
$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_TIMEOUT, 40); 
curl_setopt($ch, CURLOPT_HEADER, 1);  
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_COOKIEJAR, '$cookie');
curl_setopt($ch, CURLOPT_COOKIEFILE, '$cookie');
curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US;           rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); 
curl_setopt ($ch, CURLOPT_URL, $url);  
curl_setopt($ch, CURLOPT_HTTPHEADER,array("Authorization: Basic " . base64_encode($username . ":" . $password)));
$result = curl_exec ($ch); 
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
if ($http_code=='200'){
header('Location: fill_job_form.php');
}
if (!$result) { 
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
    curl_close($ch); // make sure we closeany current curl sessions 
    die($http_code.' Unable to connect to server. Please come back later.'); 
} 

?>

<---fill_job_form.php--->

<form action="createjob.php" method="post">
  <fieldset>
    tags for filling job form goes here
    <button>
      Login
    </button>           
 </fieldset>

<---createjob.php--->

<?php
$url="http://jenkin url/createItem?name=mynewtestjob"; 
$input1=$_POST['input1'];
//get all other inputs and created request data xml 
// hard coded for now....
$req_data="<?xml version='1.0' encoding='UTF-8'?><project><actions/><description></description><keepDependencies>false</keepDependencies><properties/><scm class='hudson.scm.NullSCM'/><canRoam>true</canRoam><disabled>false</disabled><blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding><blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding><triggers class='vector'/><concurrentBuild>false</concurrentBuild><builders/><publishers/><buildWrappers/></project>";
$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_TIMEOUT, 40); 
curl_setopt($ch, CURLOPT_HEADER, 1);  
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt( $ch, CURLOPT_COOKIE, '/tmp/cookies.txt' );
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req_data);

$result = curl_exec ($ch); 
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
print_r($http_code);
print_r($result);
//prints :The requested URL /login was not found on this server.
?>

我能够成功登录,但在创建 job.php 时出现以下错误:在此服务器上找不到请求的 URL /login。但是,如果我将 login_jenkin 和 createjob.php 合并在一起并对所有用户表单数据进行硬编码,则效果很好

任何想法,为什么会这样?

4

1 回答 1

5

Jenkins 支持 API 调用来触发作业。它称为远程访问 API,请参阅https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API

对于没有参数的作业,您只需执行 HTTP GET on

JENKINS_URL/job/JOBNAME/build?token=TOKEN

其中 TOKEN 在作业配置中设置。

但是,由于您有参数,因此您需要使用 JSON 有效负载进行 POST。David Walsh 在这里http://davidwalsh.name/curl-post很好地解释了如何在 PHP 中使用 cURL 执行此操作的示例。

因此,从您的网页中,获取表单字段并在提交时调用适当的 API 到您希望完成的工作。

于 2013-05-30T15:31:22.847 回答