Jenkins – Azure Repository Cloning

Jenkins provides different ways to take Clone/checkout of a repository from the Microsoft Azure DevOps portal. In this blog, We will see three Jenkins methods to clone the Azure Repository.

  1. Using Git command
  2. Using Jenkins Git Plugin
  3. Using Groovy script

1. Using Git command :

In the Jenkins job, Microsoft Azure Repository can be cloned using the “git clone” command in the Build module of the job. This is a direct method where username and password can be given in the clone command itself.

Copy the repository clone URL, username, and password from the Azure DevOps portal. (Refer figure 1.1)

azure

figure 1.1

Add the below command in the “Execute Windows batch command” box – For Windows (Execute shell – For Linux) as shown in figure 1.2 and execute the job to clone the repository in your local machine.

Command : git clone https://<username>:<password>@dev.azure.com/tscbala/testbala/_git/ant-sample.git

git commandfigure 1.2

2. Using Jenkins Git Plugin :

In this method, the Azure repo will be cloned using Jenkins Plugin called “Git Plugin”. Git Plugin can be installed from Dashboard –> Manage Jenkins –> Manage Plugins.(figure 2.1)

plugin

figure 2.1

Once the Plugin is installed, the Git option will be shown under the “Source Code Management” module in the Jenkins jobs. Select the Git option and then provide Azure Repository URL and credentials as shown in figures 2.2 & 2.3 below.

git

figure 2.2

credentials

figure 2.3

After Providing the Azure repository details, save and run the job. The repository will be cloned directly into your local workspace.

3. Using Groovy Script :

Jenkins has an inbuilt Snippet Generator for Pipeline jobs which generates Groovy scripts for various build steps. To Clone an azure repository, click the “Pipeline Syntax” (figure 3.1) option in the Pipeline job. A snippet Generator page will be opened.

pipeline

figure 3.1

In the Snippet Generator,

  • Select the “checkout: Check out from version control” option from the dropdown list.
  • Select “Git” in the SCM dropdown list.
  • Provide Azure Repository URL & Credentials in the respective boxes.
  • Click “Generate Pipeline Script”

snippet1

figure 3.2

snippet-2figure 3.3

A Pipeline Groovy script to clone the Azure repository will be generated which can be used in the pipeline jobs for repository checkout.

There are several other options available in the snippet generator which generates groovy scripts and are very useful in the pipeline job configurations.

The above methods can be used for Azure Repositories and for other Git applications as well. Please feel free to reach Cittabase for more information. Visit our blogs for more topics on DevOps & Power BI.