我必須使用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在一起,硬編碼的所有用戶表單數據,它工作得很好
任何想法,爲什麼會出現這種情況?
尼斯後由大衛然而,認證通過後,我試圖執行像創建其他行動,建立卻收到一個錯誤「登錄在此服務器上找到」任何想法?如何解決這個問題? –
你可以發佈你的API調用和完整的響應(錯誤信息)? – blotto
嗨Blotto中,這是我在下一答覆 –