Azure DevOps CLI

In my quest for incorporating automation into repetitive tasks and reducing my interaction with Azure services from a GUI perspective, using the command line has slowly become second nature.

As the primary custodian for customer onboarding onto the various platforms and tooling that we use as an Azure Expert MSP, I often have to create a Project in Azure DevOps and create Azure Pipelines along with service connections that are used for automation, it dawned on me that I had yet to interact with Azure DevOps using the CLI.

In this blog post I’ll show you how to create a Project as well as a service connection using the Azure DevOps CLI extension.

Microsoft has a Getting started with Azure DevOps CLI tutorial

Requirements

Azure DevOps Organization
Azure CLI
PowerShell
•Azure DevOps extension for Azure CLI
Homebrew package manager (macOS)
Existing Azure AD Application

Creating a Project

Project in Azure DevOps can be thought of as a repository for source code, you can store ARM templatesPowerShell scripts, Terraform templates, YAML configuration files etc.

The below PowerShell snippet includes variables that are used in the command to create the Project, I have omitted optional parameters and kept it simple.

## Log in first with az login command
az login
$ProjectName = “Azure DevOps Demo Project”
$ProjectDescription = “Azure Deployments for $ProjectName”
$Organization = “https://dev.azure.com/AdrianMDemo/”
## Create Project
az devops project create –name $ProjectName –description $ProjectDescription –org $Organization –source git –output table

view rawCreate-AzDevOps-Project.ps1 hosted with ❤ by GitHub

Once you have run the PowerShell commands, you’ll be prompted to login via the web browser, after returning to the PowerShell session you’ll be greeted by an output indicating that the project has been created.

If you navigate towards the Azure DevOps portal you’ll notice a newly created project like the below screenshot.

Creating a Service Connection

Service connections enable you to connect to external and remote services to execute tasks in a job. For example, you may need to connect to your Microsoft Azure subscription, to a different build server or file server, to an online continuous integration environment, or to services you install on remote computers.

It’s possible to define service connections in Azure Pipelines that are available for use in all your tasks. For example, you can create a service connection for your Azure subscription and use this service connection name in an Azure Web Site Deployment task in a release pipeline.

I have a pre created application that I granted contributor access on my subscription that I will use in this step, you’ll need the Application ID and Client Secret.

The below PowerShell script requires a few variables that will be used in the command to create the service connection.

## Create service connection
$TenantId = “”
$SubscriptionName = “”
$SubscriptionId = “”
$ServicePrincipal = “Application ID”
$Organization = “”
$ProjectName = “”
az devops service-endpoint azurerm create –azure-rm-service-principal-id $ServicePrincipal `
–azure-rm-subscription-id $SubscriptionId `
–azure-rm-subscription-name $SubscriptionName `
–name $SubscriptionName `
–azure-rm-tenant-id $TenantId `
–project $ProjectName `
–org $Organization

view rawCreate-Service-Connection.ps1 hosted with ❤ by GitHub

That concludes this post. In an upcoming blog post I’ll go through the process of creating a Pipeline using the Azure DevOps CLI.

Retrieved from; Azure DevOps CLI – DEV Community

Author : Adrian Mudzwiti

Leave a Reply

Your email address will not be published. Required fields are marked *