Unleash the Power of Git Diff in Your Azure DevOps CI Pipeline: A Step-by-Step Guide
Image by Adzoa - hkhazo.biz.id

Unleash the Power of Git Diff in Your Azure DevOps CI Pipeline: A Step-by-Step Guide

Posted on

Are you tired of manually comparing code changes between multiple repositories in your Azure DevOps CI pipeline? Do you want to automate the process and get instant insights into what’s changed? Look no further! In this article, we’ll show you how to use git diff on an Azure DevOps CI pipeline with more than one repository, and take your pipeline’s efficiency to the next level.

Why Use Git Diff in Your CI Pipeline?

Git diff is an incredibly powerful tool that allows you to compare changes between two commits, branches, or even entire repositories. In the context of an Azure DevOps CI pipeline, git diff can help you:

  • Identify and track changes between builds, ensuring that only necessary code is re-compiled and reducing overall build time.
  • Detect and prevent unwanted changes from making it to production, ensuring a more stable and reliable codebase.
  • Automate the code review process, making it easier to spot errors, security vulnerabilities, and performance bottlenecks.

Prerequisites

Before we dive into the meat of this tutorial, make sure you have the following:

  • A working Azure DevOps account with an active CI pipeline.
  • Multiple repositories connected to your Azure DevOps project.
  • Basic knowledge of YAML, Azure Pipelines, and Git.

Step 1: Configure Your Pipeline

First, let’s get our pipeline setup. Log in to your Azure DevOps account, navigate to your pipeline, and click on the “Edit” button.

azure-pipelines.yml
trigger:
- main
pool:
vmImage: 'ubuntu-latest'
steps:
- task: AzureCLI@2
displayName: 'Azure CLI'
inputs:
command: 'version'

In this example, we have a basic pipeline triggered on the “main” branch, running on an Ubuntu VM. We’ll add more steps and tasks as we progress.

Step 2: Clone Multiple Repositories

To use git diff, we need to clone all the repositories involved in our pipeline. Add the following step to your pipeline YAML:

azure-pipelines.yml
steps:
- task: AzureCLI@2
displayName: 'Clone Repositories'
inputs:
command: 'run'
script: |
git clone https://dev.azure.com/[organization]/[repository1].git
git clone https://dev.azure.com/[organization]/[repository2].git
...
git clone https://dev.azure.com/[organization]/[repositoryN].git

Replace [organization], [repository1], [repository2], …, [repositoryN] with your actual organization name and repository names.

Step 3: Configure Git Diff

Now, let’s configure git diff to compare changes between our repositories. Add the following step to your pipeline YAML:

azure-pipelines.yml
steps:
- task: AzureCLI@2
displayName: 'Run Git Diff'
inputs:
command: 'run'
script: |
git diff --name-only --diff-filter=ACMRTUXB $(git rev-parse HEAD^) $(git rev-parse HEAD) > diff.txt

In this example, we’re using the `git diff` command to compare the changes between the current commit (`HEAD`) and the previous commit (`HEAD^`). The `–name-only` flag returns only the names of changed files, while `–diff-filter=ACMRTUXB` filters out files that are not added, copied, modified, renamed, typechanged, unknown, or broken.

The output is redirected to a file named `diff.txt`, which we’ll use later to analyze the changes.

Step 4: Analyze and Display the Diff

Now that we have our diff output, let’s analyze and display it in a meaningful way. Add the following step to your pipeline YAML:

azure-pipelines.yml
steps:
- task: PowerShell@2
displayName: 'Analyze and Display Diff'
inputs:
targetType: 'inline'
script: |
$diff = Get-Content -Path diff.txt -Encoding UTF8
Write-Host "Changes between commits:"
foreach ($file in $diff) {
Write-Host " - $file"
}

In this example, we’re using PowerShell to read the contents of the `diff.txt` file and display the list of changed files. You can customize this step to suit your needs, such as sending the diff output to a notification channel or triggering additional tasks based on the changes.

Putting it All Together

Here’s the complete pipeline YAML:

azure-pipelines.yml
trigger:
- main
pool:
  vmImage: 'ubuntu-latest'
