Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How create and configure a new Jenkins Job using groovy?

Tags:

jenkins

groovy

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?

like image 284
Pavel Bernshtam Avatar asked Jun 06 '13 13:06

Pavel Bernshtam


People also ask

How do I create a Jenkins Groovy script?

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.

How do you create a Jenkins job and what are the types?

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.

How Groovy is used in Jenkins?

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.


4 Answers

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

like image 92
Der_Meister Avatar answered Sep 18 '22 14:09

Der_Meister


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}")
        }
    }
}
like image 21
KeepCalmAndCarryOn Avatar answered Sep 19 '22 14:09

KeepCalmAndCarryOn


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

like image 39
Kenneth Baltrinic Avatar answered Sep 19 '22 14:09

Kenneth Baltrinic


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()
like image 41
qxo Avatar answered Sep 18 '22 14:09

qxo