Working with Azure DevOps Pipelines using Command line

One of the new exciting features introduced in Azure DevOps updates is the introduction of ‘az pipelines’ command group. Microsoft has been putting some efforts in writing YAML based pipelines over the last few months and has also recently introduced the ability to do Continuous Integration or Release or define both in one single pipeline. Since Azure Pipelines can now be managed at the command line, you can use it to further introduce the automation that you have created for your organization. In this blog post, we’ll learn how to use ‘az pipelines’ commands to define, initiate and manage Azure pipelines at the command line.

Install Azure DevOps CLI

Since az pipelines is part of the Az CLI, one first need it to install Az CLI on the machine from where one needs to run the commands. The commands for same are mentioned here and here (for ubuntu only). At a minimum, your Azure CLI version must be 2.0.49. You can use command: az ––version to validate the version installed.

After this, you can add Azure DevOps Extension by running below command:

az extension add --name azure-devops

The installation would be completed silently. You can verify the same using below command:

az extension list

install azure devops extensions

Working with Azure DevOps Command line help

Working with command line can be intimidating in the beginning as one need to remember the exact syntax and options to run the command properly. We can view the Azure DevOps help using below command:

az devops -h 

This should list below output:

listing azure devops help

One can also choose to use ––help instead of –h. In above output, the commands: configure, feedback etc. are to be followed by the group name (az devops in this case). Same goes for sub-groups like admin, extension, service-endpoints etc. These sub-groups are further collection of commands. If you need to know what you can do further with these sub-group of commands or commands, you need to append –h or ––help after the same:

listing help with azure devops extensions

Since we are going to work with Azure Pipelines, we’ll restrict mostly to group ‘az pipelines’ and we can see available commands or sub-command groups by using:

az pipelines -h

working with az pipelines help

Configure Defaults for the Azure DevOps

To configure defaults, one needs to authenticate to Azure using az ––version. After this, one can configure default Organization and Project using below commands:

az devops configure --defaults organization={organization-name} project={project-name}

One can list the configuration using below command:

az devops configure --list

listing default azure devops configuration gimp

Alternatively, one can direct login to Azure DevOps Organization using Personal Access Token (PAT) using below command:

az devops login 

Working with Azure Pipelines at Command Line

Create Azure Pipeline

There are multiple variations to create azure pipeline using ‘az pipeline create’ command. These differ on the basis of providing context of repository, branch, repository-type etc. The simplest of them is to create pipeline using the current checked out repository:

az pipelines create --name "dotnet-core.ci"

Alternatively, we can create a pipeline by providing the repository context by ourselves:

az pipelines create --name "dotnet-core.ci" \
    --description "pipeline for dotnet-core project" \
    --repository pipelines-dotnet-core \
    --repository-type tfsgit \
    --branch master \
    --yml-path azure-pipelines.yml

In the above command, we are creating pipeline using the yaml written in file named azure-pipelines.yml in the repository pipelines-dotnet-core in master branch.

You would see something like below on successful completion of the command:

create azure pipeline by supplying repository context

List Azure Pipelines

Another useful operation is to list the pipelines using below command:

az pipelines list

We can customize the result to show for a particular organization or repository using ––org or ––project option. On successful completion, you should get a json array containing the information:

output from azure pipelines list command

Granted this is not very useful for human parsing. But this is very useful for the automation since json can be natively consumed by most programming languages. You can also choose to consume it using languages like Python, PowerShell etc.

Below Python code shows how to parse above json output:

which lists a nice output as below:

parsing output of az pipelines list using python

List Azure Pipelines Runs

Once the pipeline is created, a pipeline run is triggered. You can use the ‘az pipelines runs’ command group to manage pipelines runs:

az pipelines runs list

By default this will list all pipelines runs. Again this will be JSON formatted data which can be easily parsed.

It presents a nice output as below:

parsing output from az pipelines runs list command

We can use ‘az pipelines runs show’ command to view details pertaining to a particular run.

Delete Azure Pipeline

We can use ‘az pipelines delete’ command to delete an azure pipeline. Of course, we would need to supply a pipeline using its ID. Passing the pipeline ID is mandatory and prevents running it as wildcard (which can be disastrous):

az pipelines delete --id {pipeline-id}

This here gets a little tricky. If you hope to supply the pipeline name, it would fail with below error:

error while deleting az pipeline command

You would need to know the pipeline id, which is an integer number. This you can find from the output of ‘az pipelines list’ or ‘az pipelines runs’ command.

After you supply the correct id of the pipeline, it would gets completed fine:

deleting azure pipeline using command line

The ‘az pipelines runs’ command group also contains the artifact and tag command sub groups which are used to manage artifacts pertaining to a run or tag a run, respectively.

Summary and Notes

We have explored a little of handling Azure Pipelines at command line. The ‘az pipelines’ command group a welcome addition to the ‘az devops’ Extension. Again, JSON output can be easily consumed with almost all programming and scripting languages as demonstrated above.

You may not be able to view source code using in-app browsers due to feature-restrictions by AMP. In such a case, open the post directly within a browser of your choice.

A copy of the source code used within this blog post is posted at here on GitHub and available under master and blog/8441 branches.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s