2013-06-06 19 views
8

harika komut (http://scriptlerweb.appspot.com/catalog/list) yeni iş yaratma hayır hayır örneğinin bir çok örnek vardır.Groovy'yi kullanarak yeni bir Jenkins Job nasıl oluşturulur ve yapılandırılır?

+0

[bu] (http bakın: //stackoverflow.com/a/8803743/2051952) ve [this] (htt Bazı içgörüler için p: //stackoverflow.com/questions/10413936/creating-a-jenkins-environment-variable-using-groovy). – dmahapatro

+0

[Configure or Create hudson job automatically] 'ın olası kopyası (http://stackoverflow.com/questions/3886892/configure-or-create-hudson-job-automatically) –

cevap

3

Jenkins eklenti Job DSL Plugin/oluşturmak için, mevcut işlerini değiştirmek için işlerin içine adımları ekleyebilir. , Yeni bir iş için config.xml içeren bir XML dizesi var olduğu göz önüne alındığında

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}") 
     } 
    } 
} 
2

:

Burada git deposunda her şube için iş oluşturduğunda eklentinin web sitesinden gelen örnektir Aşağıdaki groovy script istediğinizi yapacağız. Daha fazla ayrıntı için

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) 

API Docs

+2

XML kullanmak kaçınmaya çalıştığım şeydir. Sadece groovy kodunu okumayı ve sürdürmeyi zorlaştırır. –

5

SCM gelen işi Boru Hattı komut dosyası oluşturun bkz:

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() 

Başka bir örnek: https://github.com/linagora/james-jenkins/blob/master/create-dsl-job.groovy

0
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() 
İlgili konular