Azure DevOps Command Line Interface (CLI)

Introduction

Over the last week I have been working on automating a process. One of the steps in the process was to set the status of an Azure DevOps Task from Closed to Active. I was able to do this using the Azure DevOps CLI. This post will show you how to install the Azure DevOps CLI and how to use it to interact with Azure DevOps Services using the CLI locally and in a pipeline. There are a lot of commands available that will allow you CRUD operations to the following Azure DevOps Services: artifacts, boards, the devops management control plane, pipelines, and repositories.).

Local Installation and Interactive Use

The Azure DevOps CLI extension is the tool that we are going to use to interact with Azure DevOps Services. To install the Azure DevOps CLI extension, you will need to have the Azure CLI installed. If you don't have the Azure CLI installed, you can download it from here. Secondly we need to configure the Azure DevOps CLI extension refer to the documentation here. To do this, run the following command:

az extension add --name azure-devops

Then we need to configure the extension to use our Azure DevOps organization. To do this, run the following command:

az devops configure --defaults organization=https://dev.azure.com/<Your ADO Organization Name Goes Here> project=<Your ADO Project Name Goes Here>
# Example
az devops configure --defaults organization=https://dev.azure.com/contoso project=ContosoWebApp

Then you need to login to your Azure DevOps organization. Before you can do this you will need a personal access token (PAT) from Azure DevOps use this guide to create a PAT token. Once you have your PAT token you can login to your Azure DevOps organization, run the following command and enter the pat token you created when you get prompted:

az devops login

Now you can interact with Azure DevOps Services using the Azure DevOps CLI. Use the following command to get the help menu that gives you some example of common commands:

az devops -h

Using the Azure DevOps CLI in a Pipeline

The Azure DevOps hosted agent already has the extension in installed so all that you need to do is to login and configure the extension for your Azure DevOps organization. The following pipeline will show you how to use the Azure DevOps CLI in a pipeline:

trigger:
- main

pool:
  vmImage: ubuntu-latest

steps:
- script: |
    echo $(System.AccessToken) | az devops login
    az devops configure --defaults organization=https://dev.azure.com/<Your ADO Organization Name Goes Here> project=<Your ADO Project Name Goes Here> # Example az devops configure --defaults organization=https://dev.azure.com/contoso project=ContosoWebApp
  env:
    AZURE_DEVOPS_CLI_PAT: $(System.AccessToken)
  displayName: 'Login And Configure Azure DevOps Extension'
- script: |
    az devops -h
  displayName: 'Execute ADO CLI CMD'

Conclusion

The Azure DevOps CLI is a powerful tool that you can use to interact with Azure DevOps Services. You can use it locally and in a pipeline. Using this method there are so many scenarios that you can automate to make your ADO experience better and more efficient. Happy Coding!