I want to clone repo using inline groovy script in jenkins. How can I execute git clone and build the app using groovy.
Asked
Active
Viewed 1.8k times
7
-
I use the following, but I actually don't know if it works in other jenkins environments `node { checkout scm }` – evolutionxbox Jan 15 '19 at 11:09
3 Answers
8
If you're using Jenkins pipelines, see examples from the official documentation, e.g.:
node {
stage('Clone sources') {
git url: 'https://github.com/jfrogdev/project-examples.git'
}
}
For simple Groovy script you can try something like:
["git", "clone", "https://github.com/jfrogdev/project-examples.git"].execute()

biruk1230
- 3,042
- 4
- 16
- 29
2
i also do something similar:
jenkinsfile / or pipeline script code:
node
{
stage('Checkout')
{
GitManager.clone(this, "https://github.com/jfrogdev/project-examples.git", "*/master", "myGitUserID");
}
}
utility class code (from sharedLib):
class GitManager
{
public static void clone(Script scriptRef, String gitURLString, String branchID, String gitUserID)
{
scriptRef.checkout([
$class: 'GitSCM',
branches: [[name: branchID]],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'CleanCheckout']],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: gitUserID, url: gitURLString]]
])
}
}

Shaybc
- 2,628
- 29
- 43
0
Note: Clone of github repo and on this step installing the dependencies throw and so solution is
Solution: Do git clone & install dependencies on one line
stage('Clone the project and Delete the existing folder') {
steps {
sh 'rm -rf jenkin-learn'
sh 'git clone --single-branch --branch main https://github.com/mabdullahse/jenkin-learn.git && cd ./jenkin-learn && npm install'
}
}

mabdullahse
- 3,474
- 25
- 23