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

No comments: