32

我的 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。

如何获取状态信息?

4

4 回答 4

31

更新:默认情况下,steps上下文现在包含有关每个步骤执行的详细信息。使用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

于 2019-09-19T03:43:03.370 回答
5

您可以使用setps.STEPNAME.outcome 属性获取状态,该属性与对 success() 或 failure() 的检查相关联

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
于 2020-12-02T17:34:07.320 回答
1

我使用的有点类似于前面建议的@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
于 2020-10-12T18:31:18.350 回答
1
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'
于 2020-12-15T01:54:36.943 回答