title: How do you properly use Azure DevOps functions in a variable template?
body: <p>How can I use the Azure DevOps <code>counter</code> function in a variable template?</p>
  <p>Up until now, I have been using the <code>counter</code> 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.</p>
  <p>Variable template - /Variables/variables--code--build-and-deploy-function-app.yml</p>
  <pre><code>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'] }}
  </code></pre>
  <p>However, after moving the exact same variables in to a <a href="https://docs.microsoft.com/en-us/azure/devops/pipelines/process/templates?view=azure-devops#variable-reuse" rel="nofollow noreferrer">variable template</a>, the value of <code>counter</code> is the literal value specified in the template.</p>
  <p>Digging further in to the documentation for templates, I found some words on <a href="https://docs.microsoft.com/en-us/azure/devops/pipelines/process/templates?view=azure-devops#template-expressions" rel="nofollow noreferrer">template expression functions</a>, together with an example of how to use a functon -</p>
  <blockquote>
  <p>You can use <a href="https://docs.microsoft.com/en-us/azure/devops/pipelines/process/expressions?view=azure-devops#functions" rel="nofollow noreferrer">general functions</a> in your templates. You can also use a few template expression functions.</p>
  </blockquote>
  <p>Given that <code>counter</code> 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 -</p>
  <pre><code>${{ 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)]
  </code></pre>
  <p>What am I doing wrong?</p>
  <hr />
  <h1>Update</h1>
  <p>My variable template is as above, an here is how I use it in the pipeline -</p>
  <pre><code>pr: none
  trigger: none
      
  variables:
  - template: ../Variables/variables--code--build-and-deploy-function-app.yml
  
  name: ${{ variables.major }}.${{ variables.minor }}.${{ variables.patch }}${{ variables.branch }}
  </code></pre>
  <p>The expanded pipeline obtained from logs after a run is as follows -</p>
  <pre><code>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
  </code></pre>
  <p>As can be seen from the extended pipeline, the <code>patch</code> variable isn't being evaluated, resulting in the <code>name</code> containing the literal value -</p>
  <p><a href="https://i.stack.imgur.com/FZG7s.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FZG7s.png" alt="enter image description here" /></a></p>