Azure Pipeline YAML dependsOn already defined

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

Azure Pipeline YAML dependsOn already defined

问题

我有一个管道,用于清理、将虚拟机转换为镜像,并删除所有资源。我想使用相同的管道删除虚拟机,而不捕获图像。我可以将所有阶段设为有条件执行,但最终没有没有条件的阶段。我不断看到这个问题的被接受的解决方案是将dependsOn条件设置如下所示。deleteVM参数是一个布尔值。

Parameters:
- name: deleteVM
  displayName: "just delete it"
  type: boolean
  default: false
    
- stages:
  - stage: firstStage
    dependsOn: []
    condition: eq('${{ parameters.deleteVM }}', 'false')
    jobs:
    - job: deallocateVM

  - stage: secondStage
    ${{ if eq(parameters.deleteVM, 'false') }}:
      dependsOn: firstStage
    ${{ if eq(parameters.deleteVM, 'true') }}:
      dependsOn: []

问题是,当我这样做,并尝试运行第二个条件为true的管道时,它会在解析管道时出错:“YAML: 'dependsOn'已经定义。”

P.S.我看过以下内容,它们都暗示我正在采用的方式是“正确”的方式:

根据下面的评论和答案,我尝试了以下:

Parameters:
- name: deleteVM
  displayName: "just delete it"
  type: boolean
  default: false
    
- stages:
  - stage: firstStage
    dependsOn: []
    condition: eq('${{ parameters.deleteVM }}', 'false')
    jobs:
    - job: deallocateVM

  - stage: secondStage
    ${{ if eq(parameters.deleteVM, 'false') }}:
      dependsOn: firstStage
    ${{ else }}:
      dependsOn: []

结果是“Unrecognized value: 'else'. Location at position 1 within expression: else.”

我尝试使用不等于:

Parameters:
- name: deleteVM
  displayName: "just delete it"
  type: boolean
  default: false
    
- stages:
  - stage: firstStage
    dependsOn: []
    condition: eq('${{ parameters.deleteVM }}', 'false')
    jobs:
    - job: deallocateVM

  - stage: secondStage
    ${{ if eq(parameters.deleteVM, 'false') }}:
      dependsOn: firstStage
    ${{ if ne(parameters.deleteVM, 'false') }}:
      dependsOn: []

结果是无论我放true还是false,或者我放在第一个位置的dependsOn是什么,当deleteVM为true时,它跳过这个阶段(我认为它正在忽略这个阶段)。

我还尝试创建一个变量,对此有一些幸运:

Parameters:
- name: deleteVM
  displayName: "just delete it"
  type: boolean
  default: false

variables:
- ${{ if eq(parameters.deleteVM, 'false') }}:
  - name: dependency
    value: anActualStage
- ${{ if eq(parameters.deleteVM, 'false') }}:
  - name: dependency
    value: '[]'

- stages:
  - stage: firstStage
    dependsOn: []
    condition: eq('${{ parameters.deleteVM }}', 'false')
    jobs:
    - job: deallocateVM

  - stage:
    - stage: secondStage
      dependsOn: ${{ variables.dependency }}

结果是这将始终执行我不想要的操作。无论我将默认值设置为true还是false,当我将deleteVM设置为true时,它显示“firstStage”作为依赖项,如果我将deleteVM设置为false,则不显示任何依赖项。

我明白这在编译时被解释,这在某种程度上是我想要的行为,因为切换deleteVM复选框会在具有依赖项或没有依赖项之间切换,但它总是选择错误。如果我继续让它执行我不想要的相反操作,我可以看到secondStage在没有任何依赖项的情况下运行(但我必须做与我想要的相反选择)。我宁愿不更新措辞以只说与背后实际发生的事情相反的话,而且我对这一点不够信任,以便合并我为人们准备的内容。如某人在评论中提到的,我正在使用具有非常陈旧软件和硬件限制的Azure Stack Hub...

英文:

I have a pipeline that will clean, convert a VM to an image, and delete all of the resources. I would like to use that same pipeline to delete VMs without capturing an image. I can make all of the stages conditional, but when I end up with no stages that have no conditions. I keep seeing that the accepted solution to this problem is to make the dependsOn conditional as shown below. the deleteVM parameter is a boolean.

Parameters:
- name: deleteVM
  displayName: "just delete it"
  type: boolean
  default: false
    
- stages:
  - stage: firstStage
    dependsOn: []
    condition: eq('${{ parameters.deleteVM }}', 'false')
    jobs:
    - job: deallocateVM

  - stage: secondStage
    ${{ if eq(parameters.deleteVM, 'false') }}:
      dependsOn: firstStage
    ${{ if eq(parameters.deleteVM, 'true') }}:
      dependsOn: []

