~/jenkins/how-git-clone-another-repository-jenkin-pipeline.md

How to Git Clone Another Repository from Jenkin Pipeline in Jenkinsfile

1 min readOctober 07, 2022by Gorav Singal#cicd#build#jenkins
// TL;DR

Use the checkout step with GitSCM class inside your Jenkinsfile to clone a second repo into a subdirectory. Pass branch, credentialsId, and use RelativeTargetDirectory extension to control where it lands.

How to Git Clone Another Repository from Jenkin Pipeline in Jenkinsfile

Introduction

There are some cases, where I need another git repository while running some automation. Lets see, how we can clone another git repository within another job.

Jenkinsfile for Git Clone

stage('Get K8s Yaml files') {
    steps {
        echo 'Getting kubernetes files from git...'

        checkout([$class: 'GitSCM', 
            branches: [[name: '*/main']], 
            doGenerateSubmoduleConfigurations: false, 
            extensions: [[
                $class: 'RelativeTargetDirectory',
                relativeTargetDir: 'k8s_deployment']],
            submoduleCfg: [], 
            userRemoteConfigs: [[
                credentialsId: 'my-git-token',
                url: 'https://git.mycorp.com/myorg/k8s_deployment.git']]])
    }
}

In above Jenkinsfile, I’m asking Jenkin to clone the git repository in a directory: k8s_deployment, and this directory will be created in the workspace directory. I will also need the git token.

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Introduction In this post we will see following: How to schedule a job on cron…

How to Fetch Multiple Credentials and Expose them in Environment using Jenkinsfile pipeline

How to Fetch Multiple Credentials and Expose them in Environment using Jenkinsfile pipeline

Introduction In this post, we will see how to fetch multiple credentials and…

Jenkins Pipeline - How to run Automation on Different Environment (Dev/Stage/Prod), with Credentials

Jenkins Pipeline - How to run Automation on Different Environment (Dev/Stage/Prod), with Credentials

Introduction I have an automation script, that I want to run on different…

Jenkinsfile - How to Create UI Form Text fields, Drop-down and Run for Different Conditions

Jenkinsfile - How to Create UI Form Text fields, Drop-down and Run for Different Conditions

Introduction I had to write a CICD system for one of our project. I had to…

A Jenkins Groovy Pipeline for Building Python Projects and Pushing to Artifactory

A Jenkins Groovy Pipeline for Building Python Projects and Pushing to Artifactory

Introduction In this post, we will see a sample Jenkin Pipeline Groovy script…

Python Build Quality Gates with Pylint and Unittest Coverage Thresholds

Python Build Quality Gates with Pylint and Unittest Coverage Thresholds

Introduction It is very important to introduce few process so that your code and…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…