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.
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.
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.
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.
Paste the copied YAML into the new file.
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.
Now we can go back to the build pipelines page and click on the button to create a new pipeline.
On the next step, click on the repository name.
And on the Configure step, we will select an ‘Existing Azure Pipelines YAML file’.
Then choose the filename that we just saved a few steps back.
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.
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.
We can now click the Run button to test the pipeline.
Once the job finishes, we can check the 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.
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.
If this triggers the build, then we have confirmed that we have really managed to set up everything successfully.