简体   繁体   English

Github 操作“always()”条件未按预期工作?

[英]Github actions "always()" condition not working as expected?

I am interested in why this always() condition is not behaving as expected in a Github workflow.我感兴趣的是为什么这个 always() 条件在 Github 工作流中没有按预期运行。

I have a core pipeline, which works just fine.我有一个核心管道,它工作得很好。 If the secondary workflow fails, the tertiary still runs but only if boolean output from secondary workflow == true.如果二级工作流失败,三级工作流仍然运行,但前提是二级工作流中的 boolean output == true。

Within the secondary pipeline, the output step is supposed to run only if a boolean input is set to true.在辅助管道中,output 步骤应该仅在 boolean 输入设置为 true 时运行。 Although I have used the exact same syntax, it only seems to run if the condition is always() rather than always() &&.尽管我使用了完全相同的语法,但它似乎只在条件为 always() 而不是 always() && 时运行。

This is the core pipeline (which works):这是核心管道(有效):

name: Core Pipeline
on: 
  workflow_dispatch:
    inputs: 
      boolean: 
        description: 'boolean'
        required: false
        default: true
        type: boolean 

jobs:
  secondary-workflow:
    uses: ./.github/workflows/secondary-workflow.yaml
    with:
      boolean: true 
  tertiary-workflow:
    needs: secondary-workflow
    uses: ./.github/workflows/tertiary-workflow.yaml
    if: always() && needs.secondary-workflow.outputs.boolean == 'true'

This is secondary-workflow (doesn't work as intended):这是辅助工作流程(未按预期工作):

on: 
  workflow_call:
    inputs: 
      boolean: 
        description: 'Boolean'
        required: false
        default: true
        type: boolean 
    outputs: 
      boolean: 
        description: "Test Output"
        value: ${{ jobs.job-4.outputs.boolean }}

jobs:
  job-1:
    runs-on: ubuntu-latest
    steps:
      - name: job 1 
        run: echo 'This job runs'
  job-2:
    runs-on: ubuntu-latest
    needs: job-1
    steps:
      - name: job 2
        run: echo 'This job runs'
  job-3:
    runs-on: ubuntu-latest
    needs: [job-1, job-2]
    steps:
      - name: Exit 
        run: echo "This job fails"; exit 1 
  job-4:
    runs-on: ubuntu-latest
    needs: [job-1, job-2, job-3]
    if: always() && inputs.boolean == 'true' (ADDING THE && CAUSES THE JOB TO BE SKIPPED) 
    steps:
      - id: setOutput
        run: echo "boolean=true" >> $GITHUB_OUTPUT; echo "OUTPUT_SET"
    outputs:
      boolean: ${{ steps.setOutput.outputs.boolean }}

My question is, why does adding a second condition in addition to always() work for the core pipeline but not for the secondary pipeline?我的问题是,为什么在 always() 之外添加第二个条件对核心管道有效,但对辅助管道无效?

Thanks!谢谢!

When you compare当你比较

if: always() && inputs.boolean == 'true'

you're comparing a boolean to a string.您正在将 boolean 与字符串进行比较。 On type mismatch, actions coerce the operands to numbers ;在类型不匹配时, 操作将操作数强制转换为数字 true becomes 1 , and 'true' (the string) becomes NaN , and 1 == NaN is false. true变为1 ,并且'true' (字符串)变为NaN ,并且1 == NaN为 false。 To fix, you can compare to a boolean literal instead of a string:要修复,您可以与 boolean 文字而不是字符串进行比较:

if: always() && inputs.boolean == true

or you should be able to just check the boolean on its own:或者您应该能够自己检查 boolean:

if: always() && inputs.boolean

As for why the seemingly identical condition works in one place and not in the other, my guess is that the input parameter in the reusable workflow is typed as a boolean, whereas needs.secondary-workflow.outputs.boolean is a string, so comparison to 'true' works.至于为什么看似相同的条件在一个地方起作用而在另一个地方不起作用,我的猜测是可重用工作流中的输入参数类型为 boolean,而needs.secondary-workflow.outputs.boolean是一个字符串,所以比较到'true'作品。

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

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