This is the 5th post in the series of blog posts on managing Azure DevOps using the Terraform. You can find the series index here. In this blog post, we’ll learn to define variables and variable groups within the Azure DevOps as well as creating a Build pipeline, both using Terraform. We’ll be building onto our previous code where we ended up creating a git repository in the Azure DevOps Project. Although going through previous posts is not required to understand the concepts explained here, but its good to have a look because we’ll be referring parts of code from previous posts.
Creating variables / variable group
There is no resource to create a single variable at this point in Azure DevOps provider. So we’ll need to create variable group and within variable group, we can create as many variables as we need, using inline blocks. To create a variable group, we need to use azuredevops_variable_group
resource. To create it, we need to define below mandatory properties:
- project_id – Provide the id of Azure DevOps Project to which it should be associated
- scope – Boolean to indicate if this variable group is shared by all pipelines of this project.
- name – Name of the variable group.
Within the variable group, we can define variable blocks to define variables and associate them with values. The only required property with the variable group is the variable name defined using name
, but its good to add a value
and is_secret
to define whether the value is a secret or not. Although the functionality is there, its debatable whether you should be defining secrets in plain text in Terraform Configuration.
Below is our code, where we added couple of variables in a variable group named ‘Any CPU – Release’:
Define Build Definition (as Yaml code)
To define build pipeline using Terraform, we need to use resource azuredevops_build_definition
resource. For this, we need to define below properties:
- project_id – Required – Id of the Azure DevOps Project to which it should be associated
- name – Optional – name of the build definition
- agent_pool_name – Optional – agent pool that should execute the build. Defaults to
Hosted Ubuntu 1604
- ci_trigger block – Optional – continuous Integration Integration trigger
- variable_groups block – Optional – list of variable group IDs (integers) to link to the build definition
- variable block(s) – Optional – A list of
variable
blocks, directly associated with the pipeline - repository block – Required with below properties:
- repo_type – Valid values:
GitHub
orTfsGit
orBitbucket
. Defaults toGithub
.TfsGit
refers to Azure DevOps itself - repo_id – The id of the repository
- branch_name – Name of the branch for which builds are triggered, defaults to
master
- yml_path – path of the Yaml file describing the build definition
- repo_type – Valid values:
With all above in place, we can define our pipeline as below:
Here’s code for azure-pipelines.yml, if interested:
At this point, we can run terraform plan
to validate our changes:
Lets go and apply our changes and hop over to Azure DevOps to see the pipeline in action:
[…] Teil 5 – Create variable, variable groups, create and define build pipelines […]
LikeLike