How create and configure a new Jenkins Job using groovy?

前端 未结 4 1367
被撕碎了的回忆
被撕碎了的回忆 2020-12-15 08:22

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 on

相关标签:
4条回答
  • 2020-12-15 09:02
    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()
    
    0 讨论(0)
  • 2020-12-15 09:03

    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}")
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-15 09:21

    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

    0 讨论(0)
  • 2020-12-15 09:25

    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

    0 讨论(0)
提交回复
热议问题