9

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

 3 years ago
source link: https://azuredevopslabs.com/labs/azuredevops/yaml/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Home / Configuring CI/CD Pipelines as Code with YAML in Azure DevOps
Rate this lab (56 Votes)

Overview

Many teams prefer to define their build and release pipelines using YAML (YAML Ain’t 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

The following image will walk you through all the steps explained in this lab

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 App + 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-yaml”.

  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. Make sure Allow Azure services to access server checkbox is selected. Click Select to confirm the server settings.

  8. On Pricing Tier , Select Standard and leave default options. Click Apply.

  9. Click Select to confirm the database settings.

  10. 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.

  11. 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.

  7. Review trigger and point to master if you repo does not have main (new repos will have “main” instead of “master”).

  8. Click Save and run.

  9. Click Save and run to confirm the commit.

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

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

  12. Close the tasks view.

  13. Select the Tests tab.

  14. 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(select) the remainder of the YAML file and indent it four spaces (two tabs). Everything after “pool” (included) should fall under “job: Build”. 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
         pool:
           vmImage: 'vs2017-win2016'
         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”. Not $(System.DefaultWorkingDirectory)! . 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.

    Note: The packageForLinux parameter is a bit misleading in the example but is valid for Windows or Linux. It’s an alias of Package, so it could be shortened to that

  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. 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. Add a property to the download task specifying the artifactName of “drop”. Be sure to match the spacing.

     artifactName: 'drop'
    
  20. Click Save to commit the changes.

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

  22. Return to the Pipelines view.

  23. 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.

  24. If you see an error message requiring you need permission , click the view button to do so. Then click Permit twice.

  25. Click the Deploy stage to follow each task.

  26. 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 OK.

  6. Click Save to apply the changes.

  7. Return to the Overview tab.

  8. Click Browse to open your site in a new tab.

  9. The deployed site should load expected.

Reference

You can watch the following video that walks you through all the steps explained in this lab


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK