<pre><code>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()
</code></pre>

<p>Thanks a lot Michael Durrant for giving that link !</p>

CC BY-SA 3.0
2018-03-15T09:14:03.597Z
2018-03-15T09:14:03.597Z
Alter_so
<p>I'm trying to setup a Jenkins server with Groovy script:</p>

<pre><code>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()
</code></pre>

<p>Now I need to set a repo branch - How do I do this?</p>

CC BY-SA 3.0
2017-12-19T08:10:57.390Z
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 ! This is strange - i've seen before but didn't manage to use the answers as i only noticed the jobDsl code fragments that time...
Alter_so
CC BY-SA 3.0
2018-03-11T17:49:17.740Z
Please post this as an answer. This Q&A pops up in the unanswered section while is has already been answered.
030
<jenkins><continuous-integration><git><release-management><groovy>
How to set a Git repository when creating a job in Jenkins from a Groovy script?
Alter_so
# How to set a Git repository when creating a job in Jenkins from a Groovy script?

(Asked by: username_0 on 2017-12-17)

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?

**Tags:** jenkins, continuous-integration, git, release-management, groovy

> **Comments:**
> **username_0**: 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 ! This is strange - i've seen before but didn't manage to use the answers as i only noticed the jobDsl code fragments that time...  
> **username_1**: Please post this as an answer. This Q&A pops up in the unanswered section while is has already been answered.  

---

## 1 answers

### Answer by username_0 on 2018-03-15. Score: 1 | Accepted answer

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 !


---

