HiveBrain v1.2.0
Get Started
← Back to all entries
snippetgitMinor

How to set a Git repository when creating a job in Jenkins from a Groovy script?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
scripthowcreatingjenkinsgitrepositorywhenfromjobset

Problem

I'm trying to setup a Jenkins server with Groovy script:

def repository = 'git@somerepo'
import hudson.tasks.Shell;

job = Jenkins.instance.createProject(FreeStyleProject, 'TestJob')
job.setDescription("Some description")
job.displayName = 'SomeTestJob(TESTING groovy)'

job.scm = new hudson.plugins.git.GitSCM(repository)
job.save()


Now I need to set a repo branch - How do I do this?

Solution

def repository = 'git@somerepo' 
import hudson.tasks.Shell
job = Jenkins.instance.createProject(FreeStyleProject, 'TestJob')
job.setDescription("Some description") 
job.displayName = 'SomeTestJob(TESTING groovy)' 
job.scm = new hudson.plugins.git.GitSCM(repository) 
job.scm.branches = [new BranchSpec('*/master')]
job.save()


Thanks a lot Michael Durrant for giving that link !

Code Snippets

def repository = 'git@somerepo' 
import hudson.tasks.Shell
job = Jenkins.instance.createProject(FreeStyleProject, 'TestJob')
job.setDescription("Some description") 
job.displayName = 'SomeTestJob(TESTING groovy)' 
job.scm = new hudson.plugins.git.GitSCM(repository) 
job.scm.branches = [new BranchSpec('*/master')]
job.save()

Context

StackExchange DevOps Q#2917, answer score: 1

Revisions (0)

No revisions yet.