Azure devops 基于一组布尔参数创建MSBuild参数字符串

Azure devops 基于一组布尔参数创建MSBuild参数字符串,azure-devops,yaml,azure-pipelines,Azure Devops,Yaml,Azure Pipelines,作为AzureDevops用户,我希望通过打开和关闭“运行管道”UI上的复选框,能够手动传递不同的MSBuild常量(通过DefineConstants) 例如: 我通过为当前管道定义两个布尔参数来实现这一点: 参数: -名称:EnableDebuggerMessages displayName:“启用调试错误消息” 类型:布尔型 默认值:false -名称:enableAnalytics displayName:“启用分析” 类型:布尔型 默认值:true 现在,我想为包含已启用常量的MS

作为AzureDevops用户,我希望通过打开和关闭“运行管道”UI上的复选框,能够手动传递不同的MSBuild常量(通过
DefineConstants
) 例如:

我通过为当前管道定义两个布尔参数来实现这一点:

参数:
-名称:EnableDebuggerMessages
displayName:“启用调试错误消息”
类型:布尔型
默认值:false
-名称:enableAnalytics
displayName:“启用分析”
类型:布尔型
默认值:true
现在,我想为包含已启用常量的MSBuild生成一个字符串。示例:

/p:DefineConstants="ENABLE_ANALYTICS"
/p:DefineConstants="ENABLE_DEBUG_MESSAGES;ENABLE_ANALYTICS"
因此,我可以通过以下方式将它们传递给MSBuild:

- task: XamariniOS@2
  inputs:
    args: '<HERE>'
-任务:XamariniOS@2
投入:
参数:“”

我已经尝试了我能想到的所有可能的表达式组合,除了。

更新了:我在一个微不足道的管道上(又一个)试了试,得出了以下结论:

pool: MyBuildPool

parameters:
- name: ENABLE_ANALYTICS
  displayName: "Analytics"
  type: boolean
  default: false
- name: ENABLE_DEBUG_MESSAGES
  displayName: "Debug Messages"
  type: boolean
  default: true
- name: ENABLE_SOMETHING_ELSE
  displayName: "Something Else"
  type: boolean
  default: false

variables:
- ${{ each p in parameters }}:
  - name: "DEFINECONSTANT_${{p.key}}"
    value: ${{ p.value }}
  
steps:
- pwsh: |
    $constantVars = Get-ChildItem -Path Env:\ | Where-Object Name -like "DEFINECONSTANT_*"
    $constantList = ""
    $constantVars | ForEach-Object {
        $constantList += "$($_.Name.Replace('DEFINECONSTANT_', ''))=$($_.Value);"
    }
    Write-Host "/p:DefineConstants=`"$constantList`""
  displayName: 'Combine parameter values'
当我将Azure管道扩展到该文件时,我得到:

parameters:
- name: ENABLE_ANALYTICS
  displayName: "Analytics"
  type: boolean
  default: false
- name: ENABLE_DEBUG_MESSAGES
  displayName: "Debug Messages"
  type: boolean
  default: true
- name: ENABLE_SOMETHING_ELSE
  displayName: "Something Else"
  type: boolean
  default: false
variables:
- name: DEFINECONSTANT_ENABLE_ANALYTICS
  value: False
- name: DEFINECONSTANT_ENABLE_DEBUG_MESSAGES
  value: True
- name: DEFINECONSTANT_ENABLE_SOMETHING_ELSE
  value: False
stages:
- stage: __default
  jobs:
  - job: Job
    pool:
      name: MyPoolName
    steps:
    - task: PowerShell@2
      displayName: 'Combine parameter values'
      inputs:
        targetType: inline
        script: |
          $constantVars = Get-ChildItem -Path Env:\ | Where-Object Name -like "DEFINECONSTANT_*"
          $constantList = ""
          $constantVars | ForEach-Object {
              $constantList += "$($_.Name.Replace('DEFINECONSTANT_', ''))=$($_.Value);"
          }
          Write-Host "/p:DefineConstants=`"$constantList`""
        pwsh: true

