[英]How to set git branch for a Jenkins job with groovy
I am looking for a way to create multiple projects using Groovy.我正在寻找一种使用 Groovy 创建多个项目的方法。 It is expected that the number of builder on our Jenkins will grow wildly.预计我们 Jenkins 上的构建器数量将疯狂增长。
I've already a simple mechanism to read a JSON file and create the other projects.我已经有了一个简单的机制来读取 JSON 文件并创建其他项目。 However, I am stuck on process to configure Git information on this projects.但是,我坚持在此项目上配置 Git 信息的过程。
I've did this:我这样做了:
def repository = 'my repo....'
job.scm = new hudson.plugins.git.GitSCM(repository)
But, with this GitSCM constructor I can only set the repository, but none o other configuration.但是,使用这个 GitSCM 构造函数,我只能设置存储库,而不能设置其他配置。 And I didn't find another way to set each configuration.而且我没有找到另一种设置每个配置的方法。
Someone know how can I set: branch, credentials, etc.有人知道我该如何设置:分支、凭据等。
Thanks!谢谢!
Another option, if you don't want to use/learn the DSL:另一种选择,如果您不想使用/学习 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()
I think this is a job for Jenkins Job-dsl plugin我认为这是Jenkins Job-dsl 插件的工作
which allows you to build (and maintain) other projects in Jenkins as another project.这允许您在 Jenkins 中构建(和维护)其他项目作为另一个项目。 This is implemented as a build step with its own DSL这是作为构建步骤使用自己的 DSL 实现的
job {
name 'GitJob'
scm {
git('git://github.com/JavaPosseRoundup/job-dsl-plugin')
}
}
From the git section of the job reference来自作业参考的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')
}
I've found a way, that not make me happy, but...我找到了一种方法,它不会让我快乐,但是......
This method need a template project, and on this case, this basic project mus have GIT configured as SCM.这种方法需要一个模板工程,在这种情况下,这个基础工程必须有GIT配置为SCM。 The process only change project repository and branch to build.该过程只更改项目存储库和分支来构建。
// 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)
This made my project to be create dynamically.这使我的项目成为动态创建的。
try this and hope it helps:试试这个,希望它有帮助:
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.