Work with YAML files

Completed

In this unit, we'll look at the structure of a YAML file. You can use the YAML files provided by Microsoft that are available and found in the HelloWorld GitHub repository in the azureDevOps folder.

If you look at the content, you can see that a YAML file defines all the steps in a text notation. If you need to provide parameters, you need to know the name of the options of each of those steps.

In the next example, I copied the first part of the YAML script. The appBuild parameter gets its version number using a variable appVersion. These variables can be configured in your pipeline using the Variables button.

Screenshot of the YAML script with variables button.

In the YAML file, the required agent pool is listed, as is the different PowerShell build steps. All of those steps run a PowerShell script that is stored in the repository. If you ever need to change something you can easily grab a copy, modify the files, and then push them back into the repository, without the need of reconfiguring your build definition.

stages:
- stage: Build
  jobs:
  - job: Build
    variables:
      build.clean: all
      platform: x64
      version: "current"
      appVersion: "1.0"
      appBuild: $[counter(variables['appVersion'],0)]
      appRevision: 0
      createRuntimePackages: False
      skipComponentGovernanceDetection: True

    pool:
      name: Default

    steps:
    - task: PowerShell@2
      displayName: 'Reading Settings'
      inputs:
        targetType: filePath
        filePath: 'scripts\Read-Settings.ps1'
        arguments: '-appVersion "$(appVersion).$(appBuild).$(appRevision)"'
        failOnStderr: true

    - task: PowerShell@2
      displayName: 'Login to bcinsider repository'
      condition: and(succeeded(),or(eq(variables['version'],'nextminor'),
                 eq(variables['version'],'nextmajor')))
      inputs:
        targetType: inline
        script: 'docker login "bcinsider.azurecr.io" -u "$(bcInsiderUsername)"
                 -p "$(bcinsiderPassword)"'
        failOnStderr: true

    - task: PowerShell@2
      displayName: 'Install NavContainerHelper'
      inputs:
        targetType: filePath
        filePath: 'scripts\Install-NavContainerHelper.ps1'
        failOnStderr: true

As we already mentioned, if your file is named CI.yml, then Azure DevOps will automatically set up a build pipeline based on that file. You always start a new pipeline, and then go for the YAML option. Select your repository and specify if you'd like to start with an empty YAML file or select an existing in your repository.

Screenshot of the configure your pipeline window.

If you select the Starter pipeline, then you get a basic YAML file where you need to add all the necessary steps. On the right side of the editor, you can find an assistant that you can use to add steps to the YAML file. It will provide an interface where you can enter your parameters. This will generate the necessary YAML code. The standard YAML file needs numerous modifications. Change the agent pool and define all your different steps.

Screenshot of the Review your pipeline YAML starter template.

More information on the YAML structure can be found in the YAML schema reference topic.