根据触发器分支设置YAML变量

时间:2019-08-14 06:23:26

标签: azure-devops

我正在设法了解在devops中定义构建管道的yaml语法。

我想根据哪个分支触发了构建,在文件中设置变量。

# trigger:
 batch: true
 branches:
   include:
    - master
    - develop
    - staging

 variables:
    buildConfiguration: 'Release' # Can I set this according to the branch which triggered the build?

我尝试了以下操作,但似乎无法两次定义变量。

 variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'

 variables:
  condition: eq(variables['Build.SourceBranch'], 'refs/heads/develop')
  buildConfiguration: 'Develop'

 variables:
  condition: eq(variables['Build.SourceBranch'], 'refs/heads/release')
  buildConfiguration: 'Release'

感谢您的帮助:-)

2 个答案:

答案 0 :(得分:3)

如果有人感兴趣,我就结束了。


 trigger:
  batch: true
  branches:
   include:
    - master
    - develop

[truncated] 

 #https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#set-a-job-scoped-variable-from-a-script    
 - pwsh: |
    If ("$(Build.SourceBranch)" -eq "refs/heads/master") {
      Write-Host "##vso[task.setvariable variable=buildConfiguration;]Release"
    }
    If ("$(Build.SourceBranch)" -eq "refs/heads/develop") {
      Write-Host "##vso[task.setvariable variable=buildConfiguration;]Debug"
    }
 - script: | 
    echo building configuration $(buildConfiguration)

 - task: VSBuild@1
   inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactStagingDirectory)"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'
    clean: true
    vsVersion: '15.0'


答案 1 :(得分:2)

我可能会添加一个脚本步骤来计算这些。因此,请像通常一样创建某种脚本来检查$(Build.SourceBranch)的值并设置buildConfiguration的值:

echo '##vso[task.setvariable variable=buildConfiguration]something'