简体   繁体   English

如何在 Azure 管道任务 AzureResourceGroupDeployment 中为 overrideParameters 添加条件

[英]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 管道任务,如下所示:

# 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.此任务将部署一些具有两个覆盖参数的 ARM 模板。

My question is, is it possible to add a condition inside the "overrideParameters" to avoid passing "parameter2" depending on the situation?我的问题是,是否可以根据情况在“overrideParameters”中添加条件以避免传递“parameter2”? (Assuming of course that those parameters are optional in the ARM) (当然假设这些参数在 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:我尝试过类似的方法,但我无法使其工作,有一些不太优雅的方法,比如创建条件任务,甚至添加一个控制 overrideParameters 的条件,如下所示:

# 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.在此上下文中不允许使用指令“if”。 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).我冒昧地为您创建了一个更新的 YAML 脚本,其中包含我原来的 if 逻辑(当然可以根据您的需要进行更改)。


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资料来源: 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资料来源: https ://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#recursive-expansion

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何配置基于条件的管道 | Azure管道 - How to configure condition based pipeline | Azure Pipeline 如何使用 Python 脚本退出代码作为 Azure 管道中以下任务的条件? - How to use a Python script exit code as a condition for the following task in Azure Pipeline? Azure 管道 yml - 更改任务中的值并根据 if else 条件执行 - Azure pipeline yml - Change value in task and execute based on if else condition 当条件符合 azure devops 管道的 ansible 任务中的主机名时 - when condition as per hostname in ansible task of azure devops pipeline 如何在连接字符串 Azure 发布管道中使用机密 - 使用 Azure 应用服务设置任务 - How you do use Secrets in a Connection String Azure Release Pipeline - Using Azure App Service Settings Task 如何强制执行 Azure 管道构建任务? - How to make a Azure Pipeline Build task mandatory? 在 Azure Devops 中,如何在运行 shell 脚本的“Azure CLI task v.2”中使用管道变量? - In Azure Devops, how can i use pipeline variables in a “Azure CLI task v.2” running shell script? 如何在 Azure PowerShell 的 Azure 管道中分配程序集名称? - How do I assign an assembly name in an Azure Pipeline with Azure PowerShell? 如何在 Azure Devops 管道中的 YAML 中设置管道“任务版本” - How to set Pipeline 'Task version' in YAML in Azure Devops pipeline 如何将 Azure Devops 管道设置为 GitLab 存储库? - How do I setup an Azure Devops pipeline to a GitLab repo?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM