Domando los bits
  • Blog
  • About/Acerca
  • Contact(o)

Jenkins and branch selection in GIT

4/13/2014

1 Comment

 
Now we are migrating to GIT from SVN.

We're very happy with GIT (but a little confused at first).

One problem is selecting in Jenkins a branch.  Jenkins available plugins don't help for this task.  Luckily, I've found this wonderful article that has helped me.  But I've improved a little, and instead of launching a cron job for gathering the available branches, I calculate them on the fly.

I've created a parameter of type Extensible Choice, and added this script in other to feed it:

 def ej(cmd) {   def proc = cmd.execute(null, new File("/home/jenkins/jobs/MyProject/workspace"))   def sb = new StringBuffer()   proc.waitForProcessOutput(sb, sb)   def cod = proc.exitValue()   if (cod != 0)       throw new Exception("Error ${cod} when invoking '${cmd}':\n${sb}")   sb.toString() }  ej("git fetch --all") ej("git remote prune origin")  def re = ~/->/  ej("git branch -r").tokenize("\n").grep({!re.matcher(it).find()}).collect() {   it.replace("origin/", "").trim() } 
Even though, I'm not an expert in Groovy, it works ok.

It's easy to debug thanks to the button Run the Script Now.

Now you can easily select a branch with a combo box!!.
1 Comment
Piotr Soróbka
10/2/2014 03:29:13 pm

Just a small tweak - you've hardcoded the path, I've changed it to variable.

And one more thing - don't use the one line input, use multiline.
And check the "Use predefined variables in scripts".
def ej(cmd) {
def proc = cmd.execute(null, new File(project.getWorkspace().toURI()))
def sb = new StringBuffer()
proc.waitForProcessOutput(sb, sb)
def cod = proc.exitValue()
if (cod != 0)
throw new Exception("Error ${cod} when invoking '${cmd}':\n${sb}")
sb.toString()
}
ej("git fetch --all")
ej("git remote prune origin")
def re = ~/->/
ej("git branch -r").tokenize("\n").grep({!re.matcher(it).find()}).collect(){it.replace("origin/", "").trim()}

Reply



Leave a Reply.

    Author

    Specialist in Open Source Devel / Especialista en desarrollo Open Source

    Archives

    April 2014

    Categories

    All

    RSS Feed


Powered by Create your own unique website with customizable templates.