title: Azure Pipelines - Setting Custom Variable to Build Number
body: <p>In my Azure Build Pipeline (classic, not YAML) I set my build number to be the branch name and then a revision number variable. This was my process for that:
  Pipelines -> Pipelines -> {my pipeline} -> Edit -> Options -> Build Number Format</p>
  
  <pre><code>$(SourceBranchName)$(Rev:.r)
  </code></pre>
  
  <p>In my testing, that works great. </p>
  
  <p>Now, in my Release Pipeline, the first script I run is a PowerShell script that takes the build number, and applies it to a local variable (MyBuild) I created. The script is as follows:</p>
  
  <pre><code>Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)"
  $buildNumber = $Env:BUILD_BUILDNUMBER
  $pipeline.variables.MyBuild.value = $buildNumber
  </code></pre>
  
  <p>This variable is used later in the pipeline to create a folder that houses my release files. </p>
  
  <pre><code>$(BuildDirectory)/$(MyBuild)/Debug
  </code></pre>
  
  <p>For some reason, my variable is always one build behind. For example, if my build number is master.5, the folder that is created by my Release Pipeline is master.4. I have tried changing the order my scripts are in the pipeline, but that doesn't solve anything. It is weird because my Build Pipeline is correct (always named properly, ex. master.1, master.2, master.3, etc.) but my Release Pipeline variable is always one revision behind. </p>