Answers: <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>
  
Answers: CC BY-SA 3.0
Answers: 2018-03-15T09:14:03.597Z
Answers: 2018-03-15T09:14:03.597Z
Answers: Alter_so
Body: <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>
  
Comments: CC BY-SA 3.0
Comments: 2017-12-19T08:10:57.390Z
Comments: 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...
Comments: Alter_so
Comments: CC BY-SA 3.0
Comments: 2018-03-11T17:49:17.740Z
Comments: Please post this as an answer. This Q&A pops up in the unanswered section while is has already been answered.
Comments: 030
Tags: <jenkins><continuous-integration><git><release-management><groovy>
Title: How to set a Git repository when creating a job in Jenkins from a Groovy script?
UserDisplayName: Alter_so
ThreadText: # 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 !
  
  
  ---
  
  