Azure DevOps 参数条件

huangapple go评论57阅读模式
英文:

Azure Devops Parameter condition

问题

parameters:
  - name: environment
    type: string
    default: dev

  - name: region
    type: string
    default: ${{ if or(eq(parameters.environment, 'dev'), eq(parameters.environment, 'tst')) }}central${{ else }}${{ if eq(parameters.environment, 'both') }}central, east${{ else }}east${{ endif }}${{ endif }}

当参数environment为dev或test时,参数region将为central;对于其他环境,参数region将为central, east, both。

但与此同时,我收到错误消息,指令不支持嵌入式表达式。

英文:
parameters:
- name: environment
  type: string
  default: dev

- name: region
  type: string
  default: ${{ if or(eq(parameters.environment, 'dev'), eq(parameters.environment, 'tst')) }}central${{ else }}${{ if eq(parameters.environment, 'both') }}central, east${{ else }}east${{ endif }}${{ endif }}

I am putting condition as if the parameter environment is dev or test then the parameter region will be central and for other environments, the region will be central, east, both

But at the same time I am getting error as directive are not supported for the expression that are embedded

答案1

得分: 1

我不相信你可以在参数中运行表达式。为了实现所需的行为,我认为你更应该运行表达式以根据条件设置变量值。

所以应该是这样的:

parameters:
- name: environment
  type: string
  default: dev

variables:
  ${{ if or(eq(parameters.environment, 'dev'), eq(parameters.environment, 'tst')) }}:
    region: 'central'
  ${{ if eq(parameters.environment, 'both') }} :
    region: 'central, east'
  ${{ else }} :
    region: 'east'

我没有测试过这个方法,但这个方法应该可以工作。你可以引用你的区域,然后作为${{ variables.region }}传递给未来的输入/模板。

这里还有一个提供示例的stackoverflow帖子

英文:

I don't believe you can run an expression in a parameter. To achieve the desired behavior I think you'd rather run your expression to set a variable value based on the condition.

So it would be:

parameters:
- name: environment
  type: string
  default: dev

variables:
  ${{ if or(eq(parameters.environment, 'dev'), eq(parameters.environment, 'tst')) }}:
    region: 'central'
  ${{ if eq(parameters.environment, 'both') }} :
    region: 'central, east'
  ${{ else }} :
    region: 'east'

I have not tested this; however, this approach should work. You'd reference your region then as ${{ variables.region}} and can pass it along to future inputs/templates.

Here's a stackoverflow post as well that provides an example.

huangapple
  • 本文由 发表于 2023年6月29日 00:57:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76575287.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定