2

I just started learning DevOps and have a query. It might be very basic so please don't mind.

Setup: Jenkins, GIT, Groovy, Java are installed on single windows server.

My Goal is to write a Groovy script which will do following: 1. Execute GIT commands (on local GIT repository) to pull some data (result). 2. Take further actions based on above result.

Query: How to execute GIT commands in Groovy script? What all is needed? Would be great if someone can please share a sample basic script.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
rock
  • 25
  • 1
  • 1
  • 7

3 Answers3

7

On a broader spectrum, what you want to achieve is just call linux commands from groovy, now regarding that:

There are 3 ways out of this, either you can just call the git commands from a shell script (since i understand you want to use jenkins for this), use some sort of git jenkins plugin, or if you absolutely want to use groovy for it, you can take a look at this question Groovy executing shell commands , to summarize, you can do the following:

def proc = "git command args".execute()
def b = new StringBuffer()
proc.consumeProcessErrorStream(b)

println proc.text
println b.toString()

on b you would have the errors of executing the linux command if there were any,

Best Regards,

Juan Sebastian
  • 968
  • 1
  • 7
  • 20
  • Hi, thanks a lot. Able to run git commands in Groovy script using code you shared. :) Just a quick query: currently my git repository resides on same machine as groovy, so I was able to run following git command: git --git-dir="C:/GIT/New/.git" diff .. How do I execute same when my git repository resides on a remote/different windows machine? – rock Aug 31 '17 at 10:25
  • thanks a lot. I was able to run git commands in Groovy script using code you shared. :) Just a quick query: currently my git repository resides on same machine as groovy, so I was able to run following git command: def proc = "git --git-dir="C:/GIT/New/.git" diff".execute() .. How do I execute same when my git repository resides on a remote/different windows machine? – rock Aug 31 '17 at 10:33
  • you just prepend "ssh user@yourremoteserver" inside the string that containes the git command, bear in mind that you are dealing with linux commands now, also, please accept the answer if it worked :D – Juan Sebastian Aug 31 '17 at 19:10
  • Juan Sebastian: Sure, I will try that and get back. :) – rock Sep 01 '17 at 03:33
  • you would need to set up ssh keys between the servers before this can work, check this out https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys--2 – Juan Sebastian Sep 01 '17 at 12:25
2

check jenkins pipeline examples

https://jenkins.io/doc/pipeline/examples/

simplest pipeline with git:

node {
    stage('Clone sources') {
        git url: 'https://github.com/jfrogdev/project-examples.git'
    }
}

git pipeline plugin doc:

https://jenkins.io/doc/pipeline/steps/git/

daggett
  • 26,404
  • 3
  • 40
  • 56
1

In Jenikins>manageJenkins>Script Console

execute following command

println(["git","--version"].execute().text)