Reputation: 33
I am trying to create a new project in jenkins using groovy. Therefore I'd like to use jenkins script console. The script works fine but if you're looking at the corresponding configuration file the tag <actions/>
is missing. Due to the missing tag the project isn't building. The following code works (from here) for me but I don't want to copy another job just to get it works.
def jenkins = hudson.model.Hudson.instance
def template = jenkins.getItem("MyTemplate")
def job = jenkins.copy(template,"MyNewJob")
job.save()
That's my actually script.
//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()
The corresponding config.xml is looking like that:
<?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>
The right form would be:
<?xml version='1.0' encoding='UTF-8'?>
<actions/>
<project>
<description>Just a placeholder for a description</description>
<keepDependencies>false</keepDependencies>
...
Any ideas how to solve the problem?
Upvotes: 3
Views: 2625
Reputation: 12962
Actually if you restart Jenkins, the job should be successfully created. If you don't want to restart Jenkins, just call reload to reload configuration.
Adding following line of code to the end of your groovy script will solve the problem:
parent.reload()
Upvotes: 1