在Jenkins中远程创建作业并不能完全创建作业

时间:2014-12-11 17:42:03

标签: php jenkins

我正在尝试使用Jenkin的API创建一份工作。它正在创建作业,但不使用XML中指定的参数。

在这种情况下,MyJob已创建,但在查看作业时它没有TEST_PARAM。我必须手动创建它。

此XML主要来自现有作业,但具有修改后的参数。

PHP代码:

$url = 'https://jenkins_url.com/createItem?name=MyJob';

$file = '<?xml version="1.0" encoding="UTF-8"?>
<project>
    <actions />
    <description></description>
    <logRotator class="hudson.tasks.LogRotator">
        <daysToKeep>-1</daysToKeep>
        <numToKeep>20</numToKeep>
        <artifactDaysToKeep>-1</artifactDaysToKeep>
        <artifactNumToKeep>-1</artifactNumToKeep>
    </logRotator>
    <keepDependencies>false</keepDependencies>
    <properties>
        <hudson.model.ParametersDefinitionProperty>
            <hudson.model.StringParameterDefinition>
                <name>TEST_PARAM</name>
                <description />
                <defaultValue></defaultValue>
            </hudson.model.StringParameterDefinition>
        </hudson.model.ParametersDefinitionProperty>
        <com.sonyericsson.rebuild.RebuildSettings
            plugin="rebuild@1.20">
            <autoRebuild>false</autoRebuild>
        </com.sonyericsson.rebuild.RebuildSettings>
    </properties>
    <scm class="hudson.scm.NullSCM" />
    <canRoam>true</canRoam>
    <disabled>false</disabled>
    <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
    <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
    <authToken>12345678910</authToken>
    <triggers />
    <concurrentBuild>false</concurrentBuild>
    <builders />
    <publishers />
    <buildWrappers />
</project>';

$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,$url);
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl_handle,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($curl_handle,CURLOPT_POST,1);
curl_setopt($curl_handle,CURLOPT_POSTFIELDS, $file);
curl_setopt($curl_handle,CURLOPT_USERPWD, "user:pass");
curl_setopt($curl_handle,CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));

$buffer = curl_exec($curl_handle);
print_r(curl_getinfo($curl_handle));
curl_close($curl_handle);
print_r($buffer);

1 个答案:

答案 0 :(得分:2)

查看带参数的作业的config.xml,显示您的XML文件与Jenkins 1.565.3上的输出相比有点不同。

尝试使用parameterDefinitions(在ParametersDefinitionProperty元素内)包装参数:

<properties>
  <hudson.model.ParametersDefinitionProperty>
    <parameterDefinitions>
        <hudson.model.StringParameterDefinition>
            <name>TEST_PARAM</name>
            <description />
            <defaultValue></defaultValue>
        </hudson.model.StringParameterDefinition>
        <!-- more parameters go here -->
    </parameterDefinitions>
  </hudson.model.ParametersDefinitionProperty>      
  ...