Reputation: 17818
I'm trying to insert an extra parameter in my pipeline based on a commit message fragment, running Azure DevOps Server 2020. I tried to implement it with the following code, without luck:
variables:
- name: commitMessage
value: $(Build.SourceVersionMessage)
steps:
- template: myTemplate.yaml
parameters:
${{ if contains(variables['commitMessage'], 'SPECIAL_MESSAGE') }}:
specialParam: 'someValue'
During investigation, I found that the expressions behave somewhat unexpected:
variables:
- name: commitMessage
value: $(Build.SourceVersionMessage)
steps:
- bash: |
echo "${{ variables['commitMessage'] }}"
echo "${{ contains(variables['commitMessage'], 'SPECIAL_MESSAGE') }}"
echo "${{ contains('test SPECIAL_MESSAGE', 'SPECIAL_MESSAGE') }}"
The script output is:
test SPECIAL_MESSAGE
False
True
The first line of the script outputs the commit message correctly. But the contains()
function in the second line of the script seems to be unable to process the variable, even though the commit message contains the SPECIAL_MESSAGE, the expression returns False
.
However, if I set my variable to a static string instead of the $(Build.SourceVersionMessage)
variable, the expression returns True
, and I am able to add the extra parameter:
variables:
- name: commitMessage
value: 'test SPECIAL_MESSAGE'
Any idea why the pipeline behaves like this, or how to make it work?
Upvotes: 6
Views: 3655
Reputation: 17818
The documentation states about the Build.SourceVersionMessage
variable:
The variable is only available on the step level and is neither available in the job nor stage levels (i.e. the message is not extracted until the job had started and checked out the code).
This means that it's not possible to properly use this variable in compile time expressions, when it comes to conditionally inserting steps or parameters. There is a feature request to make this variable fully available at compile time: https://developercommunity.visualstudio.com/t/post/10029833
Upvotes: 2
Reputation: 14104
You can change to like as below:
variables:
- name: commitMessage
value: ${{ variables['Build.SourceVersionMessage'] }}
The expression '${{ variables['Build.SourceVersionMessage'] }}
' would pass the value to the custom variable 'commitMessage
' at compile time. This is equivalent to manually assigning the static value before running the pipeline.
And the expression '$(Build.SourceVersionMessage)
' is at runtime.
I have tested with the expression '${{ variables['Build.SourceVersionMessage'] }}
', it would be able to work as expected.
For more details, you can reference the document about "Understand variable syntax".
Upvotes: 1