我有几个多分支管道作业,我需要在所有作业中进行设置。我知道我可以点击,但如何在 Jenkins 脚本控制台中以编程方式设置它们?它们是作业配置页面的
Branch Sources -> Behaviors
块。我知道如何搜索我需要的所有工作,只需要这些设置的特定代码。谢谢!
您可以使用脚本控制台中的 groovy api 来配置您的作业,如下所示:
import com.cloudbees.hudson.plugins.folder.*
import com.cloudbees.hudson.plugins.folder.computed.DefaultOrphanedItemStrategy
import jenkins.branch.*
import jenkins.plugins.git.*
import jenkins.plugins.git.traits.*
import jenkins.scm.impl.trait.*
import org.jenkinsci.plugins.workflow.multibranch.*
import jenkins.*
import jenkins.model.*
import hudson.*
import hudson.model.*
import hudson.triggers.*
def jobName = "my_job_name"
def job = Jenkins.instance.getItemByFullName(jobName, WorkflowMultiBranchProject)
// Initiate the SCM source with the repository URL
def source = new GitSCMSource("my_repo_url")
// Set the Git credentials ID
source.credentialsId = "my_id"
// Set the Behaviors (Traits)
source.setTraits([
new BranchDiscoveryTrait(), // Discover Branches
new TagDiscoveryTrait(), // Discover Tags
new WildcardSCMHeadFilterTrait("master feature/* patch/*","staging"), // Filter by name (with wildcards)
new RegexSCMHeadFilterTrait(''), // Filter by name (with regular expression)
])
// Set the source
job.setSourcesList([new BranchSource(source)])
// Set other job parameters
job.setDescription("")
job.setDisplayName(jobName)
job.setOrphanedItemStrategy(new DefaultOrphanedItemStrategy(true, "10", "10"))
// Save the Job
job.save()
有关更多可用特征,请参阅以下API