I have the following setup, a template file, and a pipeline that extends the template. I am wondering is it possible to dynamically add all parameters to extends' parameter?
Template.yml
parameters:
- name: Location
type: string
default: 'eastus'
Washington.yml
parameters:
- name: Location
type: string
default: 'westus'
extends:
template: Template.yml
parameters:
Location: ${{ parameters.Location }}
I can do something like this but I think it doesn't work because the shadowed parameters variable will be used instead of root level parameters.
parameters:
- name: Location
type: string
default: 'westus'
extends:
template: Template.yml
parameters:
- ${{ each param in parameters }}
${{ param.Name }}: ${{ param.Value }}
This works for me:
parameters:
- name: Location
type: string
default: 'westus'
extends:
template: Template.yml
parameters:
${{ each param in parameters }}:
${{ param.Key }}: ${{ param.Value }}
So you had small syntax issues.
It printed:
Assuming that template is:
parameters:
- name: Location
type: string
default: 'eastus'
steps:
- script: echo ${{ parameters.Location }}
Related
This is part of my Yml file
I need to re-call this template if it failed.it should be rerun again. After a few seconds, ideally.I am new to yml files
I tried to use retryCountOnTaskFailure but it should be under task but the template calling different hierarchy
https://learn.microsoft.com/en-us/azure/devops/release-notes/2021/sprint-195-update#automatic-retries-for-a-task
- template: ${{variables['System.DefaultWorkingDirectory']}}
parameters:
Test: ${{ parameters.isTestRelease }}
Environment: ${{ parameters.deploymentTarget }}
Component: '${{ parameters.component }}'
The retryCountOnTaskFailure feature is applied to individual tasks within a pipeline. Templates aren't tasks, they act more like an include that expands the contents of the template into your pipeline.
# pipeline.yml
trigger: none
parameters:
- name: isTestRelease
type: boolean
default: false
- name: deploymentTarget
type: string
default: DEV
values:
- DEV
- QA
- UAT
- PROD
- name: component
type: string
default: 'x'
stages:
- template: my-template.yml
parameters:
Test: ${{ parameters.isTestRelease }}
Environment: ${{ parameters.deploymentTarget }}
Component: ${{ parameters.component }}
And within the template, you'd want to add the retry logic to specific tasks:
# my-template.yml
parameters:
- name: isTestRelease
type: boolean
- name: component
type: string
stages:
- stage: Test
jobs:
- job: 1
steps:
- task: ...
- task: ...
- task: ...
retryCountOnFailure: 2
- task: ...
It's also located under "Control Options" when the "Command Line" task is added:
Do you guys know a way to split nested parameters such as "myubuntu:1" into $image and $tag so i can pass it on my yaml templates?
parameters:
- name: base_images
type: object
default:
public:
amazon1: "amazonlinux:1"
amazon2: "amazonlinux:2"
private:
ubuntu1: "myubuntu:1.2"
ubuntu2: "myubuntu:2.0"
steps:
- ${{ each version in parameters.base_images.public }}:
- template: <path to my template>...
parameters:
image_type: ${{ value }} # public
image_name: ${{ value }} # amazonlinux
image_tag: ${{ value }} # 1
i tried several methods on splitting the value but none of them works and im running out of ideas.
I have template
dockerbuild.yml
steps:
- task: Bash#3
displayName: Build an image to container registry
inputs:
script : echo $(PATH)
then a variable file
var.build.yml
- name: PATH
value: 'docker/path'
- name: PATH1
value: 'docker/oldpath'
- name: PATH2
value: 'docker/newpath'
azurepipeline1.yml
resources:
repositories:
- repository: templates
type: git
name: components/pipeline_templates
trigger:
- none
pool:
name: PoolA
variables:
- template: variabletemplates/var.build.yml#templates
jobs:
steps:
- template: CI-CD/dockerbuild.yml#templates # Template reference
parameters:
PATH: ${{ variables.PATH }}
- template: CI-CD/dockerbuild.yml#templates # Template reference
parameters:
PATH: ${{ variables.PATH1 }}
- template: CI-CD/dockerbuild.yml#templates # Template reference
parameters:
PATH: ${{ variables.PATH2 }}
The above code works
But if I change the parameter to
parameters:
PATH: ${{ variables.PATH1 }}
OR
parameters:
PATH: ${{ variables.PATH2 }}
In the output it shows
Output:
docker/path
$(PATH1)
$(PATH2)
I need to reuse the template with many times within the pipeline with different paths. Please help in resolving the issue
Your template needs a parameters block.
parameters:
- name: PATH
type: string
steps:
- task: Bash#3
displayName: Build an image to container registry
inputs:
script : echo ${{ parameters.PATH }}
I wonder if it is possible to mix map arguments in Azure Pipelines template yaml, and how to do it.
These two scenarios shown bellow do same thing: place template parameter as env argument in a task, but in the second I'm trying to do it through two maps instead of a single one. That could be useful when I have different purposes to those values (at the eyes of someone who is extending the template) but both are going to be used as 'env' under the hood.
This works fine:
Main Pipeline:
...
extends:
template: templates/deploy/v1/deployment.job.yaml#infrastructure-templates
parameters:
name: dev
variableGroup: 'AzureDevopsVariableGroupName'
secretEnvVariables:
SECRET1: ${SECRET1}
SECRET2: ${SECRET2}
Target Template:
parameters:
- name: secretEnvVariables
type: object
jobs:
...
steps:
- bash: |
#!/bin/bash
echo "SECRET1 = ${SECRET1}"
...
displayName: Substitute Env VARS on files
enabled: true
env:
${{ parameters.secretEnvVariables }}
This doesn't work (and I wonder if it is possible to make it work):
Main Pipeline:
...
extends:
template: templates/deploy/v1/deployment.job.yaml#infrastructure-templates
parameters:
name: dev
variableGroup: 'AzureDevopsVariableGroupName'
secretEnvVariables:
SECRET1: ${SECRET1}
SECRET2: ${SECRET2}
moreVariables:
VAR1: ${VAR1}
Target Template:
parameters:
- name: secretEnvVariables
type: object
- name: moreVariables
type: object
jobs:
...
steps:
- bash: |
#!/bin/bash
echo "SECRET1 = ${SECRET1}"
echo "VAR = ${VAR1}"
...
displayName: Substitute Env VARS on files
enabled: true
env:
${{ parameters.secretEnvVariables }}
${{ parameters.moreVariables }}
Can it be done? How to do it?
I am doing something similar and this isn't well documented but can use objects to accomodate for this.
Here it is the combo of environment and region deployment:
- name: environmentObjects
type: object
default:
- environmentName: 'dev'
regionAbrvs: ['eus']
- environmentName: 'uat'
regionAbrvs: ['eus', 'cus']
From there it would be a loop to access each one like:
- ${{ each environmentObject in parameters.environmentObjects }} :
- ${{ each regionAbrv in enviornmentObject.regionAbrvs }} :
This should work for your scenario as well.
Help me please with creating conditions in parameters in the step template.
I want to change parameters in the template depending on variable.Mode value.
For example, this code doesn't work:
- template: ../templates/template1.yml
parameters:
${{ if eq(variables.Mode, 'dev') }}:
mode: dev
namespace: dev-namespace
fqdn: dev.mysite.com
${{ if eq(variables.Mode, 'staging') }}:
mode: staging
namespace: staging-namespace
fqdn: staging.mysite.com
anothervar: value
yetanothervar: value2
The template executes with default values of parameters.
What can I do to set vars depending on variable.Mode value.
You can specify different parameter values in stages. Here is my sample:
azure-pipelines.yml:
variables:
Mode: dev
stages:
- ${{ if eq(variables.Mode, 'dev') }}:
- template: template.yml
parameters:
mode: dev
namespace: dev-namespace
fqdn: dev.mysite.com
- ${{ if eq(variables.Mode, 'staging') }}:
- template: template.yml # Template reference
parameters:
mode: staging
namespace: staging-namespace
fqdn: staging.mysite.com
anothervar: value
yetanothervar: value2
template.yml:
parameters:
- name: mode
default: ''
- name: namespace
default: ''
- name: fqdn
default: ''
- name: anothervar
default: ''
- name: yetanothervar
default: ''
stages:
- stage:
jobs:
- job:
steps:
- script: echo ${{ parameters.mode }}