Home / Configuring CI/CD Pipelines as Code with YAML in Azure DevOps

Overview

Many teams prefer to define their build and release pipelines using YAML (Yet Another Markup Language). This allows them to access the same pipeline features as those using the visual designer, but with a markup file that can be managed like any other source file. YAML build definitions can be added to a project by simply adding their source file to the root of the repository. Azure DevOps also provides default templates for popular project types, as well as a YAML designer to simplify the process of defining build and release tasks.

Prerequisites

Exercise 1: Configuring CI/CD Pipelines as Code with YAML in Azure DevOps

Task 1: Creating Azure resources

  1. This lab requires a deployment of the Parts Unlimited project out to an Azure app service. To do this, you will need to spin up the necessary infrastructure. Log in to your Azure account at https://portal.azure.com.

  2. Click Create a resource and search for “web sql”.

  3. Select the Web App + SQL option published by Microsoft.

  4. Click Create.

  5. Enter a globally unique name for the app service. You may find it easiest to incorporate your name, such as “pul-yaml-johndoe”. Select the option to create a new resource group named “partsunlimited”.

  6. Select the option to configure the SQL Database. Click Create a new database and enter the name “partsunlimited”.

  7. Select the option to configure the Target server and click Create a new server. Enter a globally unique server name, such as “pul-yaml-johndoe” and provide admin credentials. Click Select to confirm the server settings.

  8. Click Select to confirm the database settings.

  9. Click Create to create the resources. Note that you may need to create an app service plan first, if you do not yet have one.

  10. It will take a few minutes for the resources to provision, so you can move on to the next task.

Task 2: Configuring the Parts Unlimited project

  1. Navigate to your team project on Azure DevOps in a new browser tab. Before digging into the YAML pipelines, you will want to disable the existing build pipeline.

  2. Navigate to Pipelines.

  3. Select the existing PartsUnlimitedE2E pipeline.

  4. From the dropdown, select Pause pipeline.

Task 3: Adding a YAML build definition

  1. Navigate to the Pipelines hub.

  2. Click New pipeline. We will use the wizard to automatically create the YAML definition based on our project.

  3. Select the Azure Repos Git as the source hosting platform. Note the others supported.

  4. Select the PartsUnlimited repo.

  5. Select the ASP.NET template as the starting point for your pipeline.

  6. Review the contents of the YAML definition. It will be saved as a new file called “azure-pipelines.yml” in the root of the repository and contain everything needed to build and test a typical ASP.NET solution. You can also customize the build as needed. In this case, update the pool to specify the build should use a Visual Studio 2017 build VM. Be sure to keep it at the same two-space indentation.

     name: Hosted VS2017
     demands:
     - msbuild
     - visualstudio
     - vstest
    

  7. Click Save and run.

  8. Click Save and run to confirm the commit.

  9. Track the build until it completes. Click Job to see the logs.

  10. Each task from the YAML file is available for review, including any warnings and errors.

  11. Close the tasks view.

  12. Select the Tests tab.

  13. The tests should now succeed as expected.

Task 4: Adding continuous delivery to the YAML definition

  1. Now that the build and test processes are successful, we can now add delivery to the YAML definition. From the options dropdown, select Edit pipeline.

  2. Add the configuration lines below after the trigger section to define a Build stage in the YAML pipeline. You can define whatever stages you need to better organize and track pipeline progress.

     stages:
     - stage: Build
       jobs:
       - job: Build
    

  3. Highlight the remainder of the YAML file and indent it four spaces (two tabs). This will simply take the existing build definition and relocate it as a child of the jobs node.

  4. At the bottom of the file, add the configuration below to define a second stage.

     - stage: Deploy
       jobs:
       - job: Deploy
         steps:
    

  5. Set the cursor on a new line at the end of the YAML definition. This will be the location where new tasks are added.

  6. Select the Azure App Service Deploy task.

  7. Select the Azure subscription where you created the app service earlier. Click Authorize and follow the path to complete authorization.

  8. Enter the App Service name you used to create the app service earlier. Update the Package or folder to ”$(System.ArtifactsDirectory)/drop/*.zip”. Click Add.

  9. The YAML that defines the task will be added to the cursor location in the file.

  10. With the added task still selected in the editor, indent it four spaces (two tabs) so that it is a child of the steps task.

  11. It’s important to note that these two stages will be run independently. As a result, the build output from the first stage will not be available to the second stage without special consideration. For this, we will use one task to publish the build output at the end of the build stage and another to download it in the beginning of the deploy stage. Place the cursor on a blank line at the end of the build stage.

  12. Search the tasks for “publish build” and select the Publish Build Artifacts task. There may be more than one available, so be sure to select the one that is not deprecated.

  13. Accept the defaults and click Add. This will publish the build artifacts to a location that will be downloadable under the alias drop.

  14. Indent the publish task four spaces (two tabs). You may also want to add an empty line before and after to make it easier to read.

  15. Place the cursor on the first line under the steps node of the deployment stage.

  16. Search the tasks for “download build” and select the Download Build Artifacts task.

  17. Set the Artifact name to “drop” and click Add.

  18. Indent the publish task four spaces (two tabs). You may also want to add an empty line before and after to make it easier to read.

  19. Click Save to commit the changes.

  20. Confirm the Save. This will begin a new build.

  21. Return to the Pipelines view.

  22. From the Runs tab, click the new build run to open it. Note that there are now multiple stages shown based on the YAML definition edits from earlier.

  23. When the Build stage completes, click the Deploy stage to follow each task.

  24. Expand the AzureRmWebAppDeployment task to review the steps performed during the Azure deployment. Once the task completes, your app will be live on Azure.

Task 5: Reviewing the deployed site

  1. Return to the Azure portal browser tab.

  2. Navigate to the app service created earlier.

  3. Select the Configuration tab.

  4. Click the defaultConnection setting.

  5. Update the Name to “DefaultConnectionString”, which is the key expected by the application. This will enable it to connect to the database created for the app service. Click Update.

  6. Click Save to apply the changes.

  7. Return to the Overview tab.

  8. Click the URL to open your site in a new tab.

  9. The deployed site should load expected.