There are a lot of examples of groovy scripts (http://scriptlerweb.appspot.com/catalog/list) however I have found no examples of new job creation. Is there a good example online of how one should do this?
Usage. To create Groovy-based project, add new free-style project and select "Execute Groovy script" in the Build section, select previously configured Groovy installation and then type your command, or specify your script file name. In the second case path taken is relatively from the project workspace directory.
Go to Jenkins top page, select “New Job”, then choose “Build a free-style software project”. Now you can tell the elements of this freestyle job: Optional SCM, such as CVS or Subversion where your source code resides. Optional triggers to control when Jenkins will perform builds.
Within a Pipeline Project (read plugin), Jenkins introduces a domain-specific language (DSL) based on 'Groovy', which can be used to define a new pipeline as a script. The flow that would typically require many “standard” Jenkins jobs chained together, can be expressed as a single script.
Create Pipeline script from SCM job:
import hudson.plugins.git.*;
def scm = new GitSCM("[email protected]:dermeister0/Tests.git")
scm.branches = [new BranchSpec("*/develop")];
def flowDefinition = new org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition(scm, "Jenkinsfile")
def parent = Jenkins.instance
def job = new org.jenkinsci.plugins.workflow.job.WorkflowJob(parent, "New Job")
job.definition = flowDefinition
parent.reload()
Another example: https://github.com/linagora/james-jenkins/blob/master/create-dsl-job.groovy
The Jenkins plugin Job DSL Plugin can add steps into jobs to create/modify existing jobs.
Here is the example from the plugin's web site, that creates a job for each branch in a git repository:
def project = 'quidryan/aws-sdk-test'
def branchApi = new URL("https://api.github.com/repos/${project}/branches")
def branches = new groovy.json.JsonSlurper().parse(branchApi.newReader())
branches.each {
def branchName = it.name
def jobName = "${project}-${branchName}".replaceAll('/','-')
job(jobName) {
scm {
git("git://github.com/${project}.git", branchName)
}
steps {
maven("test -Dproject.name=${project}/${branchName}")
}
}
}
Given that you have an XML string containing the config.xml for the new job, the following groovy script will do what you want.
import jenkins.model.*
def jobName = "my-new-job"
def configXml = "" // your xml goes here
def xmlStream = new ByteArrayInputStream( configXml.getBytes() )
Jenkins.instance.createProjectFromXML(jobName, xmlStream)
For more details see the API Docs
def jobDSL="""
node {
stage("test"){
echo 'Hello World'
}
}
""";
//http://javadoc.jenkins.io/plugin/workflow-cps/index.html?org/jenkinsci/plugins/workflow/cps/CpsFlowDefinition.html
def flowDefinition = new org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition(jobDSL, true);
//http://javadoc.jenkins.io/jenkins/model/Jenkins.html
def parent = Jenkins.instance;
//parent=Jenkins.instance.getItemByFullName("parentFolder/subFolder")
//http://javadoc.jenkins.io/plugin/workflow-job/org/jenkinsci/plugins/workflow/job/WorkflowJob.html
def job = new org.jenkinsci.plugins.workflow.job.WorkflowJob(parent, "testJob")
job.definition = flowDefinition
job.setConcurrentBuild(false);
//http://javadoc.jenkins.io/plugin/branch-api/jenkins/branch/RateLimitBranchProperty.html
job.addProperty( new jenkins.branch.RateLimitBranchProperty.JobPropertyImpl
(new jenkins.branch.RateLimitBranchProperty.Throttle (60,"hours")));
def spec = "H 0 1 * *";
hudson.triggers.TimerTrigger newCron = new hudson.triggers.TimerTrigger(spec);
newCron.start(job, true);
job.addTrigger(newCron);
job.save();
Jenkins.instance.reload()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With