0
votes

I'm trying to set the Package Version for nuget packages being deployed to Azure Artifacts in a Pipeline.

The requirement is that if I'm on a feature branch I need to append -preview to the end of the package version.

So based on this article here: https://www.koskila.net/fun-with-azure-devops-nuget-package-versioning/

I've set up the following variables:

enter image description here

and this is the piepline .yml

steps:

- task: PowerShell@2
  displayName: 'Set Package Version Type'
  inputs:
    targetType: 'inline'
    script: |
      # if trigger branch is not master - then append -prelease to package version
      if($Env:BUILD_SOURCEBRANCHNAME -ne 'master'){
        $Env:PackageVersionType = '-preview'
        $Env:PackageVersion = "$Env:Major.$Env:Minor.$Env:Patch$Env:PackageVersionType"
      }
      Write-Host $Env:PackageVersionType
      Write-Host $Env:PackageVersion

# Create Nuget Package - After running tests
- task: DotNetCoreCLI@2
  displayName: "Create Nuget Package"
  condition: succeeded()
  inputs:
    command: 'pack'
    packagesToPack: '**/my.csproj'
    versioningScheme: 'byEnvVar'
    versionEnvVar: 'PackageVersion'
    arguments: '--configuration $(buildConfiguration)'

# Push Nuget Package to Artifacts, so it can be consumed
- task: NuGetCommand@2
  displayName: "Publish Nuget Package"
  condition: succeeded()
  inputs:
    command: 'push'
    packagesToPush: '$(Build.ArtifactStagingDirectory)/**/*.nupkg;!$(Build.ArtifactStagingDirectory)/**/*.symbols.nupkg'
    nuGetFeedType: 'internal'
    publishVstsFeed: ''

As you can see in the first step, I'm trying to set the $Env:PackageVersionType to -preview, when the branch name is not master.

The Console output for this is:

enter image description here

The question is, how do I set the $Env:PackageVersion based on the trigger branch in my pipeline?

1

1 Answers

0
votes

okay so figured this out:

declared a variable of the name packageVersionType, and set it's value if Env:BUILD_SOURCEBRANCHNAME is not the main branch to -preview

variables:
  packageVersionType: ''

steps:

- task: PowerShell@2
  displayName: 'Set Package Version Type'
  inputs:
    targetType: 'inline'
    script: |
      # if trigger branch is not master - then append -prelease to package version
      if($Env:BUILD_SOURCEBRANCHNAME -ne 'master'){
        $localVersionType = '-preview'
        Write-Host "##vso[task.setvariable variable=packageVersionType;]$localVersionType"
      }

Then in the Nuget Pack:

# Create Nuget Package - After running tests
- task: DotNetCoreCLI@2
  displayName: "Create Nuget Package"
  condition: succeeded()
  inputs:
    command: 'pack'
    packagesToPack: '*Myproj.csproj'
    versioningScheme: 'byEnvVar'
    versionEnvVar: 'PackageVersion'
    arguments: '--configuration $(buildConfiguration)'

# Push Nuget Package to Artifacts, so it can be consumed
- task: NuGetCommand@2
  displayName: "Publish Nuget Package"
  condition: succeeded()
  inputs:
    command: 'push'
    packagesToPush: '$(Build.ArtifactStagingDirectory)/**/*.nupkg;!$(Build.ArtifactStagingDirectory)/**/*.symbols.nupkg'
    nuGetFeedType: 'internal'
    publishVstsFeed: ''

and declare the following in the Variables for the pipeline.

PackageVersion = `$(Major).$(Minor).$(Patch)$(packageVersionType)` 
Patch = $[counter(format('{0}.{1}', variables['Major'], variables['Minor']), 0)]