Azure Pipeline Variables Package version

Date: 2024-10-21

Available variables:

https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml

Common used variables:

$(Build.BuildNumber)
$(Build.SourcesDirectory)
$(Build.StagingDirectory)

Making use of your own version number is in Azure Pipelines a terribly difficult task for some reason, a work-around below.

Define a ‘PackageVersion’ variable, with DayOfYear and Revision number (available only in BuildNumber/Name)

trigger:
  branches:
    include:
      - refs/heads/main
  batch: True
name: $(BuildDefinitionName).$(Date:yy)$(DayOfYear).$(Rev:r).$(Build.SourceVersion)
variables:      
  PackageVersion:  $[format('1.0.{0}.{1}', split(variables['Build.BuildNumber'], '.')[1], split(variables['Build.BuildNumber'], '.')[2])]
jobs:
  - job: Phase_1
    displayName: Agent job 1
    pool:
      name: Web-Agents
    steps:
      - checkout: self
        clean: true
      - task: BatchScript@1
        name: BatchScript_2
        displayName: dotnet restore (restore Nu-Get packages)
        inputs:
          filename: '"C:\Program Files\dotnet\dotnet.exe"'
          arguments: restore -f --no-cache
      - task: BatchScript@1
        name: BatchScript_3
        displayName: dotnet build (release build)
        inputs:
          filename: '"C:\Program Files\dotnet\dotnet.exe"'
          arguments: build -c Release --no-restore  /p:Version=$(PackageVersion)
      - task: BatchScript@1
        name: BatchScript_4
        displayName: dotnet test (Domain.Tests)
        continueOnError: True
        enabled: False
        inputs:
          filename: '"C:\Program Files\dotnet\dotnet.exe"'
          arguments: test -c Release Domain.Tests\Domain.Tests.csproj --no-build
      - task: BatchScript@1
        name: BatchScript_5
        displayName: dotnet publish (create published website)
        inputs:
          filename: '"C:\Program Files\dotnet\dotnet.exe"'
          arguments: publish -c Release  WebApi\WebApi.csproj --no-build -o WebApi\published-app
      - task: BatchScript@1
        name: BatchScript_6
        displayName: dotnet publish (publish databaseupdater)
        inputs:
          filename: '"C:\Program Files\dotnet\dotnet.exe"'
          arguments: publish -c Release  DatabaseUpdater\DatabaseUpdater.csproj --no-build -o DatabaseUpdater\published-app
      - task: BatchScript@1
        name: publishing_WorkerService
        displayName: dotnet publish (create published WorkerService)
        inputs:
          filename: '"C:\Program Files\dotnet\dotnet.exe"'
          arguments: publish -c Release  WorkerService\WorkerService.csproj --no-build -o WorkerService\published-app
      - task: BatchScript@1
        name: BatchScript_8
        displayName: dotnet pack (create WebApi Nu-Get package)
        inputs:
          filename: '"C:\Program Files\dotnet\dotnet.exe"'
          arguments: pack -c Release  WebApi\WebApi.csproj --no-build /p:PackageVersion=$(PackageVersion) -o $(Build.StagingDirectory)
      - task: BatchScript@1
        name: BatchScript_9
        displayName: dotnet pack (create DatabaseUpdater Nu-Get package)
        inputs:
          filename: '"C:\Program Files\dotnet\dotnet.exe"'
          arguments: pack -c Release  DatabaseUpdater\DatabaseUpdater.csproj --no-build /p:PackageVersion=$(PackageVersion) -o $(Build.StagingDirectory)
      - task: BatchScript@1
        name: packing_WorkerService
        displayName: dotnet pack (create WorkerService Nu-Get package)
        inputs:
          filename: '"C:\Program Files\dotnet\dotnet.exe"'
          arguments: pack -c Release  WorkerService\WorkerService.csproj --no-build /p:PackageVersion=$(PackageVersion) -o $(Build.StagingDirectory)         
      - task: OctopusPush@5
        name: OctopusPush_11
        displayName: Push Packages to Octopus
        inputs:
          OctoConnectedServiceName: "Octopus Deploy"
          Space: "Spaces-1"
          Package: '$(Build.StagingDirectory)\*.nupkg'
          Replace: "false"
      - task: OctopusCreateRelease@6
        displayName: Create Octopus Release
        inputs:
          OctoConnectedServiceName: 'Octopus Deploy'
          Space: 'Applicatiebeheer'
          Project: 'My API'
          ReleaseNumber: '$(PackageVersion)'
          GitRef: 'main'
        
89680cookie-checkAzure Pipeline Variables Package version