如何使用PHP创建/触发新的詹金斯工作? [英] How to create/trigger a new jenkins job using PHP?

查看:117
本文介绍了如何使用PHP创建/触发新的詹金斯工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我已经使用CURL成功登录了jenkins,但是我不知道如何从Web页面创建作业或配置config.xml.有任何建议吗?

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 suggestion???

< --- ligin.php --->

<---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 --->

<---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 --->

<---fill_job_form.php--->

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

< --- createjob.php --->

<---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合并在一起,并对所有用户表单数据进行硬编码,则效果很好

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?

推荐答案

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

Jenkins supports API calls to trigger jobs. Its called Remote Access API , see https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API.

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

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

JENKINS_URL/职位/JOBNAME/build?token = TOKEN

JENKINS_URL/job/JOBNAME/build?token=TOKEN

在作业配置中设置了令牌的地方.

where TOKEN is set up in the job configuration.

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

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.

因此,从您的网页中获取表单字段,并在提交时调用您要查找的工作的相应API.

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

这篇关于如何使用PHP创建/触发新的詹金斯工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