Azure Pipeline Part 8 – Templates Parameters
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
Today we are going to discuss how to modularize your Azure Pipeline config YAML file using templates and parameters.
Azure Pipeline Part 8 – Templates Parameters
Prerequisites:
- One active Azure DevOps account
- Personal Access Token (PAT)
- A self-hosted agent registered to your Azure DevOps organization
Setup:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
## ----- ## Setup ## ----- ## Define variables ORGANIZATION=" USER=$(az account show | jq -r .user.name) PROJECT=" PAT=' B64_PAT=$(printf "%s"":$PAT" | base64) ## Test if authentication working (List projects in your organization) curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/_apis/projects?api-version=6.0 | jq . ## Create a new Azure DevOps project curl -s --request POST \ -u $USER:$PAT \ --header "Content-Type: application/json" \ --data '{ "name": "'"$PROJECT"'", "description": "Demo Project For Azure Pipeline", "capabilities": { "versioncontrol": { "sourceControlType": "Git" }, "processTemplate": { "templateTypeId": "6b724908-ef14-45cf-84f8-768b5384da45" } } }' \ https://dev.azure.com/$ORGANIZATION/_apis/projects?api-version=6.0 | jq . ## Get project default repository details REPOSITORY_ID=$(curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/git/repositories?api-version=6.0 | jq -r .value[0].id) && curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/git/repositories/$REPOSITORY_ID?api-version=6.0 | jq . ## Create a new Azure Pipeline using REST API curl -s --request POST \ -u $USER:$PAT \ --header "Content-Type: application/json" \ --data '{ "folder": null, "name": "mypipeline", "configuration": { "type": "yaml", "path": "/azure-pipelines.yml", "repository": { "id": "'"$REPOSITORY_ID"'", "name": "'"$PROJECT"'", "type": "azureReposGit" } } }' \ https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/pipelines?api-version=6.0-preview.1 | jq . ## Clone project default repository REPO_HTTPS_CLONE_URL=$(curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/git/repositories?api-version=6.0 | jq -r .value[0].webUrl ) && B64_PAT=$(printf "%s"":$PAT" | base64) && git -c http.extraHeader="Authorization: Basic ${B64_PAT}" \ clone $REPO_HTTPS_CLONE_URL cd $PROJECT |
Azure Pipeline – Templates:
Templates let you define reusable content, logic, and parameters. Templates function in two ways. You can insert reusable content with a template or you can use a template to control what is allowed in a pipeline.
If a template is used to include content, it functions like an include directive in many programming languages. Content from one file is inserted into another file. Azure pipeline supports templating of Stage, Jobs, Steps, and Variables. A template file can include another template file. Azure Pipelines supports a maximum of 50 unique template files in a single pipeline.
When a template controls what is allowed in a pipeline, the template defines logic that another file must follow.
Azure Pipeline – Parameters:
You can specify parameters and their data types in a template and pass those parameters to a pipeline. Parameters must contain a name and data type. You can specify parameters and their data types in a template and pass those parameters to a pipeline.
Azure Pipeline – Stage Templates:
You can define a set of stages in one file and use it multiple times in other files.
Azure Pipeline – Stage Templates Syntax:
1 2 3 4 5 6 7 8 |
## Stage Templates Syntax ## template.yaml - template: string # name of template to include parameters: { string: any } # provided parameters ## azure-pipelines.yml parameters: { string: any } # expected parameters stages: [ stage ] |
Azure Pipeline – Stage Templates Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
## --------------- ## Stage Templates ## --------------- ## Create a stage template file cat <<'EOF'> stage_template.yml parameters: - name: name type: string default: GCP stages: - stage: Stage_${{ parameters.name }} jobs: - job: Job_${{ parameters.name }} steps: - script: echo hello ${{ parameters.name }} EOF ## Create pipeline definition cat <<'EOF'> azure-pipelines.yml pool: mypool stages: - template: stage_template.yml # Template reference parameters: name: AWS - template: stage_template.yml # Template reference again with different parameters parameters: name: Azure EOF ## Push the changes to Azure Repo git add . git commit -m "update1" git -c http.extraHeader="Authorization: Basic ${B64_PAT}" push |
Azure Pipeline – Job Templates:
You can define a set of jobs in one file and use it multiple times in other files.
Azure Pipeline – Job Templates Syntax:
1 2 3 4 5 6 7 8 |
## Job Templates Syntax ## template.yml - template: string # name of template to include parameters: { string: any } # provided parameters ## azure-pipelines.yml parameters: { string: any } # expected parameters jobs: [ job ] |
Azure Pipeline – Job Templates Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
## ------------- ## Job Templates ## ------------- ## Create a job template file cat <<'EOF'> job_template.yml parameters: - name: name type: string default: GCP jobs: - job: Job_${{ parameters.name }} steps: - script: echo hello ${{ parameters.name }} EOF ## Update pipeline definition cat <<'EOF'> azure-pipelines.yml pool: mypool stages: - stage: Cloud jobs: - template: job_template.yml # Template reference parameters: name: AWS - template: job_template.yml # Template reference again with different parameters parameters: name: Azure EOF ## Push the changes to Azure Repo git add . git commit -m "update2" git -c http.extraHeader="Authorization: Basic ${B64_PAT}" push |
Azure Pipeline – Step Templates:
You can define a set of steps in one file and use it multiple times in another file.
Azure Pipeline – Step Templates Syntax:
1 2 3 4 5 6 7 8 9 |
## Step Templates Syntax ## template.yml steps: - template: string # reference to template parameters: { string: any } # provided parameters ## azure-pipelines.yml parameters: { string: any } # expected parameters steps: [ script | bash | pwsh | powershell | checkout | task | templateReference ] |
Azure Pipeline – Step Templates Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
## -------------- ## Step Templates ## -------------- ## Create a step template file cat <<'EOF'> step_template.yml parameters: - name: name type: string default: GCP steps: - script: echo hello ${{ parameters.name }} EOF ## Update pipeline definition cat <<'EOF'> azure-pipelines.yml pool: mypool stages: - stage: Cloud jobs: - job: Providers steps: - template: step_template.yml # Template reference parameters: name: AWS - template: step_template.yml # Template reference again with different parameters parameters: name: Azure EOF ## Push the changes to Azure Repo git add . git commit -m "update3" git -c http.extraHeader="Authorization: Basic ${B64_PAT}" push |
Azure Pipeline – Variable Templates:
You can define a set of variables in one file and use it multiple times in other files.
Azure Pipeline – Variable Templates Syntax:
1 2 3 4 5 6 7 8 |
## Variable Templates Syntax ## template.yml - template: string # name of template file to include parameters: { string: any } # provided parameters ## azure-pipelines.yml parameters: { string: any } # expected parameters variables: [ variable ] |
Azure Pipeline – Variable Templates Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
## ------------------ ## Variable Templates ## ------------------ ## Create a variable template file cat <<'EOF'> variable_template.yml variables: - name: provider1 value: AWS - name: provider2 value: Azure - name: provider3 value: GCP EOF ## Update pipeline definition cat <<'EOF'> azure-pipelines.yml variables: - template: variable_template.yml # Template reference pool: mypool stages: - stage: Cloud jobs: - job: Providers steps: - script: echo ${{ variables.provider1 }} - script: echo ${{ variables.provider2 }} - script: echo ${{ variables.provider3 }} EOF ## Push the changes to Azure Repo git add . git commit -m "update4" git -c http.extraHeader="Authorization: Basic ${B64_PAT}" push |
Clean up:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
## -------- ## Clean up ## -------- ## Delete the Azure DevOps project and local repository PROJECT_ID=$(curl -s -u $USER:$PAT \ https://dev.azure.com/$ORGANIZATION/_apis/projects?api-version=6.0 \ | jq -r '.value[] | select(.name == "'"$PROJECT"'") | .id') && curl -s --request DELETE \ -u $USER:$PAT \ --header "Content-Type: application/json" \ https://dev.azure.com/$ORGANIZATION/_apis/projects/$PROJECT_ID?api-version=6.0 cd .. rm -rf $PROJECT |
Hope you have enjoyed this article. To know more about Azure DevOps, please refer below official documentation
https://docs.microsoft.com/en-us/azure/devops/?view=azure-devops