16

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?

Bruce Becker
  • 335
  • 1
  • 6
  • 23
Pavel Bernshtam
  • 4,232
  • 8
  • 38
  • 62
  • Refer [this](http://stackoverflow.com/a/8803743/2051952) and [this](http://stackoverflow.com/questions/10413936/creating-a-jenkins-environment-variable-using-groovy) for some insight. – dmahapatro Jun 07 '13 at 02:14
  • possible duplicate of [Configure or Create hudson job automatically](http://stackoverflow.com/questions/3886892/configure-or-create-hudson-job-automatically) – Mark O'Connor Jun 07 '13 at 23:51

4 Answers4

13

Create Pipeline script from SCM job:

import hudson.plugins.git.*;

def scm = new GitSCM("git@github.com: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

Der_Meister
  • 4,771
  • 2
  • 46
  • 53
  • How do you add credentials to the git checkout? – Shaun Mar 27 '18 at 10:15
  • 1
    I have SSH keys and known_hosts in .ssh directory in jenkins user profile. You may try to use the second constructor: new GitSCM([new UserRemoteConfig('ssh://example.com', null, null, 'jenkins-ssh-credential')], [new BranchSpec('*/develop')], false, null, null, null, null) – Der_Meister Mar 27 '18 at 15:38
  • 1
    https://github.com/jenkinsci/git-plugin/blob/master/src/main/java/hudson/plugins/git/GitSCM.java#L193 – Der_Meister Mar 27 '18 at 15:39
6

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}")
        }
    }
}
Boris Brodski
  • 8,425
  • 4
  • 40
  • 55
KeepCalmAndCarryOn
  • 8,817
  • 2
  • 32
  • 47
4

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

Kenneth Baltrinic
  • 2,941
  • 2
  • 28
  • 45
3
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()
qxo
  • 1,584
  • 15
  • 11
  • Works fine for me. It seems the API has changed since, I had to add third parameter to constructor: ` (new jenkins.branch.RateLimitBranchProperty.Throttle (60,"hours",true)));` – Jürgen Jatzkowski Aug 07 '18 at 06:43