Azure Pipeline YAML configuration is a crucial aspect of automating your build, test, and deployment processes. You can define your pipeline entirely in a YAML file, making it easier to manage and share with your team.
To start with Azure Pipeline YAML configuration, you need to create a new YAML file in your repository. This file will contain the definition of your pipeline, including the tasks and stages involved.
A YAML file consists of a series of key-value pairs that define the pipeline configuration. For example, the `trigger` keyword is used to specify the trigger for the pipeline, such as a Git push or a schedule.
In Azure Pipeline YAML configuration, you can use variables to make your pipeline more flexible and reusable. Variables can be defined at the pipeline level or at the stage level, and they can be used to store sensitive information like API keys or database credentials.
Azure provides a range of built-in variables that can be used in your pipeline, such as `Build.SourceBranch` and `Build.SourceVersion`. These variables can be used to access information about the build, such as the source branch and version.
Azure Pipeline YAML Basics
Azure Pipeline YAML Basics is a fundamental concept in automating your build, release, and deployment processes.
A YAML file is used to define the pipeline, and it's stored in a repository, typically in Azure DevOps.
The pipeline YAML file should be named .azure-pipelines.yml to enable auto-discovery by Azure Pipelines.
The pipeline YAML file is divided into sections, including trigger, pool, and job.
Triggers are used to initiate the pipeline, and there are two types: manual and continuous integration.
A pool is a collection of agents, and you can specify the pool in the pipeline YAML file.
A job is a self-contained unit of work in the pipeline, and you can have multiple jobs in a single pipeline.
Jobs are executed in parallel, and each job can have its own set of tasks.
Tasks are the building blocks of a job, and they're used to perform specific actions, such as building, testing, or deploying code.
Configuring CI/CD
Configuring CI/CD is a crucial step in Azure Pipeline YAML, and it starts with defining the trigger for your pipeline. This can be done using the `trigger` keyword, which allows you to run your pipeline on a schedule, or when a specific change is made to your code.
You can also use the `pool` keyword to specify the agent pool that your pipeline will use, which is essential for distributed builds. For example, you can use the `vmImage` property to specify a specific virtual machine image for your agent.
In Azure Pipeline YAML, you can use the `variables` section to define variables that can be used throughout your pipeline. This is useful for storing sensitive information, such as API keys or database credentials, and can be used to simplify your pipeline configuration.
Configuring CI/CD with YAML
Configuring CI/CD with YAML is a crucial step in automating your development process. YAML files are used to define the configuration for CI/CD pipelines.
You can use YAML files to specify the jobs, tasks, and stages that make up your pipeline. For example, a YAML file can define a job that runs a test script, followed by a stage that deploys the code to production.
The syntax for YAML files is straightforward, with indentation used to denote nesting. For instance, a YAML file might have a section for environment variables, with key-value pairs listed below. Environment variables can be used to store sensitive information, such as API keys or database credentials.
In a CI/CD pipeline, YAML files can be used to define the workflow, including the order in which jobs are run and the conditions under which they are triggered. This allows you to create a flexible and customizable pipeline that meets the needs of your project.
Approval Checks
Approval checks are a crucial aspect of configuring CI/CD pipelines. They allow you to manually control when a resource runs, ensuring that your pipeline adheres to specific conditions.
You can use approval checks and templates to manually control when a resource runs. With the required template approval check, you can require that any pipeline using a resource or environment extends from a specific YAML template.
This approach enhances security by ensuring that your resource is used only under specific conditions. Setting a required template approval ensures that your pipeline is secure and reliable.
Approval checks can be used to manually control when a resource runs, which is particularly useful for sensitive resources or environments.
Repository
Repository is a crucial part of configuring CI/CD pipelines. You can specify an external repository using the repository keyword, which allows you to use multi-repo checkout with a repository that requires a service connection.
Azure Pipelines supports various repository types, including git, github, githubenterprise, and bitbucket. The type of repository you choose depends on the type of repository you're working with.
Here are the supported repository types and their corresponding requirements:
Trigger evaluation only occurs on the default branch, so make sure to set the correct default branch or merge the YAML file into the current default branch.
Pipeline Structure
The pipeline structure in Azure Pipelines is defined by a YAML file, which contains a series of tasks that are executed in a specific order.
The YAML file is automatically created when you use the wizard to create a new pipeline, and it will contain everything needed to build and test a typical ASP.NET solution.
A YAML build definition can be created by navigating to the Pipelines hub and clicking New pipeline, then selecting the Azure Repos Git as the source hosting platform and the PartsUnimited repo.
Selective Exclusion
Selective Exclusion is a powerful feature that allows you to control which stages run in your pipeline. This is particularly useful when you have stages that are only relevant in certain situations.
You can use parameters to set whether a stage runs, as shown in an example pipeline with four stages. The Performance Test stage runs if the parameter runPerfTests is true.
The default value of runPerfTests is false, so without any updates, only three of the four stages run. This means you can easily exclude a stage by setting a parameter to its default value.
You can also use parameters as part of conditional logic, allowing you to make decisions based on specific criteria. This is achieved through the use of conditionals in the pipeline YAML.
Include Steps with StepList Parameter
Including steps with the stepList parameter allows you to dynamically include a list of steps in your build process. This is especially useful when you have a variable number of tasks to complete in your pipeline.
You can use the stepList parameter type to define a list of build tasks in your main pipeline. For example, in the main pipeline (azure-pipelines.yml), you can define two jobs: build and deploy. The build job can use a template (build.yml) and pass a list of build tasks using the stepList parameter.
The build.yml template can dynamically include the steps defined in the build_tasks parameter. This is done by iterating over each step in the build_tasks parameter and executing each step defined in the build_tasks list.
Here's a breakdown of how this works:
By using the stepList parameter, you can easily add or remove steps from your build process without having to modify the main pipeline. This makes it easier to maintain and update your pipeline over time.
Steps
Steps are an essential part of a pipeline's structure, allowing you to define a sequence of tasks that are executed in a specific order. A pipeline can have multiple jobs, each with its own set of steps.
You can reuse steps across multiple jobs by inserting a template, as seen in Example 4. This is particularly useful when you have a set of steps that are common to multiple jobs.
Steps can be dynamically included using the stepList parameter, as shown in Example 3. This parameter type allows you to pass a list of steps to a job, which can then be executed in the specified order.
Some pipelines may require conditional logic to determine which steps to run. This can be achieved by using parameters, such as the boolean parameter "test" in Example 2. By setting the value of this parameter, you can control whether or not to run specific steps in the pipeline.
Here's a summary of the different types of steps you can use in a pipeline:
By understanding the different types of steps available, you can design a pipeline that meets the specific needs of your project.
Job, Stage, and Step Templates with Parameters
Job, stage, and step templates with parameters can greatly simplify your pipeline structure. You can define a template with parameters and then reuse it in multiple places throughout your pipeline.
For example, you can define a template called `npm-with-params.yml` that takes two parameters: `name` and `vmImage`. This template can then be used to create multiple jobs with different names and VM images.
When you consume the template in your pipeline, you can specify values for the template parameters. For instance, you can reference the template three times with different parameter values, each referring to a different operating system and VM image name.
Templates with parameters can also be used to define stages with multiple jobs. For example, a template called `stage-template.yml` can define four parameters: `stageName`, `jobName`, `vmImage`, and `scriptPath`. This template can then be used to dynamically define stages and jobs with different parameters.
You can also use parameters with step or stage templates. For instance, a template called `steps-with-params.yml` can define a parameter called `runExtendedTests` with a default value of `false`. This parameter can then be used to determine whether to run extended tests or not.
Here's a summary of the benefits of using job, stage, and step templates with parameters:
- Simplified pipeline structure: By reusing templates, you can simplify your pipeline structure and reduce code duplication.
- Increased flexibility: Templates with parameters allow you to customize your pipeline for different scenarios and environments.
- Improved maintainability: By defining templates with parameters, you can make changes to your pipeline more easily and consistently.
Some key things to keep in mind when using job, stage, and step templates with parameters:
- Scalar parameters are treated as strings: If you don't specify a type for a parameter, it will be treated as a string.
- Non-empty strings are cast to true: In a Boolean context, non-empty strings are treated as true.
- You can use parameters with different data types: Parameters can be scalar strings, integers, or even objects.
Containers
Containers are a powerful tool in your pipeline structure, allowing you to consume container images from public or private Docker registries or Azure Container Registry instances.
You can define a generic container resource to consume images from a Docker registry, without needing to specify a type keyword.
Container resources can be used for container jobs or to support one or more services, and you can use volumes to share data between services.
To enable container triggers for all image tags, use the enabled: 'true' syntax, which is different from the syntax for other resource triggers.
Container image metadata is passed to the pipeline as variables, making it accessible across all jobs used in your container deployment tasks.
Container resource variables work with Docker and Azure Container Registry, but not with local image containers.
The location variable applies only to the acr type of container resources.
Frequently Asked Questions
Where is the Azure pipeline YAML file?
The Azure pipeline YAML file is typically located at the root of your repository, usually named azure-pipelines.yml. Find it there to start configuring your pipeline.
How do I create a pipeline from a YAML file in Azure DevOps?
To create a pipeline from a YAML file in Azure DevOps, navigate to Pipelines and click New pipeline, then select your code hoster and repository in the Connect and Select steps. Next, choose Existing Azure Pipelines YAML file and select your YAML file from the dropdown menu.
What is the YAML file for CI CD pipeline?
A CI/CD YAML file is a configuration file that outlines the steps for building, testing, and deploying an application. It's the blueprint for a smooth and automated development process.
Sources
- https://learn.microsoft.com/en-us/azure/devops/pipelines/yaml-schema/
- https://www.azuredevopslabs.com/labs/azuredevops/yaml/
- https://learn.microsoft.com/en-us/azure/devops/pipelines/process/runtime-parameters
- https://learn.microsoft.com/en-us/azure/devops/pipelines/process/resources
- https://learn.microsoft.com/en-us/azure/devops/pipelines/process/templates
Featured Images: pexels.com