Introduction
In modern software development, Continuous Integration and Continuous Deployment (CI/CD) are crucial in ensuring smooth, automated deployments. Azure DevOps provides robust pipeline capabilities that enable developers to automate the deployment of their applications, reducing manual effort and minimizing errors.
In this blog, we’ll walk through setting up an Azure DevOps pipeline for an Azure Service App, ensuring seamless deployment whenever changes are pushed to the repository.
Setting Up the Azure DevOps Pipeline
- An Azure Service App created in the Azure Portal.
- A service connection in Azure DevOps is linked to your Azure subscription.
- Your Service App code is stored in a repository like Azure Repos, GitHub, or Bitbucket.
Pipeline Configuration
Below is a YAML-based Azure DevOps pipeline that automates the build and deployment of an Azure Service App.
trigger: branches: include: - main # Change this to your branch if needed paths: include: - ServiceAppCode/* variables: azureSubscription: 'ServiceAppDeployment' # Azure service connection name serviceAppName: 'TestingApp' # Azure Service App name serviceAppPath: 'ServiceAppCode' # Path to Service App source code buildConfiguration: 'Release' # Build service app source code in release publishDirectory: '$(Build.ArtifactStagingDirectory)/publish' pool: vmImage: 'ubuntu-22.04' # Also use ubuntu-latest stages: - stage: Build displayName: 'Build Stage' jobs: - job: Build displayName: 'Build Job' steps: - task: UseDotNet@2 displayName: 'Install .NET SDK' inputs: packageType: 'sdk' version: '8.0.x' includePreviewVersions: false - script: | echo "Cleaning up existing publish directory..." rm -rf $(publishDirectory) mkdir -p $(publishDirectory) displayName: 'Ensure Clean Publish Directory' - task: DotNetCoreCLI@2 displayName: 'Restore Dependencies' inputs: command: 'restore' projects: '$(serviceAppPath)/*.csproj' - task: DotNetCoreCLI@2 displayName: 'Build Service App' inputs: command: 'build' projects: '$(serviceAppPath)/*.csproj' arguments: '--configuration $(buildConfiguration) /p:WarningLevel=0' # /p:WarningLevel=0 Remove the warning at the time of build service app - task: DotNetCoreCLI@2 displayName: 'Publish Service App' inputs: command: 'publish' projects: '$(serviceAppPath)/*.csproj' publishWebProjects: false arguments: '--configuration $(buildConfiguration) --output $(publishDirectory)' zipAfterPublish: true - task: PublishBuildArtifacts@1 displayName: 'Publish Artifacts' inputs: pathToPublish: '$(publishDirectory)' artifactName: 'drop' - stage: Deploy displayName: 'Deploy Stage' dependsOn: Build condition: succeeded() jobs: - job: Deploy displayName: 'Deploy to Azure Service App' steps: - download: current displayName: 'Download Build Artifacts' artifact: 'drop' - task: AzureServiceApp@1 displayName: 'Deploy to Azure Service App' inputs: azureSubscription: '$(azureSubscription)' appType: 'webApp' # functionApp for function app OR webApp for web app appName: '$(serviceAppName)' package: '$(Pipeline.Workspace)/drop/*.zip'
Understanding the Pipeline
1. Trigger Configuration
- The pipeline is triggered when changes are pushed to the
main
branch. - It monitors specific folders where Service App updates are made.
2. Defining Variables
azureSubscription
: The name of the Azure DevOps service connection.serviceAppName
: The name of the Service App in Azure.serviceAppPath
: The directory containing the Service App source code.publishDirectory
: The folder where the build output is stored.
3. Choosing the Right Agent Pool
- The pipeline uses the Ubuntu 22.04 VM image for the build process
4. Build Stage
- Installing .NET SDK: Ensures that the required version is available.
- Cleaning the Publish Directory: Prevents old files from interfering with new builds.
- Restoring Dependencies: Ensures that all NuGet dependencies are downloaded.
- Building the Service App: Compiles the service app with the specified configuration.
- Publishing the Service App: Packages the compiled app for deployment.
- Publishing Artifacts: Stores the published files as build artifacts for the deployment stage.
5. Deploy Stage
- Downloading Build Artifacts: Retrieves the published application files.
- Deploying to Azure: Uses the
AzureServiceApp@1
task to deploy the service app to Azure.
Conclusion
By setting up this Azure DevOps pipeline, you can automate the deployment of your Azure Service App, ensuring quick and error-free releases.
With automation in place, you can focus on developing new features while Azure DevOps handles the heavy lifting of deployments!