Creating a Service Connection on Azure DevOps to Authenticate Azure

Introduction

Integrating Azure with Azure DevOps allows you to leverage the powerful CI/CD capabilities of Azure DevOps while managing your infrastructure and applications on Microsoft Azure. To enable this integration, you need to create a service connection in Azure DevOps that authenticates with Azure. This blog post will guide you through the steps to set up this service connection.

Prerequisites

  • Azure Subscription: Ensure you have an active Azure subscription.
  • Azure DevOps Account: Ensure you have an active Azure DevOps organization and project.
  • Service Principal in Azure: Create a service principal in Azure with the necessary permissions.

Step-by-Step Guide

1. Create a Service Principal in Azure
  • Open the Azure Portal.
  • Navigate to Azure Active Directory > App registrations.
  • Click New registration.
  • Provide a name for the application (e.g., “AzureDevOpsServicePrincipal”).
  • Select the supported account types (e.g., “Accounts in this organizational directory only”).
  • Click Register.
2. Generate a Client Secret for the Service Principal
  • In the App registrations page, select the application you just created.
  • Navigate to Certificates & secrets.
  • Click New client secret.
  • Provide a description and set an expiration period.
  • Click Add.
  • Copy the client secret value and save it securely. You will need it later.
3. Assign Roles to the Service Principal
  • Navigate to your subscription or resource group where you want to grant access.
  • Click Access control (IAM).
  • Click Add > Add role assignment.
  • Select the appropriate role (e.g., Contributor).
  • Search for your service principal by name and select it.
  • Click Save.
4. Create a Service Connection in Azure DevOps
  • Go to your Azure DevOps project.
  • Navigate to Project Settings > Service connections.
  • Click New service connection.
  • Select Azure Resource Manager from the list of service connection types.
  • Click Next.
5. Configure the Service Connection
  • In the New Azure service connection window, provide the following details:
    • Scope level: Select Subscription.
    • Subscription: Select your Azure subscription.
    • Resource Group: Optionally, specify a resource group.
    • Service principal (manual): Select this option.
    • Subscription ID: Enter your Azure subscription ID.
    • Service principal ID: Enter the application (client) ID of your service principal.
    • Service principal key: Enter the client secret value you saved earlier.
    • Tenant ID: Enter your Azure Active Directory tenant ID.
    • Service connection name: Provide a name for your service connection.
  • Click Verify and save to test the connection and save it.
6. Use the Service Connection in Pipelines
  • In your Azure DevOps pipeline YAML file, reference the service connection to authenticate with Azure.
  • Example:
pool:
  vmImage: 'ubuntu-latest'

steps:
- task: AzureCLI@2
  inputs:
    azureSubscription: '<Your Service Connection>'
    scriptType: 'bash'
    scriptLocation: 'inlineScript'
    inlineScript: |
      az login --service-principal -u $(servicePrincipalId) -p $(servicePrincipalKey) --tenant $(tenantId)
      az account set --subscription $(subscriptionId)
      # Add your Azure commands here

Conclusion

By following these steps, you can create a service connection in Azure DevOps to authenticate with Azure. This setup allows you to seamlessly integrate Azure with your Azure DevOps pipelines, enabling efficient CI/CD processes for your applications and infrastructure.

Feel free to reach out if you have any questions or need further assistance! Happy deploying! 

I hope this helps! Let me know if you need any more details or have other questions.

Comments

Popular posts from this blog

How to update build number in Azure DevOps pipeline?

How to get latest build ID from Azure DevOps pipeline?

How to install AWS System Manager (SSM) Agent on windows using PowerShell?