Search code examples
azureazure-devopsazure-pipelinesmauiversioning

Azure DevOps automatic versioning of .Net maui Application


I'm currently building an Azure DevOps pipeline to automatically deploy my .NET Maui application to the Google Play Store.

The building, signing and publishing part already works. but now I'd like to automate the versioning of the app as well.

For the Version Code (ApplicationVersion in .csproj) I'm using a counter like this:

  - name: versionCode
    value: $[counter('versionCode', 1)]

For the Display Version however I'd like to use a Major.Minor.Patch numbering.

So far I've found that I can recover my Major.Minor numbering from my .csproj file using the following script:

- task: PowerShell@2
  name: setVariable
  inputs:
    targetType: 'inline'
    script: |
         $xml = [Xml] (Get-Content .\MyApp\MyApp.csproj)
         $version = $xml.Project.PropertyGroup.ApplicationDisplayVersion
         Write-Host "##vso[task.setvariable variable=versionNumber;isoutput=true]$version"

Which I can then use in another step like this: $(setVariable.versionNumber)

My question is: Is there a way to use a counter (or alternative) to automatically add the Patch number when my pipeline runs, which also resets when I adjust my Major or Minor numbering?

Most answers I found here on StackOverflow have the version number hard-coded in the pipeline (which I'd like to avoid, as to not having to change the pipeline every time my Major or Minor version number changes)

variables:
  - name: versionNumber
    value: 'will be filled in later'
  - name: buildNumber
    value: $[counter(variables['versionNumber'], 0)]
  - name: versionCode
    value: $[counter('versionCode', 1)]

steps:
- task: PowerShell@2
  name: setVariable
  inputs:
    targetType: 'inline'
    script: |
         $xml = [Xml] (Get-Content .\MyApp\MyApp.csproj)
         $version = $xml.Project.PropertyGroup.ApplicationDisplayVersion
         Write-Host "##vso[task.setvariable variable=versionNumber;isoutput=true]$version.$env:buildNumber"
         
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
         Write-Host $(setVariable.versionNumber)

This is what I've tried so far, but this gives me 2 issues:

  1. the buildNumber counter is not reset when I increase the version number in my .csproj file
  2. This introduces a space " " between my versionNumber and the .

Solution

  • My question is: Is there a way to use a counter (or alternative) to automatically add the Patch number when my pipeline runs, which also resets when I adjust my Major or Minor numbering?

    As per the doc, the counter function can only be used in an expression that defines a variable. It cannot be used as part of a condition for a step, job, or stage.

    However, your Major.Minor number comes from ApplicationDisplayVersion in csproj in powershell task with logging command. Hence, you need to output the Major.Minor number in 1st job, and map it in 2nd job , then you can add patch with counter function.

    Details below:

    I have a sample csproj, which has 1.0 for ApplicationDisplayVersion which will be used as Major.Minor number.

    enter image description here

    Used sample yaml:

    pool:
      vmImage: Windows-latest
    
    jobs:
      - job: getversion
        steps:
        - task: PowerShell@2
          name: setVariable
          inputs:
            targetType: 'inline'
            script: |
                $xml = [Xml] (Get-Content .\MyApp\MyApp.csproj)
                $version = $xml.Project.PropertyGroup.ApplicationDisplayVersion
                Write-Host "##vso[task.setvariable variable=MajorMinor;isoutput=true]$version"
    
      - job: useversion
        dependsOn: getversion  
        variables:
          # map the output variable from previous into this job
          - name: MajorMinorNumber
            value: $[ dependencies.getversion.outputs['setVariable.MajorMinor'] ]
          # set the patch number counter on MajorMinorNumber
          - name: patch
            value: $[counter(variables['MajorMinorNumber'], 1)]
    
        steps:
          - task: PowerShell@2
            name: versionnumber
            inputs:
              targetType: 'inline'
              script: |
                  Write-Host $(MajorMinorNumber).$(patch)
    

    To run the pipeline, it will output versionumber as 1.0.1, 1.0.2, which is counter correctly.

    enter image description here

    I changed the ApplicationDisplayVersion value in csproj from 1.0 to 1.1.

    Run the pipeline, the number is reset and counter for new:

    enter image description here