How can I add git clone operation in inline groovy script

I want to clone repo using inline groovy script in jenkins. How can I execute git clone and build the app using groovy.

Upvotes: 8

Views: 21770

Answers (3)

Muhammad Abdullah
Muhammad Abdullah

Reputation: 4495

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' 
        }
    }

Upvotes: 0

Shaybc
Shaybc

Reputation: 3147

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]]
        ])
    }
}

Upvotes: 2

biruk1230
biruk1230

Reputation: 3156

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()

Upvotes: 9

Related Questions