简体   繁体   中英

How do I add a condition to overrideParameters in Azure pipeline task AzureResourceGroupDeployment

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM