Showing posts with label Git. Show all posts
Showing posts with label Git. Show all posts

Friday, September 27, 2013

Jenkins - Git remote brach choice parameter

  1. Generate a Jenkins property file for each project's git remote branch list

    First of all, There is a recursive scheduled job to generate each project's property files which contains each own git remote repository's branches list and keep that file up to date.

    1. I made a below script to make a Jenkins property file by using Groovy 

      import hudson.model.*
        
      // reference all Jenkins projects 
      for(job in Hudson.instance.items)
      {
       // it will filtering out for only projects which has git SCM (Source Code Management) setting
       if(job.scm instanceof hudson.plugins.git.GitSCM ) {
       url = job.scm.getRepositories()[0].getURIs()[0].toASCIIString()
        Process p = "c:/Progra~2/git/bin/git.exe ls-remote -h $url".execute()
         branches = p.text.findAll(/(?m)heads\/(.*?)$/) { match, branch -> return branch }
         .findAll { it != "master" }
         .sort()
        
       branches.add(0, "master") 
        
       def folder = new File(job.workspace.toString(), "properties")
       if(!folder.exists()) { folder.mkdirs() } // make a properties folder if it's not existed.
        
       def props_file = new File(folder, "branches.properties")
        
       // create or update a properties file
       props_file.withWriter { out ->
          out.println "branches=" + branches.toString().replace("[", "").replace("]", "")
       }
       }
      }
      
    2. Implemented above Groovy script into a Job by using groovy build script plugin
    3. Setup some recursive schedule in order to update the file to be up to date.
  2. Setup a extended choice parameter with the properties file.
    just add extended choice parameter with below settings
    Property file : each property file is stored under each job project's workspace directory with below path
    c:/program files (x86)/jenkins/workspace/{project name}/properties/branches.properties

Git branch modeling

There is a really good reference : http://nvie.com/posts/a-successful-git-branching-model/

There are two central repo (remote) branches with an infinite lifetime.
  • master
  • develop
There are three limited lifetime branches.
  • feature branches
  • release branches (named as "branch-{minor version}" )
  • hot-fixes (named as "hotfix-{revision version}" )
if there is a hot-fix issue and at the same time there is a existed release branch, it needs to be merged into the release branch. otherwise, needs to be merged into develop.

Jenkins - get a local remote branch names

def job = hudson.model.Hudson.instance.getItem("$Jobname")

Process p = "git.exe branch -r".execute(null, new File(job.workspace.toString()))

p.text.findAll(/(?m)origin\/(.*?)$/) { match, branch -> return branch }