[英]How to set git branch for a Jenkins job with groovy
我正在寻找一种使用 Groovy 创建多个项目的方法。 预计我们 Jenkins 上的构建器数量将疯狂增长。
我已经有了一个简单的机制来读取 JSON 文件并创建其他项目。 但是,我坚持在此项目上配置 Git 信息的过程。
我这样做了:
def repository = 'my repo....'
job.scm = new hudson.plugins.git.GitSCM(repository)
但是,使用这个 GitSCM 构造函数,我只能设置存储库,而不能设置其他配置。 而且我没有找到另一种设置每个配置的方法。
有人知道我该如何设置:分支、凭据等。
谢谢!
另一种选择,如果您不想使用/学习 DSL:
def repository = 'your repo...'
job.scm = new hudson.plugins.git.GitSCM(repository)
job.scm.userRemoteConfigs[0].credentialsId = 'your credentials...'
// branches
job.scm.branches[0].name = '*/master'
job.scm.branches[1].name = '*/develop'
job.scm.branches[2].name = '*/release.*'
// extension
identity = new hudson.plugins.git.extensions.impl.UserIdentity('jenkins', 'jenkins@yourdomaine.com')
job.scm.extensions.add(identity)
job.save()
这允许您在 Jenkins 中构建(和维护)其他项目作为另一个项目。 这是作为构建步骤使用自己的 DSL 实现的
job {
name 'GitJob'
scm {
git('git://github.com/JavaPosseRoundup/job-dsl-plugin')
}
}
来自作业参考的git 部分
// checkout repo1 to a sub directory and clean the workspace after checkout
git {
remote {
name('remoteB')
url('git@server:account/repo1.git')
}
clean()
relativeTargetDir('repo1')
}
我找到了一种方法,它不会让我快乐,但是......
这种方法需要一个模板工程,在这种情况下,这个基础工程必须有GIT配置为SCM。 该过程只更改项目存储库和分支来构建。
// Create a new Job from template
def template = hudson.model.Hudson.instance.getItem('Basic Template')
job = hudson.model.Hudson.instance.copy(template, 'New Project')
// Add a description
job.setDescription(currentProject.description)
// Get GitSCM ann change its values
def gitScm = job.scm
// Change REpository
gitScm.userRemoteConfigs[0].url = currentProject.repository
// Change branch to build
gitScm.branches = [new hudson.plugins.git.BranchSpec(currentProject.version)]
// Get the build list
def bld = job.getBuildersList()
bld.clear()
// Add a new shell task
def shell = new hudson.tasks.Shell(valu)
bld.add(shell)
// Add this project to a view.
tView.add(job)
这使我的项目成为动态创建的。
试试这个,希望它有帮助:
def getbranches = ("git ls-remote -h https://github.com/some.git").execute()
return getbranches .text.readLines()
.collect { it.split()[1].replaceAll('refs/heads/', '') }
.unique()
.findAll { it.startsWith('r') }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.