我正在尝试使用groovy在jenkins中创建一个新项目。因此,我想使用jenkins脚本控制台。该脚本工作正常,但如果您查看相应的配置文件,则标记<actions/>
将丢失。由于缺少标签,项目无法建设。以下代码适用于我(from here),但我不想复制另一份工作只是为了让它有效。
def jenkins = hudson.model.Hudson.instance
def template = jenkins.getItem("MyTemplate")
def job = jenkins.copy(template,"MyNewJob")
job.save()
这是我的实际剧本。
//Get instance of Jenkins
def parent = Jenkins.getInstance()
//Define a job name
def jobName = "Job"
//Instantiate a new project
def project = new FreeStyleProject(parent, jobName);
//Set a description for the project
project.setDescription("Just a placeholder for a description")
//Create a parameter for the project
def parameterDefinitions = new ArrayList<ParameterDefinition>();
def name = "ParameterOne"
def defaultValue = "1"
def description = "Just a placeholder for a parameter description"
parameterDefinitions.add(new StringParameterDefinition(name, defaultValue, description) )
//Create a job property for the project
def jobProperty = new ParametersDefinitionProperty(parameterDefinitions);
//Adding and saving the job property to the project
project.addProperty(jobProperty)
project.save()
相应的config.xml看起来像这样:
<?xml version='1.0' encoding='UTF-8'?>
<project>
<description>Just a placeholder for a description</description>
<keepDependencies>false</keepDependencies>
<properties>
<hudson.model.ParametersDefinitionProperty>
<parameterDefinitions>
<hudson.model.StringParameterDefinition>
<name>ParameterOne</name>
<description>Just a placeholder for a parameter description</description>
<defaultValue>1</defaultValue>
</hudson.model.StringParameterDefinition>
</parameterDefinitions>
</hudson.model.ParametersDefinitionProperty>
</properties>
<scm class="hudson.scm.NullSCM"/>
<canRoam>false</canRoam>
<disabled>false</disabled>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<triggers/>
<concurrentBuild>false</concurrentBuild>
<builders/>
<publishers/>
<buildWrappers/>
</project>
正确的形式是:
<?xml version='1.0' encoding='UTF-8'?>
<actions/>
<project>
<description>Just a placeholder for a description</description>
<keepDependencies>false</keepDependencies>
...
任何想法如何解决问题?
答案 0 :(得分:1)
实际上,如果重新启动Jenkins,则应该成功创建作业。如果您不想重新启动Jenkins,只需调用 reload 重新加载配置。
在groovy脚本的末尾添加以下代码行将解决问题:
parent.reload()