繁体   English   中英

Jenkins使用groovy脚本添加Git行为

[英]Jenkins add Git Behaviors using groovy scripts

我正在使用groovy脚本创建我的Jenkins实例,因为我正在自动化Jenkins的创建过程。 我创建此脚本:

/* Adds a multibranch pipeline job to Jenkins */
import hudson.model.*
import hudson.util.PersistedList
import jenkins.*
import jenkins.branch.*
import jenkins.model.*
import jenkins.model.Jenkins
import jenkins.plugins.git.*
import com.cloudbees.hudson.plugins.folder.computed.PeriodicFolderTrigger
import org.jenkinsci.plugins.workflow.multibranch.*

// Create job
def env = System.getenv()
Jenkins jenkins = Jenkins.instance
String jobName = "Job"
String jobScript = "Jenkinsfile"
def job = jenkins.getItem(jobName)

// Create the folder if it doesn't exist
if (job == null) {
  job = jenkins.createProject(WorkflowMultiBranchProject.class, jobName)
}
job.getProjectFactory().setScriptPath(jobScript)

// Add git repo
String id = null
String remote = env.CODE_COMMIT_URL
String includes = "*"
String excludes = ""
boolean ignoreOnPushNotifications = false
GitSCMSource gitSCMSource = new GitSCMSource(id, remote, null, includes, excludes, ignoreOnPushNotifications)
BranchSource branchSource = new BranchSource(gitSCMSource)

// Remove and replace?
PersistedList sources = job.getSourcesList()
sources.clear()
sources.add(branchSource)
job.addTrigger(new PeriodicFolderTrigger("1m"))

并将其粘贴到$JENKINS_HOME/ref/init.groovy.d/ 当我启动Jenkins时,已经按工作创建了。 除此之外,我需要在工作中添加一些Git行为,我想知道是否可以使用groovy脚本添加Git行为?

创建后的我的Git:

在此处输入图片说明

我想在初始化时添加的Git行为(发现标签,检出匹配的本地分支,自定义用户名/电子邮件地址)

在此处输入图片说明

谢谢!

我认为您想要的是通过特征来管理的(我实际上没有尝试过):

import jenkins.plugins.git.traits.*

def traits = []
// Add your traits...
traits.add(new TagDiscoveryTrait())
traits.add(new LocalBranchTrait())
gitSCMSource.setTraits(traits)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM