Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use GIT commands (diff, log, etc.) in Groovy script (windows)

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.

like image 957
rock Avatar asked Sep 13 '25 03:09

rock


2 Answers

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,

like image 64
Juan Sebastian Avatar answered Sep 14 '25 20:09

Juan Sebastian


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/

like image 26
daggett Avatar answered Sep 14 '25 21:09

daggett