Change your Azure DevOps classic pipelines to YAML (without being a YAML guru)

In this tutorial, we will see how to create a YAML build pipeline from an existing pipeline built with the classic editor. Creating a YAML build pipeline from scratch may be intimidating for novice users, but if you have an existing one in the classic editor, switching over is easier. You don’t need to be a YAML guru.

Once logged in Azure DevOps, head over to your existing pipeline. This can be accessed from the build pipelines section as shown below.

Opening the original pipeline

In my case, I have a pipeline to build this website’s code. Click on the job name, then we will have the option to view it in YAML form.

View it as YAML

This will open a popup window showing the existing job in the YAML format. Click the ‘Copy to clipboard’ button. We will need to paste this code into a new file soon.

Copy YAML to clipboard

We now need to copy the code and store it inside a text file in our repository. This can be done directly from the Azure DevOps portal. From your repository, create a new branch and then a new file right afterwards. Put this new file in the Pipeline folder, so that it will not be stored directly beside your code.

Create new YAML file

Paste the copied YAML into the new file.

The YAML editor

Before you commit, it is important to check if you need to specify the image name of the build agent. When you run your build on an Azure-hosted agent, you need to add the vmImage parameter, as I did with windows-2019 below. If you have a private agent instead, just the name of the agent is enough.

Setting the YAML build agent

Now we can go back to the build pipelines page and click on the button to create a new pipeline.

Start build with Azure Repos Git

On the next step, click on the repository name.

Selecting the repository

And on the Configure step, we will select an ‘Existing Azure Pipelines YAML file’.

Connect pipeline to existing YAML file

Then choose the filename that we just saved a few steps back.

Selecting the YAML file

On the final review step, we can now add the required variables. The generated YAML file contains comments about the variables that the build expects so that it can run successfully.

Need to declare some variables

Click on the Variables button so that we can add them and then just add each one individually. We can look the existing classic pipeline if we need to verify the values of the variables.

Adding a variable for YAML pipeline

We can now click the Run button to test the pipeline.

The build is queued

Once the job finishes, we can check the outcome.

The build run outcome

So now we now that the build has run successfully, we can set it to be triggered automatically whenever any code is committed into the master branch. Go back to edit the YAML file and add the trigger as shown here.

Adding a master branch trigger to the YAML file

And there’s no better way to test it than creating a pull request on the YAML file so that it can be integrated into master and automatically trigger the build. Create a new pull request and choose to merge the new branch into master.

The change is ready to be added to a pull request

If this triggers the build, then we have confirmed that we have really managed to set up everything successfully.

The pull request has triggered the build


Add your comments