The issue is that when I do this, and try to run the pipeline with the second condition being true, it will error when parsing the pipeline: "YAML: 'dependsOn' is already defined."

P.S. I've looked at the following and they imply that the way I'm doing it is the "right" way:

Based on the comments and answers below, I have tried:

Parameters:
- name: deleteVM
  displayName: "just delete it"
  type: boolean
  default: false
    
- stages:
  - stage: firstStage
    dependsOn: []
    condition: eq('${{ parameters.deleteVM }}', 'false')
    jobs:
    - job: deallocateVM

  - stage: secondStage
    ${{ if eq(parameters.deleteVM, 'false') }}:
      dependsOn: firstStage
    ${{ else }}:
      dependsOn: []

Result is "Unrecognized value: 'else'. Location at position 1 within expression: else."

I have tried to use NOT EQUALS:

Parameters:
- name: deleteVM
  displayName: "just delete it"
  type: boolean
  default: false
    
- stages:
  - stage: firstStage
    dependsOn: []
    condition: eq('${{ parameters.deleteVM }}', 'false')
    jobs:
    - job: deallocateVM

  - stage: secondStage
    ${{ if eq(parameters.deleteVM, 'false') }}:
      dependsOn: firstStage
    ${{ if ne(parameters.deleteVM, 'false') }}:
      dependsOn: []

Result is that no matter if I put true or false, or what I put in the dependsOn in the first position, it skips this stage when deleteVM is true (I think it is ignoring this stage).

I have also tried creating a variable which I had some luck with:

Parameters:
- name: deleteVM
  displayName: "just delete it"
  type: boolean
  default: false

variables:
- ${{ if eq(parameters.deleteVM, 'false') }}:
  - name: dependency
    value: anActualStage
- ${{ if eq(parameters.deleteVM, 'false') }}:
  - name: dependency
    value: '[]'

- stages:
  - stage: firstStage
    dependsOn: []
    condition: eq('${{ parameters.deleteVM }}', 'false')
    jobs:
    - job: deallocateVM

  - stage:
    - stage: secondStage
      dependsOn: ${{ variables.dependency }}

Result is that this will always do the opposite of what I want. no matter if I set the default value to true or false, when I set deleteVM:true, it shows "firstStage" as a dependency, and if I set deleteVM:false, it shows NO DEPENDENCY.

I get that this is interpreted at compile time and this is somewhat the behavior I'm looking for because toggling the deleteVM checkbox switches between having a dependency or not, however, it is always the wrong selection. If I continue with it doing the opposite thing I want, I can see that the secondstage runs without any dependencies (but I have to make the opposite selection of what I want). I would rather not update the verbiage to just say the opposite of what is really going on in the background and I have little confidence in this being consistent across different runs so I don't trust this enough to merge what I have for people to use.

As someone mentioned in the comment, I am using Azure Stack Hub which has very dated software and hardware limitations...

答案1

得分: 3

  • stages:
    • stage: aStage
      ${{ if eq(parameters.deleteVM, 'false') }}:
      dependsOn: anActualStage
      ${{ else }}:
      dependsOn: []

    • stage: aStage
      ${{ if eq(parameters.deleteVM, 'false') }}:
      dependsOn: anActualStage

英文:

Instead of 2 if statements, use if/else. Or simply leave out the else altogether, since you're setting the depends on to an empty list.

- stages:
  - stage: aStage
    ${{ if eq(parameters.deleteVM, 'false') }}:
      dependsOn: anActualStage
    ${{ else }}:
      dependsOn: []

  - stage: aStage
    ${{ if eq(parameters.deleteVM, 'false') }}:
      dependsOn: anActualStage

答案2

得分: 1

@4c74356b41的关于使用if-else的回答可能是你最好的选择。

YAML的工作方式是,如果你的if语句解析为true,那么在你的示例中的dependsOn才会真正添加到"compiled" YAML文件中。因此,你的变量可能有问题,因为看起来你的两个逻辑语句都解析为true。我不能看出为什么,因为根据你的示例,这似乎是不可能的,但这是我能想到你看到的错误的唯一方式。

英文:

@4c74356b41´s answer about using if-else is probably your best bet.

The way yaml works is that the dependsOn in your example is only really added to the "compiled" yaml file if your if statement resolves to true. Thus, there must be something wrong with your variables, as it looks like both of your logical statements resolves to true. I cannot really see why as it looks impossible by your example, but that is the only way I can see you getting the error you are seeing.

huangapple
  • 本文由 发表于 2023年2月10日 10:13:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75406309.html
匿名

发表评论

匿名网友

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

确定