2013-05-29 41 views
0

我必须使用PHP创建网页,用户可以从下拉框中选择参数并在远程创建/构建作业。如何使用PHP创建/触发新的jenkins作业?

我已经成功登录到使用curl詹金斯,但我不知道如何创建一个作业或从网页配置config.xml中。任何建议?

代码

< --- --- ligin.php>

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

< --- --- login_jenkin.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_jenkin和createjob.php在一起,硬编码的所有用户表单数据,它工作得很好

任何想法,为什么会出现这种情况?

回答

3

詹金斯支持API调用来触发作业。它被称为远程访问API,见https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API

For a job with no parameters, you need merely do an HTTP GET on

JENKINS_URL/job/JOBNAME/build?token=TOKEN

where TOKEN is set up in the job configuration.

但是,由于您有参数,您需要使用JSON有效负载进行POST。如何在PHP中使用cURL来实现这个功能的一个例子,David Walsh在这里解释得很好http://davidwalsh.name/curl-post

从你的网页

因此,采取表单字段和提交调用相应的API,你想打的工作。

+0

尼斯后由大卫然而,认证通过后,我试图执行像创建其他行动,建立却收到一个错误“登录在此服务器上找到”任何想法?如何解决这个问题? –

+0

你可以发布你的API调用和完整的响应(错误信息)? – blotto

+0

嗨Blotto中,这是我在下一答复 –

相关问题