Search code examples
githubjenkinsgroovy

How does one clone a GitHub git repo with Groovy?


I used to know how to do this, but that was 3 years ago. I have this Jenkins Pipeline Groovy ...

pipeline {
    agent any

    stages {
        
        stage("Checkout") {
             steps {
                withCredentials([sshUserPrivateKey(credentialsId: 'mySshUserCred', 
                  keyFileVariable: 'key_file_var', usernameVariable: 'username_var')]) {
                    sh 'git clone [email protected]:SorryAssInc/ops.git'
                }
            } 
        }
    }
}

I have these parameters:

enter image description here

When I run my pipeline in Jenkins I get this output log:

[Pipeline] sh
+ git clone [email protected]:SorryAssInc/ops.git
Cloning into 'ops'...
Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

What am I doing wrong? Am I missing a plugin? What is up with the keyFileVariable: 'key_file_var'? Is that where Jenkins will write the private key data I have stored in my Jenkins credentials or does Jenkins expect that file to exist already and if so how does one do that?


Solution

  • You can follow this steps.

    stage('Git Clone') {
        steps {
            git branch: 'master',
                credentialsId: 'git_credential', #suggest to use credential on Jenkins.
                url: 'git_URL'
        }
    }