这更接近你想要的吗?您可以使用不同常量名称、说明和默认值的复选框。

更新了:我在一个普通管道上对其进行了(另一次)破解,并得出以下结论:

pool: MyBuildPool

parameters:
- name: ENABLE_ANALYTICS
  displayName: "Analytics"
  type: boolean
  default: false
- name: ENABLE_DEBUG_MESSAGES
  displayName: "Debug Messages"
  type: boolean
  default: true
- name: ENABLE_SOMETHING_ELSE
  displayName: "Something Else"
  type: boolean
  default: false

variables:
- ${{ each p in parameters }}:
  - name: "DEFINECONSTANT_${{p.key}}"
    value: ${{ p.value }}
  
steps:
- pwsh: |
    $constantVars = Get-ChildItem -Path Env:\ | Where-Object Name -like "DEFINECONSTANT_*"
    $constantList = ""
    $constantVars | ForEach-Object {
        $constantList += "$($_.Name.Replace('DEFINECONSTANT_', ''))=$($_.Value);"
    }
    Write-Host "/p:DefineConstants=`"$constantList`""
  displayName: 'Combine parameter values'
当我将Azure管道扩展到该文件时,我得到:

parameters:
- name: ENABLE_ANALYTICS
  displayName: "Analytics"
  type: boolean
  default: false
- name: ENABLE_DEBUG_MESSAGES
  displayName: "Debug Messages"
  type: boolean
  default: true
- name: ENABLE_SOMETHING_ELSE
  displayName: "Something Else"
  type: boolean
  default: false
variables:
- name: DEFINECONSTANT_ENABLE_ANALYTICS
  value: False
- name: DEFINECONSTANT_ENABLE_DEBUG_MESSAGES
  value: True
- name: DEFINECONSTANT_ENABLE_SOMETHING_ELSE
  value: False
stages:
- stage: __default
  jobs:
  - job: Job
    pool:
      name: MyPoolName
    steps:
    - task: PowerShell@2
      displayName: 'Combine parameter values'
      inputs:
        targetType: inline
        script: |
          $constantVars = Get-ChildItem -Path Env:\ | Where-Object Name -like "DEFINECONSTANT_*"
          $constantList = ""
          $constantVars | ForEach-Object {
              $constantList += "$($_.Name.Replace('DEFINECONSTANT_', ''))=$($_.Value);"
          }
          Write-Host "/p:DefineConstants=`"$constantList`""
        pwsh: true
这更接近你想要的吗?您可以使用具有不同常量名称、说明和默认值的复选框。

您可以根据布尔参数的值设置相应变量的值

以下是示例:

variables:
   ${{ if and(parameters.enableDebugErrorMessages, 'true', ne(parameters.enableAnalytics, 'true')) }}: 
     DefineConstants: ENABLE_DEBUG_MESSAGES 
   ${{ if and(parameters.enableDebugErrorMessages, 'true', eq(parameters.enableAnalytics, 'true')) }}: 
     DefineConstants: ENABLE_DEBUG_MESSAGES;ENABLE_ANALYTICS
   ${{ if and(parameters.enableAnalytics, 'true', ne(parameters.enableDebugErrorMessages, 'true')) }}: 
     DefineConstants: ENABLE_ANALYTICS


parameters:
- name: enableDebugErrorMessages
  displayName: 'Enable debug error messages'
  type: boolean
  default: false
- name: enableAnalytics
  displayName: 'Enable analytics'
  type: boolean
  default: true

pool:
  vmImage: ubuntu-latest


steps:
...
在MSbuild参数中,可以使用以下格式:
/p:DefineConstants=“$(DefineConstants)”

选择EnableDebuggerMessages->$(DefineConstants)= 启用调试消息

