This repository contains tasks to learn how to use Azure Pipelines in Azure DevOps. The tasks are designed to be completed in order, starting with a simple pipeline and gradually introducing more advanced features.
Learning goal: Understand the basic structure of an Azure DevOps pipeline and how to run scripts.
In your Repository, create a simple starter pipeline.
- Go to Pipelines in Azure DevOps
- Click on "New Pipeline"
- Select "Azure Repos Git" as the source
- Select your repository
- Select "Starter pipeline"
- Review the generated YAML file and click "Run" to create the pipeline
The pipeline will contain two script tasks. The script runs in the default command shell of the agent, which is bash on Linux and cmd on Windows.
To run a script in a specific shell, you can use the bash or pwsh task, depending on the shell you want to use.
Learning goal: Understand how to use parameters, conditions and expressions in Azure Pipelines.
Update the starter pipeline to use the parameters feature of Azure Pipelines.
-
Checkout the repository locally.
-
Move the starter pipeline
.azure-pipelines/main.yml. I generally recommend to use this dedicated folder for all pipeline YAML files. The.prefix shows that it's a config folder.mkdir .azure-pipelines mv azure-pipelines.yml .azure-pipelines/main.yml
-
Add
booleanparameter. -
Add
stringparameter with default value and allowed values. -
Add
numberparameter with default value. -
Print the parameters in a task.
-
Use the task
conditionto run a task only if the boolean parameter istrue. -
Use the
ifexpression to show and run a task only if boolean parameter istrue. -
After running the pipeline, view the full YAML file of the pipeline and investigate the parameters in the file.
Remark: After renaming the pipeline file, you need to update the pipeline settings in Azure DevOps to point to the new file location.
Learning goal: Understand how to use variables in Azure Pipelines.
Update the parameters pipeline to use the variables and variable groups features of Azure Pipelines.
- Create in the Azure DevOps Library a variable group named
{your-name}-variablesa secret and a non-secret variable. - Add the variable group to the pipeline.
- Add another variable to the pipeline.
- Define a variable that is derived from a parameter using an if expression.
- Append to the parameter tasks additional tasks to print the variables.
- After running the pipeline, view the Full YAML file of the pipeline and investigate the variables in the file, compare them with the parameters.
Learning goal: Understand how to run build tasks in Azure Pipelines.
Update the variables pipeline to build a simple Blazor application.
-
Prepare a sample project in your repository. This requires .NET Core SDK installed on your machine. If you don't have it installed, you can use the official .NET installation guide.
-
Create the sample project in your repository using the following commands:
# Create src folder mkdir src # Create a new solution cd src && dotnet new sln -n Training # Create a folder for the sample project mkdir BlazorApp # Create a new blazor app cd BlazorApp && dotnet new blazor # Add the project to the solution cd .. && dotnet sln add BlazorApp/BlazorApp.csproj
-
Clear the starter pipeline and add variables for the
solutionFolderusing the predefined variableBuild.SourcesDirectory, thesolutionPatternand anartifactName. -
Add tasks to the pipeline:
-
Set up the .NET Core SDK version
-
Convert the following commands into dotnet core pipeline tasks:
# Restore dependencies dotnet restore # Build the project dotnet build --configuration Release --no-restore # Create the artifact dotnet publish --configuration Release --no-build --output $TEMP
-
Publish the artifact as Pipeline artifact with the name defined in the variable
artifactName.
-
-
Run the pipeline and verify that the artifact can be downloaded and contains the published Blazor app.
Learning goal: Understand how to use templates in Azure Pipelines.
Update the build pipeline to use templates.
- Create a steps template following the folder structure in the good practices.
- Move the steps from the previous task into the template and identify the parameters that are required to run the steps.
- Use the template in the pipeline and pass the required parameters.
- Run the pipeline and verify that it works as expected.
Learning goal: Understand how to use another repository as a resource in Azure Pipelines.
Update the template pipeline to use another repository as a resource.
- Instead of using the steps template from your repository, use the steps template from the template repository.
- Run the pipeline and verify that it works as expected.
- Revert the changes to use your own steps template
Learning goal: Understand how to use jobs and service connections in Azure Pipelines.
Create a jobs template that wraps the steps template from the previous task and use it in the pipeline. Additionally add a job to deploy the artifact to Azure.
- Create another steps template that uses the DownloadPipelineArtifact@2 and AzureWebApp@1 tasks.
- Wrap the steps templates in two separate job templates, one for the build and one for the deployment.
- Use the job templates in the pipeline and pass the required parameters.
- Run the pipeline and verify that your Blazor app is reachable via the Azure Web App URL.
Learning goal: Understand how to use stages in Azure Pipelines.
Create a build and test stage and a deploy stage in the pipeline.
- Copy the current solution from the training repository to your repository.
- Create a job that wraps the central test steps template. The job should test the application in parallel to the build.
- Wrap your job templates in stages, one for the build and test and one for the deployment. The build and test jobs should run in parallel. It's not necessary to create stage templates.
- Run the pipeline and verify that the build and test jobs run in parallel and the deployment job runs after the build and test jobs have completed successfully.
Learning goal: Understand how to use approvals and checks in Azure Pipelines.
Change the deploy job to use the environment feature of Azure Pipelines and add an approval before deployment.
- Create an environment in Azure DevOps name it
{web-app-name}-environment. - Add an approval check to the environment that requires your user to approve the deployment.
- Change the deploy job to target the environment.
- Run the pipeline and verify that the deployment job waits for your approval before deploying the application.
Remark: The deployment job type automatically downloads the artifact, so you can remove the download artifact task from the deploy job.
Learning goal: Understand how to use PR triggers in Azure Pipelines.
Update the pipeline to trigger on pull requests.
- Add a new trigger for pull requests in the pipeline YAML file.
- Create a new branch and make a change to the pipeline file.
- Specify the branch
mainto be included for the PR trigger. - Set a condition on the deploy stage to run only on the
mainbranch. - Protect the
mainbranch in the repository settings and require the pipeline to pass before merging. - Create a pull request and verify that the pipeline is triggered.
Learning goal: Understand how to implement automated versioning and tagging in Azure Pipelines.
Update the pipeline to automatically version and tag the commit on successful deployment.
- Inspect the versioning and tagging implementation in the training repository.
- Include the versioning and tagging templates for builds running on the
mainbranch. - The tagging stage should depend on the
Deploystage and run only if the deployment was successful.
Remark: Read the documentation for consuming variables from other stages.