0

I am looking for a simple step in my yaml pipeline to add 1 to the run-version number every time the task is run successfully. I then want to parse this to the outside, something like version: #{version}# I will then use replace token to pick this up and update the variable in the pipeline.

riQQ
  • 9,878
  • 7
  • 49
  • 66
gawe
  • 1
  • 1
  • 1
    How about the issue? Does the answers below resolve your question, If yes, you could [Accept it as an Answer](https://meta.stackexchange.com/a/5235/515442), so it could help other community members who get the same issues and we could archive this thread, thanks. – LoLance Aug 31 '20 at 07:02

3 Answers3

0

You can use epoch time, A unique timestamp to generate a unique version everytime when you run yaml

nagendra
  • 1
  • 1
0

You can check counter function (https://learn.microsoft.com/en-us/azure/devops/pipelines/process/expressions?view=azure-devops#counter).

For instance I can prepend version of my product (and increment it every single run) to the name of the run by adding this:

name: $(Major).$(Minor).$(Fix)
variables:
  Major: 2
  Minor: 0
  Fix: $[counter(variables['Major'].variables['Minor'], 0)]
steps: 
- step: ...

The result of this increases by +1 every time you run your build. You can print it out in your pipeline or use it as an argument for a task.

evgeny
  • 1,039
  • 1
  • 9
  • 26
0

You can consider storing the unique run version value in Variable Group. So that we can make this unique value available across multiple pipelines/jobs/steps.

1.Create one Variable Group and define the version variable.

enter image description here

2.Manage its security and add the ProjectName Build Service as admin role, save the changes.

3.Then we can add one PS task to update the version value.

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      $url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/distributedtask/variablegroups/{YourVariableGroupID}?api-version=6.0-preview.2"
      $linkedVariableGroup = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}
      $versionNumber = [int]$linkedVariableGroup.variables.version.value
      $versionNumber+=1
      $linkedVariableGroup.variables.version.value=$versionNumber
      $json = $linkedVariableGroup | ConvertTo-Json -Depth 100
      Invoke-RestMethod -Method PUT -Uri $url -ContentType "application/json" -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"} -Body $json
  env:
      SYSTEM_ACCESSTOKEN: $(System.AccessToken)

Add this step at the end of your pipeline, so that this task adds 1 to the version variable every time the pipeline runs successfully. You should reply the {YourVariableGroupID} with your own VariableGroupID in above script.

LoLance
  • 25,666
  • 1
  • 39
  • 73
  • Thanks Lance, I have been testing this but I can't get it too compile in the pipeline. So I want to try this just on its own as a single task to see if it parses to to the AccessToken, but keep getting the following: Encountered error(s) while parsing pipeline YAML: /task.yml (Line: 1, Col: 5): A sequence was not expected /task.yml: (Line: 2, Col: 7, Idx: 31) - (Line: 2, Col: 8, Idx: 32): While parsing a block mapping, did not find expected key. – gawe Aug 18 '20 at 17:04
  • @gawe Can you share the content of your azure-pipelines.yml and task.yml files(edit the question and add more details), so that I can check for you directly. – LoLance Aug 19 '20 at 03:36
  • According to your error message, there's something wrong with your template usage which is not related to my answer above. See [this similar issue](https://stackoverflow.com/questions/60571976/how-to-use-yaml-template-parameters-in-azure-devops-server-2019-on-prem), you can simply create a new repo, and a new yaml pipeline to test if my solution above resolves your issue. – LoLance Aug 19 '20 at 03:38
  • @gawe You don't need to pass the AccessToken as parameter, just use same format like mine, it;'s enough... – LoLance Aug 19 '20 at 03:40
  • I have tested using a couple of Json files and using Replace Token from market place in Azure Devops it worked. Replace Token took the variable from the first Json and copied it into the second variable. So taking that this works, and not wanting to create two new json files for every new pipeline I would like to just add a task in my YAML file which will generate a version number then add 1 to the version number and storing this in a variable. Then a step in the pipeline, replace token will take this variable and replace it in the variable group (versiontest) – gawe Aug 19 '20 at 10:44
  • @gawe It seems that you're resolved the issue and achieved what you want. If so, please check if my answer is helpful for resolving your issue. If it's helpful, you could consider [accepting it as answer](https://meta.stackexchange.com/a/5235/515442). If you use own workaround to resolve the issue, it's recommended to share your solution as answer to help other members with similar issue~ – LoLance Aug 20 '20 at 09:55