选择enableAnalytics->$(定义常量)=启用分析

同时选择->$(定义常量)= 启用调试消息;启用\u分析

可以使用根据布尔参数的值设置相应变量的值

以下是示例:

variables:
   ${{ if and(parameters.enableDebugErrorMessages, 'true', ne(parameters.enableAnalytics, 'true')) }}: 
     DefineConstants: ENABLE_DEBUG_MESSAGES 
   ${{ if and(parameters.enableDebugErrorMessages, 'true', eq(parameters.enableAnalytics, 'true')) }}: 
     DefineConstants: ENABLE_DEBUG_MESSAGES;ENABLE_ANALYTICS
   ${{ if and(parameters.enableAnalytics, 'true', ne(parameters.enableDebugErrorMessages, 'true')) }}: 
     DefineConstants: ENABLE_ANALYTICS


parameters:
- name: enableDebugErrorMessages
  displayName: 'Enable debug error messages'
  type: boolean
  default: false
- name: enableAnalytics
  displayName: 'Enable analytics'
  type: boolean
  default: true

pool:
  vmImage: ubuntu-latest


steps:
...
在MSbuild参数中,可以使用以下格式:
/p:DefineConstants=“$(DefineConstants)”

选择EnableDebuggerMessages->$(DefineConstants)= 启用调试消息

选择enableAnalytics->$(定义常量)=启用分析

同时选择->$(定义常量)= 启用调试消息;启用\u分析


想象一下,当我再添加几个参数时,需要添加多少组合。是的。你是对的。但是布尔参数不能设置字符串值,需要通过参数判断设置相应的值。据我所知,没有比这更简单的方法了。ThanksKevin,我认为这是一个完美的用例,可以作为支持数组变量的参数带回开发团队。然后,脚本可以使用
p.key
上的
${{{each p in parameters}}:
p.key上的
${{if}
来确定它是否是“组”的一部分,然后将值发送到单个数组变量中(可以是
${join}})ed
在脚本的后面!想象一下,当我再添加几个参数时,我需要添加多少组合。是的。你是对的。但是布尔参数不能设置字符串值,所以你需要通过参数判断来设置相应的值。据我所知,没有更简单的方法。谢谢Skevin,我认为这是一个完美的用法然后,脚本可以使用
p.key
上的
${{{p in parameters}}:
p.key
上的
${{if}}
来确定它是否是“组”的一部分,然后将值发送到单个数组变量中(可以是
${join}}ed
在脚本的后面!感谢使用pwsh的提示。通过使用
echo“##vso[task.setvariable=defineConstants]$constantList”设置最后一个变量,我稍微改进了您的示例
将其用于msbuild。这似乎是一个非常合理的增强-您还可以使用复选框来驱动不同的选项标志(比如它不仅仅是
定义常量
)通过使用多个前缀。这实际上是一个很有趣的工作,因为它正好碰到了Azure管道中不支持数组变量类型的已知限制(我想将参数枚举到一个数组变量中并加入它),并且
${{each}
不能在字符串中使用(我想将参数直接枚举到选项字符串中)这就迫使我考虑一个创造性的工作。对所有其他的挑战,Azure的YAML语法在数组方面也有各种各样的问题。感谢使用PWSH的提示。我已经通过将最后的变量设置为“代码>回声”,改进了你的示例。$constantList“
将其用于msbuild。这似乎是一个非常合理的增强-您还可以使用复选框驱动不同的选项标志(比如它不仅仅是
定义常量
)通过使用多个前缀。这实际上是一个很有趣的工作,因为它正好碰到了Azure管道中不支持数组变量类型的已知限制(我想将参数枚举到一个数组变量中并加入它),并且
${{each}
不能在字符串中使用(我想直接将参数枚举到选项字符串中)。这迫使我考虑一个创造性的工作。对所有其他挑战,Azure的YAML语法在数组方面也有各种各样的问题。