0
0
AzureHow-ToBeginner · 4 min read

How to Deploy to Azure Using Pipeline: Step-by-Step Guide

To deploy to Azure using a pipeline, create an Azure DevOps pipeline with a YAML file that builds your code and uses the AzureWebApp task to deploy your app. Connect your Azure subscription to the pipeline and configure deployment steps in the YAML file.
📐

Syntax

The basic syntax for deploying to Azure in a pipeline uses a YAML file with these parts:

  • trigger: Defines when the pipeline runs (e.g., on code push).
  • pool: Specifies the agent machine to run the pipeline.
  • steps: Lists the tasks to build and deploy your app.
  • AzureWebApp@1: The task that deploys your app to Azure Web App.
yaml
trigger:
  branches:
    include:
      - main

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: AzureWebApp@1
  inputs:
    azureSubscription: '<your-service-connection>'
    appName: '<your-app-name>'
    package: '$(System.DefaultWorkingDirectory)/drop/*.zip'
💻

Example

This example shows a simple pipeline that triggers on the main branch, builds a .NET app, and deploys it to Azure Web App.

yaml
trigger:
  branches:
    include:
      - main

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: UseDotNet@2
  inputs:
    packageType: 'sdk'
    version: '7.x'

- script: dotnet build --configuration Release
  displayName: 'Build project'

- task: ArchiveFiles@2
  inputs:
    rootFolderOrFile: '$(Build.SourcesDirectory)'
    includeRootFolder: false
    archiveType: 'zip'
    archiveFile: '$(Build.ArtifactStagingDirectory)/app.zip'
    replaceExistingArchive: true

- publish: '$(Build.ArtifactStagingDirectory)/app.zip'
  artifact: drop

- task: AzureWebApp@1
  inputs:
    azureSubscription: 'MyAzureServiceConnection'
    appName: 'my-azure-webapp'
    package: '$(System.DefaultWorkingDirectory)/drop/app.zip'
Output
Pipeline runs triggered on push to main branch. Build completes successfully. App.zip is created and published. Azure Web App 'my-azure-webapp' is deployed with the package.
⚠️

Common Pitfalls

Common mistakes when deploying to Azure using pipelines include:

  • Not creating or linking an Azure service connection in Azure DevOps, so the pipeline cannot authenticate.
  • Using incorrect appName that does not match your Azure Web App name.
  • Forgetting to package the app correctly before deployment.
  • Not setting the trigger correctly, so the pipeline does not run on code changes.
yaml
trigger:
  branches:
    include:
      - main

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: AzureWebApp@1
  inputs:
    azureSubscription: ''  # Missing service connection
    appName: 'wrong-app-name'  # Incorrect app name
    package: '$(System.DefaultWorkingDirectory)/drop/app.zip'

# Corrected version:
- task: AzureWebApp@1
  inputs:
    azureSubscription: 'MyAzureServiceConnection'
    appName: 'my-azure-webapp'
    package: '$(System.DefaultWorkingDirectory)/drop/app.zip'
📊

Quick Reference

Tips for smooth Azure deployment using pipelines:

  • Always create and verify your Azure service connection in Azure DevOps.
  • Use the correct app name exactly as it appears in Azure.
  • Package your app files before deployment.
  • Test your pipeline with a manual run before automating triggers.

Key Takeaways

Create an Azure service connection in Azure DevOps to allow pipeline authentication.
Use the AzureWebApp@1 task in your YAML pipeline to deploy your app package.
Ensure your app is correctly packaged and the app name matches your Azure Web App.
Set pipeline triggers to automate deployment on code changes.
Test your pipeline manually before relying on automated triggers.