我想我的多分支管道每40分钟运行一次,我想通过groovy配置作业,这很难做到。下面是我的Groovy代码。
import com.cloudbees.hudson.plugins.folder.*
import com.cloudbees.hudson.plugins.folder.computed.DefaultOrphanedItemStrategy
import com.cloudbees.jenkins.plugins.bitbucket.*
import jenkins.branch.*
import jenkins.plugins.git.*
import jenkins.scm.impl.trait.*
import org.jenkinsci.plugins.workflow.multibranch.*
import jenkins.branch.NoTriggerBranchProperty
import jenkins.branch.NoTriggerMultiBranchQueueDecisionHandler.SuppressionStrategy
import jenkins.branch.DefaultBranchPropertyStrategy
import jenkins.*
import jenkins.model.*
import hudson.*
import hudson.model.*
import hudson.triggers.*
def jobName = "PipelineBuild"
def bitbucketUrl = "https://bitbucket.com"
def credentialIdForBitbucket = "12346790"
def repositoryName = "ApplicationRepo"
def projectOwner = "Loop"
def cronExpression = "H/40 * * * *" // Every 40 minutes
def multibranch = Jenkins.instance.getItemByFullName(jobName, WorkflowMultiBranchProject)
if (!multibranch) {
multibranch = Jenkins.instance.createProject(WorkflowMultiBranchProject, jobName)
}
def branch = new BitbucketSCMSource(projectOwner, repositoryName)
branch.credentialsId = credentialIdForBitbucket
branch.serverUrl = bitbucketUrl
branch.traits = [
new BranchDiscoveryTrait(3), // All branches
]
multibranch.getProjectFactory().setScriptPath("ServerApiJenkinsBuild")
branchsource = new BranchSource(branch)
multibranch.setSourcesList([branchsource])
multibranch.setDescription("")
multibranch.setDisplayName(jobName)
multibranch.setOrphanedItemStrategy(new DefaultOrphanedItemStrategy(true, "30", "30"))
def periodicTrigger = new TimerTrigger(cronExpression)
multibranch.addTrigger(periodicTrigger)
multibranch.save()
Jenkins.instance.reload()
Thread.start {
sleep 3000 // 3 seconds
multibranch.scheduleBuild()
}
脚本成功运行,但未设置触发器。下面是作业配置
的图片
有什么想法可以实现这一目标吗?
1条答案
按热度按时间huus2vyu1#
我认为你可以尝试使用
PeriodicFolderTrigger
类而不是TimerTrigger
。如果您不知道示例化哪个类,可以手动修改作业,然后通过将
/config.xml
附加到URL来查看xml配置。这就是我如何发现的:
还有两件事
1.我建议您使用JobDSL插件来自动创建工作。
1.此触发器将启动“扫描多分支管道”作业,而不是作业本身。如果你想让作业在触发器上运行,我建议在Jenkinsfile中指定cron触发器:https://www.jenkins.io/doc/book/pipeline/syntax/#triggers
祝你今天愉快!