0

Let's say that we have an Azure pipeline task like so:

# Azure resource group deployment v2
# Deploy an Azure Resource Manager (ARM) template to a resource group and manage virtual machines.
- task: AzureResourceGroupDeployment@2
  inputs:
    azureSubscription: someSubscription
    action: someAction
    resourceGroupName: someResourceGroupName
    templateLocation: someTemplateLocation
    deploymentMode: someDeploymentmode 
    overrideParameters: '
     -parameter1 "value1"
     -parameter2 "value1"

This task will deploy some ARM template with two overridedParameters.

My question is, is it possible to add a condition inside the "overrideParameters" to avoid passing "parameter2" depending on the situation? (Assuming of course that those parameters are optional in the ARM)

Example of what I would like in pseudo-code:

# Azure resource group deployment v2
# Deploy an Azure Resource Manager (ARM) template to a resource group and manage virtual machines.
- task: AzureResourceGroupDeployment@2
  inputs:
    azureSubscription: someSubscription
    action: someAction
    resourceGroupName: someResourceGroupName
    templateLocation: someTemplateLocation
    deploymentMode: someDeploymentmode 
    overrideParameters: '
     -parameter1 "value1"
     if(someVariableisTrue):
      -parameter2 "value1"

I've tried similar approaches but I couldn't make it work, there are less elegant ways like creating a conditional task or even adding a condition that controls the overrideParameters like this:

# Azure resource group deployment v2
# Deploy an Azure Resource Manager (ARM) template to a resource group and manage virtual machines.
- task: AzureResourceGroupDeployment@2
  inputs:
    azureSubscription: someSubscription
    action: someAction
    resourceGroupName: someResourceGroupName
    templateLocation: someTemplateLocation
    deploymentMode: someDeploymentmode 
    ${{if someVariableisTrue}}:
    overrideParameters: '
     -parameter1 "value1"
     -parameter2 "value1"
    '
    ${{else}}:
    overrideParameters: '
     -parameter1 "value1"
    '

But I would like to avoid that if there is a better solution.

Thanks in advance!

Edit

To avoid confusion what I really wold love would be this:

# Azure resource group deployment v2
# Deploy an Azure Resource Manager (ARM) template to a resource group and manage virtual machines.
- task: AzureResourceGroupDeployment@2
  inputs:
    azureSubscription: someSubscription
    action: someAction
    resourceGroupName: someResourceGroupName
    templateLocation: someTemplateLocation
    deploymentMode: someDeploymentmode 
    overrideParameters: '
     -parameter1 "value1"
     ${{ if ne(value, '')}}:  ###Just example condition could be anything
     -parameter2 "value1"

The purpose of that is so we can achieve a code with high maintainability, so if now another person wants to add another optional parameter doesn't need to copy code and create a "monstruous" condition, that person will just need to add another if.

To clarify further, if I try something like the code above I get this;

The directive 'if' is not allowed in this context. Directives are not supported for expressions that are embedded within a string. Directives are only supported when the entire value is an expression.

jaumecen
  • 3
  • 3
  • The purpose of adding directly `if` expression into overrideParameters is good,.However, i'm afraid the task `AzureResourceGroupDeployment@2` doesn't support this way from MS doc at present. – Dou Xu-MSFT Dec 22 '22 at 10:32
  • Okey that's what I thought, thanks! – jaumecen Dec 27 '22 at 07:44

1 Answers1

1

I used conditions before in variables. I think this logic can also be used for your situation as well.

I took the liberty to create an updated YAML script for you, with my original if-logic (which of course can be altered to your needs).


variables:
  ${{ if startsWith(variables['Build.SourceBranch'], 'refs/heads/') }}:
    overrideParameter: '
     -parameter1 "value1"
     -parameter2 "value1"
    '
  ${{ if startsWith(variables['Build.SourceBranch'], 'refs/pull/') }}:
    overrideParameter: '
     -parameter1 "value1"
    '

steps:
# Azure resource group deployment v2
# Deploy an Azure Resource Manager (ARM) template to a resource group and manage virtual machines.
- task: AzureResourceGroupDeployment@2
  inputs:
    azureSubscription: someSubscription
    action: someAction
    resourceGroupName: someResourceGroupName
    templateLocation: someTemplateLocation
    deploymentMode: someDeploymentmode 
    overrideParameters: $(overrideParameter)

I'm not able to test your exact wish with my setup, but I managed to get the above approach and the approach you're looking for working in this example:

variables:
  ${{ if startsWith(variables['Build.SourceBranch'], 'refs/heads/') }}:
    overrideParameter: '
     -parameter1 "value1"
     -parameter2 "value1"
    '
  ${{ if startsWith(variables['Build.SourceBranch'], 'refs/pull/') }}:
    overrideParameter: '
     -parameter1 "value1"
    '

trigger:
- main

pool:
  vmImage: ubuntu-latest

steps:
- script: echo $(overrideParameter) #using the earlier conditional determined var

- task: PublishPipelineArtifact@1
  inputs:
    targetPath: '$(Pipeline.Workspace)'
    ${{ if eq(variables['Build.SourceBranchName'], 'main') }}:  #conditionally settin atask-input
      artifact: 'prod'
    ${{ else }}:
      artifact: 'dev'
    publishLocation: 'pipeline'

Source: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/expressions?view=azure-devops#conditionally-set-a-task-input

Edit

Updated answer after the edited question, with a variable which is expanded outside the task:

variables:
  overrideParameter: '
    -parameter1 "value1"'
  overrideParameterIncludingOptional: $(overrideParameter) '
    -parameter2 "value1"'

steps:
# Azure resource group deployment v2
# Deploy an Azure Resource Manager (ARM) template to a resource group and manage virtual machines.
- task: AzureResourceGroupDeployment@2
  inputs:
    azureSubscription: someSubscription
    action: someAction
    resourceGroupName: someResourceGroupName
    templateLocation: someTemplateLocation
    deploymentMode: someDeploymentmode 
    ${{ if eq(variables['Build.SourceBranchName'], 'main') }}:  
    overrideParameters: $(overrideParameter)
    ${{ else }}:
    overrideParameters: $(overrideParameterIncludingOptional)

Source: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#recursive-expansion

promicro
  • 1,280
  • 7
  • 14
  • That's interesting I was trying something similar, but that means that there is no way to put the condition inside de "OverrideParameter" section right? (I was hoping for something like that to exist haha) – jaumecen Dec 16 '22 at 11:28
  • Technical you should be able to do this according to the documentation: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/expressions?view=azure-devops#conditionally-set-a-task-input And with the `PublishPipelineArtifact@1` I can get it to work. Please try it in your situation. I'll update the answer with my proof of concept – promicro Dec 16 '22 at 12:28
  • Just updated my question, because I think that I know what you want me to try but I'm not sure that It matches my exact necessities (or maybe we can call them desires, because as I stated maybe this can't be done haha) I appreciate a lot your effort to help me!! – jaumecen Dec 16 '22 at 14:36
  • Interesting plot twist, but let's give it a try ;-) Updated my answer – promicro Dec 16 '22 at 17:03
  • As stated by @Dou Xu-MSFT it seems impossible to achieve what I was looking for but your approach is very interesting so I'll flag it as the best answer. Thanks! – jaumecen Dec 27 '22 at 07:46
  • I don't know if someone will face this necessity but I think it would be interesting to have a solution like the one I wanted to achieve so I opened an issue directly to microsoft just in case someone is interested. https://github.com/microsoft/azure-pipelines-tasks/issues/17985 – jaumecen Apr 13 '23 at 12:35