2

I want to perform git checkout operation using Groovy in Jenkins. How to achieve that?

Note: The Jenkins job in which I am trying to perform the checkout operation is not a pipeline job, it is a freestyle job and we are executing it under system groovy script.

Vitalii Vitrenko
  • 9,763
  • 4
  • 43
  • 62
ANIL
  • 2,542
  • 4
  • 25
  • 44

1 Answers1

2

You can just invoke git through groovy

["git", "checkout", "master"].execute()
Vitalii Vitrenko
  • 9,763
  • 4
  • 43
  • 62
  • Can you please provide an example for this? – ANIL Aug 02 '17 at 06:58
  • When I am executing the above Groovy script, I am getting the following error in Jenkins: `[‎02-‎08-‎2017 12:54] jenkins user: groovy.lang.MissingMethodException: No signature of method: java.util.LinkedHashMap.execute() is applicable for argument types: () values: [] Possible solutions: compute(java.lang.Object, java.util.function.BiFunction), keySet(), keySet(), keySet(), every()` – ANIL Aug 02 '17 at 08:01
  • @ANILMAHAPATRAOfficial are you sure you're using exactly the same command? Because you got exception about LinkedHashMap but the above Groovy script uses List. Please put full context in which you are executing it. – Vitalii Vitrenko Aug 02 '17 at 08:16
  • I am executing the command like this: ["git":'https://repo.org.com/gitlab/proj/repo.git' , "checkout": '/Baseline/Package/Logistics/'].execute() – ANIL Aug 02 '17 at 08:19
  • @ANILMAHAPATRAOfficial You shod not use a map. Do you want to **checkout** a branch or **clone** the repository? If you want to clone you need to execute `["git", "clone", "repo.org.com/gitlab/proj/repo.git", "/Baseline/Package/Logistics/"].execute()` and then you can checkout to the particular branch – Vitalii Vitrenko Aug 02 '17 at 08:38
  • Thank you. The script seems fine but we are getting another error. Upon execution of the job the script returns this: `java.lang.UNIXProcess@448dce01` – ANIL Aug 02 '17 at 08:47
  • @ANILMAHAPATRAOfficial it is an instance of [Process](http://docs.groovy-lang.org/latest/html/groovy-jdk/java/lang/Process.html) that execute() method returns. See an [example](http://mrhaki.blogspot.com/2009/10/groovy-goodness-executing-string-or.html) how to use it. – Vitalii Vitrenko Aug 02 '17 at 09:54