Github操作:检查步骤状态

时间:2019-09-09 08:44:23

标签: github-actions

我的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 个答案:

答案 0 :(得分:2)

查看有关步骤上下文的文档,它看起来不包含除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

答案 1 :(得分:0)

我使用的与之前建议的@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

答案 2 :(得分:0)

您可以使用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

答案 3 :(得分:0)

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'