azure 管道 - 基于触发分支设置变量

Man*_*hen 5 powershell azure azure-powershell devops azure-devops

我正在尝试使用 powershell 根据触发的分支设置 BuildConfiguration

有谁知道如何做到这一点?

switch($env:Build.SourceBranchName) {
   'master' {$env:BuildConfiguration = Release; break;} 
   'staging' {$env:BuildConfiguration = Staging; break;} 
   'develop' {$env:BuildConfiguration = Dev; break;} 
}

Run Code Online (Sandbox Code Playgroud)

Man*_*hen 13

终于有了这个工作

switch(${env:BUILD_SOURCEBRANCH}) {
   'refs/heads/master' {Write-Host "##vso[task.setvariable variable=BuildConfiguration]Release"; } 
   'refs/heads/staging' {Write-Host "##vso[task.setvariable variable=BuildConfiguration]Staging"; } 
   'refs/heads/develop' {Write-Host "##vso[task.setvariable variable=BuildConfiguration]Dev"; } 
}
Run Code Online (Sandbox Code Playgroud)


Max*_*hom 9

您可以在 yaml 管道的顶部设置变量并随意使用它们。

variables:
  ${{ if eq(variables['Build.SourceBranchName'], 'main') }}: 
    deployTarget: prod
  ${{ if eq(variables['Build.SourceBranchName'], 'develop') }}: 
    deployTarget: dev
Run Code Online (Sandbox Code Playgroud)

并使用:

- task: CmdLine@2
  displayName: Display deployment
  inputs:
  script: |
    echo '${{ variables.deployTarget }}'
Run Code Online (Sandbox Code Playgroud)