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
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:
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:
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
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
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:
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:
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Parse output from az pipelines list command into a nice format using python | |
Below code assumes that output from 'az pipelines list' command has been stored | |
under the az-pipelines-list.json file | |
""" | |
import sys, json | |
with open("working-with-azure-devops-pipelines-at-command-line/az-pipelines-list.json", "r") as f: | |
pipelines = json.load(f) | |
print(f"PipelineName \t CreationDate \t AuthoredBy \t ProjectName") | |
for item in pipelines: | |
authored_by = item['authoredBy']['displayName'] | |
project_name = item['project']['name'] | |
print(f"{item['name']} \t {item['createdDate']} \t {authored_by} {project_name}") |
which lists a nice output as below:
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Parse output from az pipelines runs list command into a nice format using python | |
Below code assumes that output from 'az pipelines runs list' command has been stored | |
under the az-pipelines-runs-list.json file. | |
""" | |
import json, sys | |
with open("az-pipelines-runs-list.json", "r") as f: | |
runs = json.load(f) | |
print(f"ProjectName \t PipelineName \t BuildNumber \t FinishedAt \t Result") | |
for item in runs: | |
project_name = item['project']['name'] | |
pipeline_name = item['definition']['name'] | |
build_number = item['buildNumber'] | |
finish_time = item['finishTime'] | |
result = item['result'] | |
print(f"{project_name} \t {pipeline_name} \t {build_number} \t {finish_time} \t {result}") |
It presents a nice output as below:
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:
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:
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.
how to set “Queue build for the user who triggered the original build” using Azure cli commands
LikeLike