【问题标题】:Github Actions: check steps statusGithub Actions:检查步骤状态
【发布时间】:2020-01-11 00:23:48
【问题描述】:

我的 CI 工作中有一些可能引发错误的步骤。我不想在出现错误的每个步骤上重新启动工作流程,并且希望转到检查这些步骤的最后一步并将此作业作为失败完成。 但我无法在之前的步骤中获取状态信息。

name: CI
on: [pull_request]
jobs:
  myjob:
    runs-on: ubuntu-latest
    steps:
      - name: Step 1
        id: hello
        run: <any> 
        continue-on-error: true
      - name: Step 2
        id: world
        run: <any> 
        continue-on-error: true
      - name: Check on failures
        if: job.steps.hello.status == failure() || job.steps.world.status == failure()
        run: exit 1

当我在 "if" 或 "run" 中使用下一个构造时,会得到:steps -> {}, job.steps -> null。

如何获取状态信息?

【问题讨论】:

  • 我不明白这句话:“当我在“if”或“run”中使用下一个结构时......”。你能改写一下吗?
  • 例如:- name: Check on failures run: ${{ toJson(steps) }} ${{ toJson(job.steps) }} 那么我会得到{} null

标签: github-actions


【解决方案1】:

更新steps context 现在包含有关默认执行每个步骤的详细信息。使用每个步骤的outcome 属性,我们可以检查其执行结果。

name: CI
on: [pull_request]
jobs:
  myjob:
    runs-on: ubuntu-latest
    steps:
      - name: Step 1
        id: hello
        run: <any> 
        continue-on-error: true
      - name: Step 2
        id: world
        run: <any> 
        continue-on-error: true
      - name: Check on failures
        if: steps.hello.outcome != 'success' || steps.world.outcome != 'success'
        run: exit 1

原答案 查看步骤上下文的文档,除了outputs 之外,它似乎不包含有关步骤的任何信息。这些必须由步骤明确定义。这就是为什么步骤上下文为空{}

https://help.github.com/en/articles/contexts-and-expression-syntax-for-github-actions#steps-context

不幸的是,据我所知,可以访问的步骤没有默认状态。该解决方案涉及手动定义每个步骤的状态输出变量。

name: CI
on: [pull_request]
jobs:
  myjob:
    runs-on: ubuntu-latest
    steps:
      - name: Step 1
        id: hello
        run: echo ::set-output name=status::failure
        continue-on-error: true
      - name: Step 2
        id: world
        run: echo ::set-output name=status::success
        continue-on-error: true
      - name: Dump steps context
        env:
          STEPS_CONTEXT: ${{ toJson(steps) }}
        run: echo "$STEPS_CONTEXT"
      - name: Check on failures
        if: steps.hello.outputs.status == 'failure' || steps.world.outputs.status == 'failure'
        run: exit 1

这将创建以下上下文输出并且作业失败。

{
  "hello": {
    "outputs": {
      "status": "failure"
    }
  },
  "world": {
     "outputs": {
      "status": "success"
    }
  }
}

https://help.github.com/en/articles/metadata-syntax-for-github-actions#outputs https://help.github.com/en/articles/development-tools-for-github-actions#set-an-output-parameter-set-output

【讨论】:

  • 它对我不起作用。 “检查故障”没有启动并进入下一步:完成这项工作。当我禁用“if”时,此步骤运行,但变量未定义:- name: Check on failures run: | echo hello - ${{ job.steps.hello.status }}! exit 1 => hello -!, Process completed with exit code 1
  • @Maxim 你是对的,对不起。在对此进行了更多测试之后,现在看起来不可能。我会更新这个答案。
  • 谢谢你,@peterevans!我对其进行了简化,并在“运行”结束时只设置了“成功”。所以对我来说足够了:if: steps.hello.outputs.status != 'success' || steps.world.outputs.status != 'success'
  • 2020 年更新:正如其他答案所述,不再需要(如果有的话,IDK)显式设置输出。您只需要:1. 在您的步骤上设置id,以及 2. 致电steps.&lt;step id&gt;.outcome。参考:docs.github.com/en/actions/reference/…@peterevans,请更新答案以添加此类信息。
  • @fcole90 谢谢。更新了答案。原始答案是在outcomeconclusion 可用之前编写的。
【解决方案2】:

我使用的有点类似于前面建议的@peterevans,但利用 shell 退出命令和set +e 标志:

name: CI
on: [pull_request]
jobs:
  myjob:
    runs-on: ubuntu-latest
    steps:
      - name: Step 1
        id: hello
        run: |
          set +e
          ./my-script.sh
          echo ::set-output name=exit_status::failure
      - name: Step 2
        id: world
        run:
          set +e
          python3 ./my-script.py
          echo ::set-output name=exit_status::$?
      - name: Check on failures
        if: steps.hello.outputs.exit_status != 0 | steps.world.outputs.exit_status != 0
        run: exit 1

【讨论】:

    【解决方案3】:

    您可以使用setps.STEPNAME.outcome property 获取状态,与成功() 或失败() 的检查相关联

    name: CI
    on: [pull_request]
    jobs:
      myjob:
        runs-on: ubuntu-latest
        steps:
          - name: Step 1
            id: hello
            run: <any> 
            continue-on-error: true
          - name: Step 2
            id: world
            run: <any> 
            continue-on-error: true
          - name: Check on failures
            if: (${{ success() }} || ${{ failure() }}) && (${{ steps.hello.outcome }} == 'failure' || ${{ steps.world.outcome }} == 'failure')
            run: exit 1
    

    【讨论】:

      【解决方案4】:
      jobs:
        build:
          name: Build
          env:
            DOCKER_PASS: ${{ secrets.DOCKER_PASS }} 
          runs-on: ubuntu-latest
          steps:
      
          - name: script
            id: test1
            continue-on-error: true
            run: |
              ls -l sadasd
              
          - name: script
            id: sync
            continue-on-error: true
            run: |
              ls -l
          - name: Dump steps context
            env:
              STEPS_CONTEXT: ${{ toJson(steps) }}
            run: echo "$STEPS_CONTEXT"
      

      输出

      Run echo "$STEPS_CONTEXT"
      {
        "test1": {
          "outputs": {},
          "outcome": "failure",
          "conclusion": "success"
        },
        "sync": {
          "outputs": {},
          "outcome": "success",
          "conclusion": "success"
        }
      }
      

      所以可以使用步骤:

          - name: sync run
            id: sync
            continue-on-error: true
            run: |
              .....
          - name: after_success
            run: |
              ...
            if: steps.sync.outcome  == 'success'
          - name: after_failure
            run: |
              ...
            if: steps.sync.outcome != 'success'
      

      【讨论】:

        猜你喜欢
        • 2022-01-13
        • 2020-05-12
        • 2020-11-28
        • 2022-12-13
        • 2021-05-02
        • 2023-03-31
        • 1970-01-01
        • 1970-01-01
        • 2021-12-29
        相关资源
        最近更新 更多