Diego Laucsen
Diego Laucsen

Reputation: 53

How to set git branch for a Jenkins job with groovy

I am looking for a way to create multiple projects using Groovy. It is expected that the number of builder on our Jenkins will grow wildly.

I've already a simple mechanism to read a JSON file and create the other projects. However, I am stuck on process to configure Git information on this projects.

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. And I didn't find another way to set each configuration.

Someone know how can I set: branch, credentials, etc.

Thanks!

Upvotes: 2

Views: 8791

Answers (4)

gaelperret
gaelperret

Reputation: 328

Another option, if you don't want to use/learn the 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', '[email protected]')
job.scm.extensions.add(identity)

job.save()

Upvotes: 3

Kai
Kai

Reputation: 165

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') }

Upvotes: 0

Diego Laucsen
Diego Laucsen

Reputation: 53

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. 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.

Upvotes: 1

KeepCalmAndCarryOn
KeepCalmAndCarryOn

Reputation: 9075

I think this is a job for Jenkins Job-dsl plugin

which allows you to build (and maintain) other projects in Jenkins as another project. This is implemented as a build step with its own DSL

job {
    name 'GitJob'
    scm {
        git('git://github.com/JavaPosseRoundup/job-dsl-plugin')
    }
}

From the git section of the job reference

// 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')
}

Upvotes: 0

Related Questions