最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to createtrigger a new jenkins job using PHP? - Stack Overflow

programmeradmin0浏览0评论

I have to create web page, using PHP, where user can select parameters from drop box and create/ build a job on jenkins remotely.

I have successfully logged in to jenkins using CURL but I have no idea how to create a job OR configure config.xml from the web page.
Any suggestions?

Code

<---login.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 e 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.
?>

I am able to login successfully but at create job.php i am getting following error: The requested URL /login was not found on this server. however if i merge login_jenkin and createjob.php together and hard code all user form data , it works very well

any idea , why is this happening?

I have to create web page, using PHP, where user can select parameters from drop box and create/ build a job on jenkins remotely.

I have successfully logged in to jenkins using CURL but I have no idea how to create a job OR configure config.xml from the web page.
Any suggestions?

Code

<---login.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 e 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.
?>

I am able to login successfully but at create job.php i am getting following error: The requested URL /login was not found on this server. however if i merge login_jenkin and createjob.php together and hard code all user form data , it works very well

any idea , why is this happening?

Share Improve this question edited Feb 17, 2020 at 16:42 Kaspar Lee 5,5965 gold badges33 silver badges54 bronze badges asked May 29, 2013 at 20:40 Rajeev RanjanRajeev Ranjan 1071 silver badge6 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Jenkins supports API calls to trigger jobs. Its called Remote Access API , see https://wiki.jenkins-ci/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.

However since you have parameters you need to POST with a JSON payload. An example of how to do this in PHP using cURL is explained pretty well by David Walsh here http://davidwalsh.name/curl-post.

So from your web page, take the form fields and on submit call the appropriate API to the job you wish to hit.

发布评论

评论列表(0)

  1. 暂无评论