有很多 groovy 脚本的示例(http://scriptlerweb.appspot.com/catalog/list)但是我没有找到新工作创建的示例。网上有没有一个很好的例子来说明应该如何做到这一点?
问问题
40297 次
4 回答
12
从 SCM作业创建管道脚本:
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()
另一个例子:https ://github.com/linagora/james-jenkins/blob/master/create-dsl-job.groovy
于 2017-05-26T14:10:57.493 回答
6
Jenkins 插件 Job DSL Plugin可以将步骤添加到作业中以创建/修改现有作业。
这是插件网站的示例,它为 git 存储库中的每个分支创建一个作业:
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}")
}
}
}
于 2013-06-20T10:08:34.967 回答
3
假设您有一个包含新作业的 config.xml 的 XML 字符串,以下 groovy 脚本将执行您想要的操作。
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 文档
于 2015-01-27T17:33:47.113 回答
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()
于 2017-09-29T14:16:41.800 回答