title | description | ms.topic | ms.date | ms.custom |
---|---|---|---|---|
Tutorial: Create a multistage pipeline with Azure DevOps |
Build an app pipeline for development and staging. |
tutorial |
07/12/2023 |
template-how-to-pattern |
[!INCLUDE version-lt-eq-azure-devops]
You can use an Azure DevOps multistage pipeline to divide your CI/CD process into stages that represent different parts of your development cycle. Using a multistage pipeline gives you more visibility into your deployment process and makes it easier to integrate approvals and checks.
In this article, you'll create two App Service instances and build a YAML pipeline with three stages:
[!div class="checklist"]
In a real-world scenario, you may have another stage for deploying to production depending on your DevOps process.
The example code in this exercise is for a .NET web application for a pretend space game that includes a leaderboard to show high scores. You'll deploy to both development and staging instances of Azure Web App for Linux.
- A GitHub account where you can create a repository. Create one for free.
- An Azure account with an active subscription. Create an account for free.
- An Azure DevOps organization and project. Create one for free.
- An ability to run pipelines on Microsoft-hosted agents. You can either purchase a parallel job or you can request a free tier.
Fork the following sample repository at GitHub.
https://github.com/MicrosoftDocs/mslearn-tailspin-spacegame-web-deploy
Before you can deploy your pipeline, you need to first create an App Service instance to deploy to. You'll use Azure CLI to create the instance.
-
Sign in to the Azure portal.
-
From the menu, select Cloud Shell and the Bash experience.
-
Generate a random number that makes your web app's domain name unique. The advantage of having a unique value is that your App Service instance won't have a name conflict with other learners completing this tutorial.
webappsuffix=$RANDOM
-
Open a command prompt and use a
az group create
command to create a resource group named tailspin-space-game-rg that contains all of your App Service instances. Update thelocation
value to use your closest region.az group create --location eastus --name tailspin-space-game-rg
-
Use the command prompt to create an App Service plan.
az appservice plan create \ --name tailspin-space-game-asp \ --resource-group tailspin-space-game-rg \ --sku B1 \ --is-linux
-
In the command prompt, create two App Service instances, one for each instance (Dev and Staging) with the
az webapp create
command.az webapp create \ --name tailspin-space-game-web-dev-$webappsuffix \ --resource-group tailspin-space-game-rg \ --plan tailspin-space-game-asp \ --runtime "DOTNET|6.0" az webapp create \ --name tailspin-space-game-web-staging-$webappsuffix \ --resource-group tailspin-space-game-rg \ --plan tailspin-space-game-asp \ --runtime "DOTNET|6.0"
-
With the command prompt, list both App Service instances to verify that they're running with the
az webapp list
command.az webapp list \ --resource-group tailspin-space-game-rg \ --query "[].{hostName: defaultHostName, state: state}" \ --output table
-
Copy the names of the App Service instances to use as variables in the next section.
Set up your Azure DevOps project and a build pipeline. You'll also add variables for your development and staging instances.
Your build pipeline:
- Includes a trigger that runs when there's a code change to branch
- Defines two variables,
buildConfiguration
andreleaseBranchName
- Includes a stage named Build that builds the web application
- Publishes an artifact you'll use in a later stage
[!INCLUDE include]
-
When the Configure tab appears, select Starter pipeline.
-
Replace the contents of azure-pipelines.yml with this code.
:::code language="yml" source="~/../snippets/pipelines/multistage/multistage-example.yml" range="1-67":::
-
When you're ready, select Save and run.
-
In Azure DevOps, go to Pipelines > Library.
-
Select + Variable group.
-
Under Properties, add Release for the variable group name.
-
Create a two variables to refer to your development and staging host names. Replace the value
1234
with the correct value for your instance.Variable name Example value WebAppNameDev tailspin-space-game-web-dev-1234 WebAppNameStaging tailspin-space-game-web-staging-1234 -
Select Save to save your variables.
Next, you'll update your pipeline to promote your build to the Dev stage.
-
In Azure Pipelines, go to Pipelines > Pipelines.
-
Select Edit in the contextual menu to edit your pipeline.
:::image type="content" source="media/mutistage-pipeline/multistage-pipeline-edit-contextual-menu.png" alt-text="Screenshot of select Edit menu item. ":::
-
Update azure-pipelines.yml to include a Dev stage. In the Dev stage, your pipeline will:
-
Run when the Build stage succeeds because of a condition
-
Download an artifact from
drop
-
Deploy to Azure App Service with an Azure Resource Manager service connection
:::code language="yml" source="~/../snippets/pipelines/multistage/multistage-example.yml" range="1-92" highlight="69-92":::
-
-
Change the
AzureWebApp@1
task to use your subscription.-
Select Settings for the task.
:::image type="content" source="media/mutistage-pipeline/select-settings-azurewebapptask.png" alt-text="Screenshot of settings option in YAML editor task. ":::
-
Update the
your-subscription
value for Azure Subscription to use your own subscription. You may need to authorize access as part of this process. If you run into a problem authorizing your resource within the YAML editor, an alternate approach is to create a service connection.:::image type="content" source="media/mutistage-pipeline/edit-your-subscription-value.png" alt-text="Screenshot of Azure subscription menu item. ":::
-
Set the App type to Web App on Linux.
-
Select Add to update the task.
-
-
Save and run your pipeline.
Last, you'll promote the Dev stage to Staging. Unlike the Dev environment, you want to have more control in the staging environment you'll add a manual approval.
-
From Azure Pipelines, select Environments.
-
Select New environment.
-
Create a new environment with the name staging and Resource set to None.
-
On the staging environment page, select Approvals and checks.
:::image type="content" source="media/mutistage-pipeline/pipeline-add-check-to-environment.png" alt-text="Screenshot of approvals and checks menu option. ":::
-
Select Approvals.
-
In Approvers, select Add users and groups, and then select your account.
-
In Instructions to approvers, write Approve this change when it's ready for staging.
-
Select Save.
You'll add new stage, Staging
to the pipeline that includes a manual approval.
-
Edit your pipeline file and add the
Staging
section.:::code language="yml" source="~/../snippets/pipelines/multistage/multistage-example.yml" range="1-116" highlight="94-116":::
-
Change the
AzureWebApp@1
task in the Staging stage to use your subscription.-
Select Settings for the task.
:::image type="content" source="media/mutistage-pipeline/select-settings-azurewebapptask.png" alt-text="Screenshot of settings option in YAML editor task. ":::
-
Update the
your-subscription
value for Azure Subscription to use your own subscription. You may need to authorize access as part of this process.:::image type="content" source="media/mutistage-pipeline/edit-your-subscription-value.png" alt-text="Screenshot of Azure subscription menu item. ":::
-
Set the App type to Web App on Linux.
-
Select Add to update the task.
-
-
Go to the pipeline run. Watch the build as it runs. When it reaches
Staging
, the pipeline waits for manual release approval. You'll also receive an email that you have a pipeline pending approval.:::image type="content" source="media/mutistage-pipeline/pipeline-wait-approval.png" alt-text="Screenshot of wait for pipeline approval.":::
-
Review the approval and allow the pipeline to run.
:::image type="content" source="media/mutistage-pipeline/pipeline-check-manual-validation.png" alt-text="Screenshot of manual validation check.":::
If you're not going to continue to use this application, delete the resource group in Azure portal and the project in Azure DevOps with the following steps:
To clean up your resource group:
-
Go to the Azure portal and sign in.
-
From the menu bar, select Cloud Shell. When prompted, select the Bash experience.
:::image type="content" source="../apps/cd/azure/media/azure-portal-menu-cloud-shell.png" alt-text="A screenshot of the Azure portal showing selecting the Cloud Shell menu item. ":::
-
Run the following az group delete command to delete the resource group that you used,
tailspin-space-game-rg
.az group delete --name tailspin-space-game-rg
To delete your Azure DevOps project, including the build pipeline:
-
In Azure DevOps, navigate to your project.
-
Select Project settings.
-
In the Project details, select Delete.