根据触发器分支设置环境变量-Azure DevOps Pipeline

时间:2020-10-23 10:08:00

标签: azure-devops azure-pipelines pipeline

我正在尝试为要在管道中部署到Azure Artifacts的nuget程序包设置程序包版本。

要求是,如果我在功能分支上,则需要在软件包版本的末尾附加-preview

因此根据此处的这篇文章: https://www.koskila.net/fun-with-azure-devops-nuget-package-versioning/

我设置了以下变量:

enter image description here

这就是条线.yml

步骤:

- 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: ''

如第一步所示,当分支名称不是$Env:PackageVersionType时,我试图将-preview设置为master

控制台输出为:

enter image description here

问题是,如何根据管道中的触发分支设置$Env:PackageVersion

1 个答案:

答案 0 :(得分:0)

好的,所以想通了:

声明了一个名称为packageVersionType的变量,如果Env:BUILD_SOURCEBRANCHNAME 不是-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"
      }

然后在Nuget包中:

# 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: ''

,然后在管道的变量中声明以下内容。

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