How to clone a github repository using Jenkins pipeline script? How to clone a github repository using Jenkins pipeline script? jenkins jenkins

How to clone a github repository using Jenkins pipeline script?


You've a syntax error in using the username and password within the stage. You should've have it like ${GIT_USERNAME} and not {GIT_USERNAME} alone.

That being said, you need to explicitly mention to which branch you need to check out.

I assume you'd want to checkout to master branch by default. So use the below:

pipeline {   agent any   stages {    stage('Checkout') {      steps {        script {           // The below will clone your repo and will be checked out to master branch by default.           git credentialsId: 'jenkins-user-github', url: 'https://github.com/aakashsehgal/FMU.git'           // Do a ls -lart to view all the files are cloned. It will be clonned. This is just for you to be sure about it.           sh "ls -lart ./*"            // List all branches in your repo.            sh "git branch -a"           // Checkout to a specific branch in your repo.           sh "git checkout branchname"          }       }    }  }}

If you want to checkout to a specific branch by default then use the below in your pipeline stage. With the below part, you won't need to run git checkout command separately.

pipeline {   agent any   stages {        stage('Checkout') {            steps {                checkout([$class: 'GitSCM', branches: [[name: '*/branchname']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'jenkins-user-github', url: 'https://github.com/aakashsehgal/FMU.git']]])                sh "ls -lart ./*"            }        }         }}