Azure DevOps Crash Course (Part 5): Deploy to Azure App Service with Dev, Prod and Approvals
I still remember my first "real" deployment. It was a Friday (I know, I never learn), and it involved copying files over remote desktop, editing a config file by hand, and praying. When something broke, nobody knew which version was running.
A deployment pipeline fixes all of that. Same steps every time, a clear history of what went where, and a human approval before anything touches production. In Part 4 our pipeline learned to build and test the Todo API. Today we teach it to deploy: first to a Dev environment automatically, then to Prod after someone clicks Approve.
What you need to know before the code
A pipeline can have stages. Think of them as chapters: Build, then Dev, then Prod.
We build once and save the result as a zip file (an artifact). Dev and Prod deploy that same zip.
A stage that points to an environment can be paused for an approval. That is our safety button before Prod.
Keep these three ideas in mind and the YAML below will make a lot more sense.
What we are building
- Build stage: test the code and package it as a zip file (we call this an artifact).
- Dev stage: deploy that zip to a Dev web app automatically.
- Prod stage: wait for a manual approval, then deploy the same zip to the Prod web app.
The key idea: we build once and deploy the same package everywhere. What you tested in Dev is exactly what goes to Prod.
Step 1: Create two web apps in Azure
You need an Azure subscription. If you do not have one, the Azure free account is enough for this.
Open the Azure Cloud Shell (it already has the Azure CLI) and run:
# Pick a region close to you
az group create --name rg-todoapi --location canadacentral
# A free Linux App Service plan
az appservice plan create --name plan-todoapi --resource-group rg-todoapi --sku F1 --is-linux
# See which .NET runtimes are available
az webapp list-runtimes --os linux
# Two apps: one for Dev, one for Prod (names must be unique worldwide)
az webapp create --name todoapi-yourname-dev --resource-group rg-todoapi --plan plan-todoapi --runtime "DOTNETCORE:10.0"
az webapp create --name todoapi-yourname-prod --resource-group rg-todoapi --plan plan-todoapi --runtime "DOTNETCORE:10.0"
If the runtime name gives you an error, use the exact .NET 10 value that list-runtimes printed. Replace yourname with something unique, like your name and a number.
The F1 plan is free, which is perfect for learning. It is slow and has daily limits, so do not run a real product on it.
Step 2: Connect Azure DevOps to Azure
Azure DevOps needs permission to deploy into your subscription. This is called a service connection.
- In your TodoApi project, go to Project settings > Service connections.
- Click New service connection and choose Azure Resource Manager.
- Choose Workload identity federation (the recommended option, no secrets to manage).
- Pick your subscription and the
rg-todoapiresource group. - Name it
azure-todoapiand save.
Scoping it to one resource group is a good habit. The pipeline can only touch what it needs. Microsoft explains the options here: Connect to Azure with an Azure Resource Manager service connection.
Step 3: Create the environments and the approval
Go to Pipelines > Environments and create two environments: todoapi-dev and todoapi-prod. Choose None for resources.
Open todoapi-prod, click the three dots, and choose Approvals and checks. Add an Approvals check and add yourself as the approver.
That is it. Any pipeline stage that targets todoapi-prod will now stop and wait for you. More details in Microsoft's docs: Create and target environments and Pipeline deployment approvals.
Step 4: Turn the pipeline into stages
We will build the new azure-pipelines.yml in three small pieces. Each piece goes right under the previous one, so by the end you have the whole file.
Piece 1: the top of the file and the Build stage
trigger:
branches:
include:
- main # run when something is merged to main
variables:
buildConfiguration: Release
stages:
- stage: Build
jobs:
- job: Build
pool:
vmImage: ubuntu-latest
steps:
- task: UseDotNet@2 # install .NET 10
inputs:
packageType: sdk
version: 10.x
- task: DotNetCoreCLI@2 # run the tests from Part 4
displayName: Test
inputs:
command: test
projects: '**/*Tests.csproj'
arguments: '--configuration $(buildConfiguration)'
- task: DotNetCoreCLI@2 # package the API as a zip
displayName: Publish
inputs:
command: publish
publishWebProjects: false
projects: 'src/TodoApi.Api/TodoApi.Api.csproj'
arguments: '--configuration $(buildConfiguration) --output $(Build.ArtifactStagingDirectory)'
zipAfterPublish: true
- publish: $(Build.ArtifactStagingDirectory) # save the zip as "drop"
artifact: drop
This is almost the same as Part 4. The new parts: we publish the API as a zip file and save it with the name drop, so the next stages can use it.
One small detail: I set publishWebProjects: false and point to the API project directly. The default setting looks for a wwwroot folder or web.config, which a Web API project does not have.
Piece 2: the Dev stage
Add this under the Build stage:
- stage: Dev
dependsOn: Build # only run if Build succeeded
jobs:
- deployment: DeployDev # "deployment" connects this to an environment
pool:
vmImage: ubuntu-latest
environment: todoapi-dev
strategy:
runOnce:
deploy:
steps:
- task: AzureWebApp@1
inputs:
azureSubscription: azure-todoapi # the service connection from Step 2
appType: webAppLinux
appName: todoapi-yourname-dev
package: $(Pipeline.Workspace)/drop/**/*.zip # the zip from Build
Read it like a sentence: "After Build, deploy the zip to the Dev web app, and record it in the todoapi-dev environment."
Two words that look scary but are not: deployment (instead of job) is what links a stage to an environment. That is how you get a deployment history and approvals. runOnce just means "deploy one time, no fancy rollout". Perfect for us.
Piece 3: the Prod stage
Add this at the end:
- stage: Prod
dependsOn: Dev # only after Dev worked
jobs:
- deployment: DeployProd
pool:
vmImage: ubuntu-latest
environment: todoapi-prod # this environment has the approval
strategy:
runOnce:
deploy:
steps:
- task: AzureWebApp@1
inputs:
azureSubscription: azure-todoapi
appType: webAppLinux
appName: todoapi-yourname-prod
package: $(Pipeline.Workspace)/drop/**/*.zip
Look closely: it is the Dev stage again, with three words changed. Same zip, different app, different environment. Because todoapi-prod has an approval, this stage waits for you before it does anything.
Putting it together
The three pieces, one under the other, are your whole azure-pipelines.yml. Since the file is now fairly long, and one wrong indent can break everything, before I push I paste it into a YAML validator to catch indentation mistakes. It is much faster than waiting for a failed run. Microsoft's full reference for this task is here: Deploy to Azure App Service by using Azure Pipelines.
Step 5: Run it
Commit the new YAML on a branch, open a pull request, let the build validation pass, and complete it. Because main changed, the pipeline starts.
The first time, Azure DevOps may ask you to permit the pipeline to use the service connection and the environments. Click Permit. This is a one-time safety check.
Now watch: Build runs the tests and creates the zip. Dev deploys automatically. Prod stops with a "Waiting for approval" message.
Open your Dev app at https://todoapi-yourname-dev.azurewebsites.net/todos. You should see an empty list: []. Our API is live on the internet!
Happy with Dev? Go back to the run, click Review, and Approve. A minute later Prod has the exact same version.
What I love about this setup
Go to Pipelines > Environments > todoapi-prod. You can see every deployment, which commit it came from and who approved it. If someone asks "what is running in Prod?", you have the answer in two clicks.
Rolling back is just re-running an older pipeline run.
Common problems (and fixes)
- The pipeline waits forever on "Permission needed": click View and Permit for the service connection or environment.
- The app shows an error page after deploy: open Log stream for the web app in the Azure portal. Most of the time it is the runtime version. Make sure the app runs .NET 10.
- App name already taken: web app names are global. Add a number.
- The free F1 app is slow on the first request: free apps go to sleep. That is normal.
Your homework
- Create the two web apps and the service connection
- Create the environments and the approval on Prod
- Deploy to Dev, then approve your first Prod release
Coming up next
Right now our pipeline has no secrets, but real apps have database passwords, API keys and connection strings. In Part 6 we will handle configuration and secrets the right way, with Variable Groups and Azure Key Vault, so no password ever ends up in YAML.
Be honest: have you ever deployed on a Friday? How did it go?
Comments
No comments yet. Start the discussion.