0

How can I use the Azure DevOps counter function in a variable template?

Up until now, I have been using the counter function to set a variable in an pipeline and the value has been set as expected - it started at 1 and has incremented every time I run the pipeline.

Variable template - /Variables/variables--code--build-and-deploy-function-app.yml

variables:
- name: major
  value: '1'
- name: minor
  value: '0'
- name: patch
  value: $[counter(format('{0}.{1}-{2}', variables['major'], variables['minor'], variables['Build.SourceBranchName']), 1)]
- name: branch
  ${{ if eq( variables['Build.SourceBranchName'], 'master' ) }}: 
    value: ''
  ${{ if ne( variables['Build.SourceBranchName'], 'master' ) }}: 
    value: -${{ variables['Build.SourceBranchName'] }}

However, after moving the exact same variables in to a variable template, the value of counter is the literal value specified in the template.

Digging further in to the documentation for templates, I found some words on template expression functions, together with an example of how to use a functon -

You can use general functions in your templates. You can also use a few template expression functions.

Given that counter is listed on the page that the link above refers to, I assumed I would be able to use it. However, no matter what I've tried, I can't get it to work. Here are a few examples -

${{ counter('${{ format('{0}.{1}-{2}', variables['major'], variables['minor'], variables['Build.SourceBranchName']) }}', 1) }}

${{ counter(format('{0}.{1}-{2}', variables['major'], variables['minor'], variables['Build.SourceBranchName']), 1) }}

$[counter('${{ format('{0}.{1}-{2}', variables['major'], variables['minor'], variables['Build.SourceBranchName']) }}', 1)]

What am I doing wrong?


Update

My variable template is as above, an here is how I use it in the pipeline -

pr: none
trigger: none
    
variables:
- template: ../Variables/variables--code--build-and-deploy-function-app.yml

name: ${{ variables.major }}.${{ variables.minor }}.${{ variables.patch }}${{ variables.branch }}

The expanded pipeline obtained from logs after a run is as follows -

pr:
  enabled: false
trigger:
  enabled: false
variables:
- name: major
  value: 1
- name: minor
  value: 0
- name: patch
  value: $[counter(format('{0}.{1}-{2}', variables['major'], variables['minor'], variables['Build.SourceBranchName']), 1)]
- name: branch
  value: -CS-805
name: 1.0.$[counter(format('{0}.{1}-{2}', variables['major'], variables['minor'], variables['Build.SourceBranchName']), 1)]-CS-805

As can be seen from the extended pipeline, the patch variable isn't being evaluated, resulting in the name containing the literal value -

enter image description here

1
  • Hi @David Gard. Test to use the same variables in template and it could work fine. You could refer to the answer and check if I miss some key steps. If it doesn't help, you may share Yaml sample about this issue.This helps me understand how you use variable. Thanks. Commented Aug 4, 2020 at 2:17

1 Answer 1

1

I inserted the same variables into the template and the patch variable works as expected. It seems that your counter is correct.

Here are my sample, you could refer to it:

Template Yaml: build.yml

variables:
- name: major
  value: '1'
- name: minor
  value: '0'
- name: patch
  value: $[counter(format('{0}.{1}-{2}', variables['major'], variables['minor'], variables['Build.SourceBranchName']), 1)]

Azure-Pipelines.yml

name: $(patch)

trigger:
- none

variables:
- template: build.yml  

pool:
  vmImage: 'windows-latest'

steps:
- script: echo $(patch)
  displayName: 'Run a one-line script'

In order to make the result more intuitive, I set patch variable to build name.

Here is the result:

enter image description here

Update:

Test with $(varname) and it could work as expected.

trigger:
- none

variables:
- template: build.yml  


name: $(major).$(minor)-$(patch)$(branch)

Result:

enter image description here

The $(varname) means runtime before a task executes.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your reply. I found that once I'd moved variables in to a template, I could no longer reference then using $(varName) and instead had to switch to ${{ variables.varName }} - it's interesting that this appears to have worked for you. I have updated my question to include details of this, along with a snippet from my pipeline and the same snippet from the expanded template obtained from the logs.
Hi @DavidGard. Thanks for your information. Test with ${{ variables.varName }}, I could reproduce this issue. But the $(Varname) could work fine. Please check my update. When you use $(varname), what problem did you encounter.
I think I've found the issue - as well as the variable template, I have stages in templates. The name of some of jobs defined by one of those stages includes the value of a parameter passed in from the pipeline, the value of which is $(functionAppSlot), which is set in the variable template. This results in a failure to even start the pipeline run. Error - Stage BVT_UK_South_Internal job Update_App_Settings_In_$(functionAppSlot) has an invalid name. So I've changed references to $(functionAppSlot) to ${{ functionAppSlot }} instead, and now the pipeline is running and the name is correct.
So I think the issue with ${{ patch }} is a red herring, caused by me thinking I'd have to change EVERY variable reference to {{ variables.varName }} do to the error in the previous comment. However, it doesn't really feel right that I should have to use different syntax to define one variable - it would be great to know why that is, and whether the issue will be addressed in the future.
Great! Glad to know that this issue has been resolved. Maybe you could refer to this doc to learn variable syntax: learn.microsoft.com/en-us/azure/devops/pipelines/process/… Different syntax means getting values at different times
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.