steps:
  - task: AzureCLI@2
    displayName: 'Azure CLI'
    inputs:
      command: 'version'
  - task: AzureCLI@2
    displayName: 'Clone Repositories'
    inputs:
      command: 'run'
      script: |
        git clone https://dev.azure.com/[organization]/[repository1].git
        git clone https://dev.azure.com/[organization]/[repository2].git
        ...
        git clone https://dev.azure.com/[organization]/[repositoryN].git
  - task: AzureCLI@2
    displayName: 'Run Git Diff'
    inputs:
      command: 'run'
      script: |
        git diff --name-only --diff-filter=ACMRTUXB $(git rev-parse HEAD^) $(git rev-parse HEAD) > diff.txt
  - task: PowerShell@2
    displayName: 'Analyze and Display Diff'
    inputs:
      targetType: 'inline'
      script: |
        $diff = Get-Content -Path diff.txt -Encoding UTF8
        Write-Host "Changes between commits:"
        foreach ($file in $diff) {
          Write-Host "  - $file"
        }

Save and run your pipeline to see the magic happen!

Troubleshooting and optimization

Here are some common issues you might encounter and tips to optimize your pipeline:

Issue Solution
Git diff takes too long to run Use `git diff –name-only` instead of `git diff` to reduce the output size. You can also use `git diff –stat` for a more concise output.
Repository cloning fails Check your repository URLs, credentials, and permissions. Ensure that the pipeline has the necessary access to clone the repositories.
Diff output is too large Use `git diff –name-only` and filter the output using `Select-Object` or `Where-Object` in PowerShell. You can also use `git diff –name-only –diff-filter=ACMRTUXB` to filter out unwanted changes.

Conclusion

And that’s it! You’ve successfully implemented git diff in your Azure DevOps CI pipeline with multiple repositories. This powerful tool can help you streamline your pipeline, catch errors earlier, and improve overall code quality. Remember to customize and optimize your pipeline to suit your specific needs, and happy coding!

By following this guide, you’ve taken a significant step towards creating a more efficient and reliable CI pipeline. Don’t forget to explore other Azure DevOps features, such as pipelines as code, multi-repo pipelines, and Azure CLI tasks, to take your pipeline to the next level.

Happy building!

Here are 5 Questions and Answers about “How to git diff on an Azure DevOps CI pipeline with more than one repository”:

Frequently Asked Question

Got some burning questions about running git diff on an Azure DevOps CI pipeline with multiple repositories? We’ve got you covered!

How do I configure my Azure DevOps CI pipeline to run git diff with multiple repositories?

To configure your pipeline to run git diff with multiple repositories, you’ll need to add a script task to your pipeline that checks out each repository and runs the git diff command. You can do this by adding a `checkout` task for each repository, followed by a `script` task that runs the `git diff` command.

How do I specify which repositories to include in the git diff output?

You can specify which repositories to include in the git diff output by using the `repository` parameter in your pipeline script. For example, if you want to include changes from two repositories, `repo1` and `repo2`, you can use the following script: `git diff $(git rev-parse HEAD) $(git rev-parse origin/HEAD) — repository=repo1,repo2`.

Can I customize the git diff output to show only specific file types or changes?

Yes, you can customize the git diff output to show only specific file types or changes by using the various options available with the `git diff` command. For example, if you want to show only changes to `.cs` files, you can use the following script: `git diff $(git rev-parse HEAD) $(git rev-parse origin/HEAD) — “*.cs”`.

How do I handle cases where the repositories have different branch names or structures?

If the repositories have different branch names or structures, you can use the `checkout` task to specify the branch and repository path for each repository. For example, if you have two repositories, `repo1` and `repo2`, with different branch names, you can use the following script: `checkout repo1 –branch=”main” && checkout repo2 –branch=”develop”`.

Can I use git diff to compare specific commits or tags across multiple repositories?

Yes, you can use git diff to compare specific commits or tags across multiple repositories by specifying the commit or tag hashes in the `git diff` command. For example, if you want to compare the `v1.0` tag in `repo1` with the `v2.0` tag in `repo2`, you can use the following script: `git diff v1.0..v2.0 — repository=repo1,repo2`.