简体   繁体   English

如何使用 groovy 为 Jenkins 工作设置 git 分支

[英]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.

相关问题 如何从詹金斯工作推到Git分支? - How to push to Git branch from Jenkins job? 如何使用groovy获取Jenkins作业的git scm url - How to get git scm url for a Jenkins job with groovy 如何在下游作业 B 中使用 Jenkins JOB A 的 git 分支名称作为参数来检出 git 分支? - How to use git branch name of Jenkins JOB A in lower stream job B as a parameter to checkout the git branch? 如何知道哪个 git 分支触发了 jenkins 工作 - how to know which git branch triggered the jenkins job 使用 groovy 脚本在 Jenkins 中选择 Git 分支 - Git branch select in Jenkins with groovy script 如何根据作业的参数在 Jenkins 的管道中设置分支? - How to set branch in Jenkins' Pipeline according to a parameter of the job? 检查 Jenkins Groovy 脚本中是否存在 Git 分支 - Checking if Git branch exists from within Jenkins Groovy script 使用Jenkins,我如何与Git进行比较,将特定的分支名称形成以前的工作,以获取“无事可做”状态 - With Jenkins how do I compare with Git specific branch names form a previous job to get 'nothing to do' status or not 如何在Groovy中运行git过滤器分支 - How to run a git filter-branch in groovy 由 Git 操作触发的 Jenkins 作业构建不正确的分支 - Jenkins job triggered by Git Action building incorrect branch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM