how to compare parameter value with a string in Azure yaml pipeline - azure

Parameter defined as -
parameters:
- name: deploy_images
displayName: Publish images to Project Repository?
type: string
default: NO
values:
- sandbox
- dev
- production
Then following condition fails -
condition: and( ne(${{ parameters.deploy_images }}, 'NO'), eq(${{ parameters.build_image }}, True))
Error -
##[error]Unrecognized value: 'sandbox'. Located at position 9 within expression: and( ne(sandbox, NO), eq(True, True)). For more help, refer to https://go.microsoft.com/fwlink/?linkid=842996

Provide single quotes around the expression:
condition: and( ne( '${{ parameters.deploy_images }}', 'NO'), eq(${{ parameters.build_image }}, True))
Also make sure, that 'NO' is actually among the values, that you allow for this parameter (I had to add it before it worked in my environment):
parameters:
- name: deploy_images
displayName: Publish images to Project Repository?
type: string
default: NO
values:
- sandbox
- dev
- production
- NO

Related

Azure pipeline yaml: Cannot use strategy matrix variable in conditions

I am having problem in calling template as the pre-condition is not getting satisfied, I am comparing parameter value to select which template to call at runtime, here I am having parameter value coming from strategy matrix and I think that might be the issue.
pipeline yaml :
stages:
- stage: build_stage
jobs:
- job:
strategy:
matrix:
Dev_Build:
build_type: "dev"
Rel_Build:
build_type: "rel"
steps:
- template: test_conditional_template.yml#templates_repo
parameters:
build: $(build_type)
test_conditional_template.yml :
parameters:
- name: build
type: string
default: 'dev'
steps:
- ${{ if eq(parameters.build, 'dev') }}: # this condition is not working
- template: test_conditional_template_generic.yml#templates_repo
parameters:
build_quality: ${{ parameters.build }}
test_conditional_template-generic.yml :
parameters:
- name: build
type: string
default: 'dev'
steps:
- script: |
echo "build quality : ${{ parameters.build }}"
displayName: "print build quality, test_conditional_template-generic"
I am not even able to compare the parameter value with normal script step too :
- script: |
echo "build quality : ${{ parameters.build }}"
condition: eq('${{ parameters.build }}', 'dev')
This is what pipeline prints for above step :
if template is called with hard-coded parameter value, condition works fine :
steps:
- template: test_conditional_template.yml#templates_repo
parameters:
build: 'dev' #this works fine with above template code
we're avoiding so much of boilerplate code with usage of strategy matrix so dont really want to get away from it, any pointers will be helpful.
I wanted to add this as a comment but my first thought was that it's comparing 'dev' with "dev" in the first example and ''dev'' (double single quotes) in the script example, so both would evaluate as false,
I've been writing quite a few templates and have had a lot of similar issues, it would be nice if we could find out before checking the code in but ehh yaml...

Azure yaml pipeline display parameter value and have another actual value

I have a yaml pipeline in which I have a string parameter with several values. I want to have a human readable options to choose from but when reading the parameter I want to have an actual different value.
a psuedo yaml declaration for what I mean:
- name: actionParam
displayName: Choose Action
type: string
default: 'Action0': 'no action'
values:
- 'Action1' : 'drop tables'
- 'Action2' : 'hack NASA'
- 'Action3' : 'solve world hunger'
In this example I want for the user to see the options on the right but when doing {{parameters.actionParam}} I want to get the corresponding option on the left.
Is such a thing possible?
Based on your requirement, you need to display parameter value and have another actual value.
I am afraid that there is no out-of-box method to achieve this requirement.
For a workaround, I suggest that you can define the parameters with the options on the right and then you can set the variable value based on the value of the parameters.
Here is an example:
parameters:
- name: actionParam
displayName: Choose Action
type: string
default: 'no action'
values:
- 'drop tables'
- 'hack NASA'
- 'solve world hunger'
variables:
- ${{ if eq(parameters.actionParam, 'solve world hunger') }}:
- name: actionParam
value: Action1
- ${{ if eq(parameters.actionParam, 'drop tables') }}:
- name: actionParam
value: Action2
- ${{ if eq(parameters.actionParam, 'hack NASA') }}:
- name: actionParam
value: Action3
Result:
Then you can use the actual value with the format: $(actionParam)

Hide/Show parameters based on a condition in Azure DevOps Pipelines YAML

I have a use case where I have multiple parameters in ADO build pipeline but those parameters should be visible based on a condition e.g. the branch name (for releases/* branches). Following is a template to showcase what I am trying to achieve:
variables:
- name: systemAccessToken
value: $(System.AccessToken)
- name: system.debug
value: true
- ${{if startsWith(variables['Build.SourceBranch'], 'refs/heads/releases/') }}:
- name: START_DATE_TIME
value: ${{ parameters.startDateTime }}
- name: END_DATE_TIME
value: ${{ parameters.endDateTime }}
- ${{if startsWith(variables['Build.SourceBranch'], 'refs/heads/releases/') }}:
parameters:
- name: startDateTime
displayName: Enter the Start Datetime
type: string
default: 04-14-2022 14:55:00
- name: endDateTime
displayName: Enter the End Datetime
type: string
default: 04-14-2022 23:00:00
resources:
repositories:
...
...
stages:
...
...
The above doesn't work but I would like to get an idea if there is some way I can achieve this using any workaround like injecting the parameters at runtime. Would appreciate any help on this
Sorry, can't be done. The list of parameters is fixed and the pipeline is compiled, before it ever knows about any runtime variables such as Build.SourceBranch.
Instead, you could perhaps define two different pipelines with different sets of parameters? You can avoid duplicating code by extracting the common elements (resources, variables, stages) into a template.

Is it possible to add runtime parameters based on condition - Azure Devops Pipeline

My current azure pipeline looks like following -
parameters:
- name: Deploy
type: Boolean
default: false
- name: Stages
type: string
values:
- Stg A
- Stg B
- Stg C
- Stg D
- Stg E
I was trying to add a condition in such a way that if user checks Deploy on running pipeline, it should dynamically show up only Stg A, Stg B as values for Stages. And if they uncheck Deploy, it should show Stg C, Stg D and Stg E.
I tried to add conditions in following way -
parameters:
- name: Deploy
type: Boolean
default: false
- name: Stages
type: string
${{ if eq(parameters['Deploy'], 'true' ) }}:
values:
- Stg A
- Stg B
${{ if ne(parameters['Deploy'], 'true' ) }}:
values:
- Stg C
- Stg D
- Stg E
However, the following conditional way worked for variables, but did not work for parameters. Is there any way to achieve this in Azure pipelines.
Is it possible to add runtime parameters based on condition - Azure Devops Pipeline
I am afraid it it impossible to runtime parameters conditionally define values based on another parameter value.
Because the parameters need to be defined before the pipeline run starts. As stated in the document Runtime parameters:
Parameters are only available at template parsing time. Parameters are
expanded just before the pipeline runs so that values surrounded
by ${{ }} are replaced with parameter values. Use variables if you
need your values to be more widely available during your pipeline run.
As workaround, we could use condition in the steps:
parameters:
- name: Deploy
type: Boolean
default: false
stages:
- ${{ if eq(parameters['Deploy'], 'true' ) }}:
- template: template.yml
parameters:
stages:
- "Stg A"
- "Stg B"
- ${{ if ne(parameters['Deploy'], 'true' ) }}:
- template: template.yml
parameters:
stages:
- "Stg C"
- "Stg D"
- "Stg E"

Conditional Variable Assignment in Azure Devops Build Pipelines

Azure Pipelines has Expressions and Conditions, but I can not find any way to assign one of two values from the parameter to a variable, based on a condition.
Is there anyway I can achieve this?
parameters:
- name: major
displayName: " Major Version(Default 3)"
type: string
default: 3
- name: minor
displayName: 'Minor Version(Default 15)'
type: string
default: 15
values:
- 15
- 16
- 17
- 18
- 19
- name: patch
displayName: 'Patch Version(Default 0)'
type: string
default: 0
- name: checksum
displayName: 'Pattern is minor version.patch version, checksum'
type: string
values:
- '15.0, 8003fcfe34dc224be816921e9543fa58ba195653'
- '18.1, d8e42f7a7b85ad72ba27117d1f13a790228f1369'
- '19.0, 4e66eeedba4e1f3ea26cadb0c2bfa96264e7c8fb'
variables:
major: ${{ parameters.major }}
minor: ${{ parameters.minor }}
patch: ${{ parameters.patch }}
finalChecksum : ${{ if contains(parameters.checksum, parameters.minor.parameters.patch) }}: parameters.checksum.split(',')[1]
what I'm after is finalchecksum: (as validation works fine)
if parameter.checksum contains/startsWith minor.patch:
then
finalchecksum = parameters.checksum.split(',')[1]
so here,
if the minor and patch versions are 18.1 then finalchecksum = d8e42f7a7b85ad72ba27117d1f13a790228f1369
In my task, I have something like this :
- bash: |
echo $(finalChecksum)
Then it prints below error:
Line 1: `echo True:15.0, 8003fcfe34dc224be816921e9543fa58ba195653.Split(",")[0]'
All I'm interested in printing the checksum and I even don't know parameters.checksum.split(',')[1] does exist in AzDo world. Any suggestions would be great.
You can use built-in functions to replace your variable. I used replace and format functions in my sample.
Here is my configuration:
parameters:
- name: major
displayName: " Major Version(Default 3)"
type: string
default: 3
- name: minor
displayName: 'Minor Version(Default 15)'
type: string
default: 15
values:
- 15
- 16
- 17
- 18
- 19
- name: patch
displayName: 'Patch Version(Default 0)'
type: string
default: 0
- name: checksum
displayName: 'Pattern is minor version.patch version, checksum'
type: string
values:
- '15.0, 8003fcfe34dc224be816921e9543fa58ba195653'
- '18.1, d8e42f7a7b85ad72ba27117d1f13a790228f1369'
- '19.0, 4e66eeedba4e1f3ea26cadb0c2bfa96264e7c8fb'
variables:
major: ${{ parameters.major }}
minor: ${{ parameters.minor }}
patch: ${{ parameters.patch }}
format: $[format('{0}.{1}, ', variables['minor'], variables['patch'])]
${{ if contains(parameters.checksum, parameters.minor.parameters.patch) }}:
finalChecksum : $[replace('${{parameters.checksum}}',variables['format'],'')]
Result of echo $(finalChecksum):
In addition, I followed this document to conditionally assign a variable.

Resources