0

As a AzureDevops user I want to be able to manually pass different MSBuild constants (via DefineConstants) by turning checkboxes on and off on the "Run Pipeline" UI.
Example:

pipeline-settings

I achieve that by defining two boolean parameters for the current pipeline:

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

Now I want to generate a string for MSBuild containing enabled constants. Examples:

/p:DefineConstants="ENABLE_ANALYTICS"
/p:DefineConstants="ENABLE_DEBUG_MESSAGES;ENABLE_ANALYTICS"

so I can pass them for MSBuild this way:

- task: XamariniOS@2
  inputs:
    args: '<HERE>'

I've tried all possible combination of expressions from the documentation I could think of except I want to do something that is not supported by expressions.

2 Answers 2

1

UPDATED: I took (another) crack at it on a trivial pipeline and came up with this:

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'

When I have Azure Pipelines expand that file, I get:

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

Is that closer to what you want? You can have checkboxes with different constant names, descriptions, and default values.

3
  • 1
    Thank for the hint of using pwsh for this. I've improved your example a little bit by setting the final variable with echo "##vso[task.setvariable variable=defineConstants]$constantList" to use it for msbuild. Feb 19, 2021 at 8:25
  • That seems like a perfectly reasonable enhancement - you could also have checkboxes driving different option flags (say it's not just DefineConstants) by having multiple prefixes. This was actually a fun one to work on, because it bumped right up against known limitations in Azure Pipelines where array variable types are not supported (I wanted to enumerate the parameters into a single array variable and join it), and where ${{ each }} cannot be used within a string (I wanted to enumerate the parameters directly into the option string). This forced me to consider a creative workaround. Feb 19, 2021 at 13:08
  • Right. On top for all other challenges Azure's yaml syntax has it also has all sort of problems when it comes to arrays. Feb 19, 2021 at 15:08
1

You could use If expression to set the value of the corresponding variable based on the value of the boolean parameter.

Here is the sample:

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

In MSbuild argument, you could use the following format: /p:DefineConstants= "$(DefineConstants)".

select enableDebugErrorMessages -> $(DefineConstants) = ENABLE_DEBUG_MESSAGES

select enableAnalytics -> $(DefineConstants) = ENABLE_ANALYTICS

select both -> $(DefineConstants) = ENABLE_DEBUG_MESSAGES;ENABLE_ANALYTICS

3
  • Imagine how many combinations I'll have to add when I add a couple of more parameters. Feb 19, 2021 at 7:38
  • Yes. You are right. But the boolean parameter can't set the string value, so you need to set the corresponding value through parameter judgment. As far as I know, there is no easier method. Thanks Feb 19, 2021 at 7:52
  • Kevin, I think this is a perfect use case to take back to the development team as an argument to support array variables. Then, the script could use ${{ each p in parameters }}: and then an ${{ if }} on p.key to determine if it is part of a "group", and then emit values into a single array variable (which could be ${{ join }}ed later in the script! Feb 19, 2021 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.