jenkins,

git clone another repository from a Jenkins pipeline

Jul 08, 2023 · 1 min read · Post a comment

There are some use cases where we need to reuse a config or multiple files from another Git repo in a Jenkins pipeline. As for the solution, here’s something you could try.

Prerequisites

  • Jenkins

Solution

Use the checkout function. The checkout function is a built-in Jenkins function that allows you to clone or checkout a repository. It takes a configuration object as an argument, where you can specify the URL of the repository and the branch you want to clone. Example Jenkinsfile:

pipeline {
    agent any
    
    stages {
        stage('Clone repo') {
            steps {
                dir('path/to/directory') {
                    checkout([$class: 'GitSCM', 
                        branches: [[name: 'main']],
                        userRemoteConfigs: [[url: 'https://github.com/example/repo.git', credentialsId: '<some_creds_go_here>']]
                    ])
                }
            }
        }
        
        stage('Build') {
            steps {
                // Your build steps go here
            }
        }
        
        // Additional stages...
    }
}

Conclusion

In case you face any issues, feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.