Github Actions条件Job未执行排查:依赖与布尔输入异常
GitHub Actions工作流问题排查:Job2/Job3始终不执行的解决思路
问题场景
我有一个通过workflow_dispatch触发的GitHub Actions工作流,包含3个Job:
- Job1为基础依赖Job,Job2和Job3需在Job1执行成功后运行
- Job2仅在复选框输入
input1设为true时触发 - Job3仅在复选框输入
input2设为true时触发
但当前配置下,即便input1/input2设为true,Job2和Job3始终不执行。已验证Job1中输入值传递正确,当前配置代码如下:
on: workflow_dispatch: inputs: input1: type: boolean description: input 1 desc default: true input2: type: boolean description: input 2 desc default: false input3: type: string description: input 3 desc jobs: job1: runs-on: ubuntu-latest outputs: matrix: ${{ steps.set-company-short-ids.outputs.IDS_ARRAY }} steps: - name: Create array of ids id: set-ids run: | echo "IDS_ARRAY=$(echo '"${{ github.event.inputs.input3 }}"' | tr -d ' ' | jq -c 'split(",")')" >> $GITHUB_OUTPUT job2: if: ${{ github.event.inputs.input1 == true }} needs: job1 strategy: fail-fast: false matrix: target: ${{ fromJSON(needs.job1.outputs.matrix) }} uses: ./.github/workflows/reusableWorkflow.yml job3: if: ${{ github.event.inputs.input2 == true }} needs: job1 strategy: fail-fast: false matrix: target: ["once"] uses: ./.github/workflows/reusableWorkflow.yml
核心原因
GitHub Actions中,workflow_dispatch的布尔型输入值在表达式中是以字符串类型存储的,而非布尔类型。也就是说,当你勾选复选框时,github.event.inputs.input1的实际值是字符串"true",而非布尔值true。直接用== true做比较永远不会成立,导致Job2/Job3的if条件始终不满足。
解决思路
方案1:直接和字符串值比较
修改Job2和Job3的if条件,将比较对象改为字符串'true':
if: ${{ github.event.inputs.input1 == 'true' }}
方案2:用fromJSON()转换为布尔值
通过fromJSON()将字符串类型的输入值转为布尔值,再做判断:
if: ${{ fromJSON(github.event.inputs.input1) }}
修正后的完整配置示例
on: workflow_dispatch: inputs: input1: type: boolean description: input 1 desc default: true input2: type: boolean description: input 2 desc default: false input3: type: string description: input 3 desc jobs: job1: runs-on: ubuntu-latest outputs: matrix: ${{ steps.set-ids.outputs.IDS_ARRAY }} steps: - name: Create array of ids id: set-ids run: | echo "IDS_ARRAY=$(echo '"${{ github.event.inputs.input3 }}"' | tr -d ' ' | jq -c 'split(",")')" >> $GITHUB_OUTPUT job2: if: ${{ github.event.inputs.input1 == 'true' }} needs: job1 strategy: fail-fast: false matrix: target: ${{ fromJSON(needs.job1.outputs.matrix) }} uses: ./.github/workflows/reusableWorkflow.yml job3: if: ${{ github.event.inputs.input2 == 'true' }} needs: job1 strategy: fail-fast: false matrix: target: ["once"] uses: ./.github/workflows/reusableWorkflow.yml
额外检查点
- 确认Job1确实执行成功(无失败、无跳过),因为
needs: job1要求Job1必须成功才会触发后续Job - 检查Job1输出的
matrix是否为合法JSON格式,避免Job2因矩阵解析失败被自动跳过
内容的提问来源于stack exchange,提问作者Shamus Harkin
相关产品推荐
相关产品推荐

