在github工作流程中运行超时命令

Mef*_*ico 9 github-actions

我有一个类似于下面代码的 GitHub 操作。我有一个文件本来打算永远运行,但在需要时会被用户中断。我尝试过使用timeout,但它不起作用,并且给出了一些奇怪的消息。

对此的一个小警告是,如果该过程超时,我希望这不会引发错误,以便操作继续并报告成功。但是,如果 python 脚本本身失败,我希望报告这一点,因为运行一段时间进行调试是在操作中运行它的重点。

name: Run file with interrupt then save results
on: 
  push:
    branches: 
      - master

jobs:
  build:
    strategy:
      matrix:
        python-version: [3.7]
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v1
        with:
          python-version: ${{ matrix.python-version }}
      - name: Install dependencies for ex 06
        run: |
          python -m pip install --upgrade pip
          cd /home/runner/work/repo_name/repo_name
          pip install -r requirements.txt
      - name: Run file with timeout
        run: |
          cd /home/runner/work/repo_name/repo_name/
          echo "hi1"
          timeout 5 sleep 10
          echo "hi2"
          python RunsForever.py
          echo "hi3"
      - name: Upload results
        uses: actions/upload-artifact@v2
        with:
          name: result
          path: /home/runner/work/repo_name/repo_name/output/
Run Code Online (Sandbox Code Playgroud)

我怎样才能让超时正常工作?当前的错误消息是:

Run cd /home/runner/work/repo_name/repo_name/
  cd /home/runner/work/repo_name/repo_name/
  echo "hi1"
  timeout 5 sleep 10
  echo "hi2"
  python RunsForever.py
  echo "hi3"
  shell: /bin/bash -e {0}
  env:
    pythonLocation: /opt/hostedtoolcache/Python/3.7.8/x64
hi1
##[error]Process completed with exit code 124.
Run Code Online (Sandbox Code Playgroud)

我不明白可能是什么问题。我预计问题与隐藏输出的超时函数有关RunsForever.py,但实际上timeout它本身似乎不起作用。我尝试过安装apt-get,也无济于事。

是否有一些调试可以使其运行?是否有其他方法可以终止进程,以便我在预定时间后有效地中断它?

Edw*_*ero 9

更新

根据评论,我更新了响应,以提供添加超时的正确方法,并且在超时时仍然成功,同时还支持正常失败。

基本上,我们检查错误代码 124(超时)和 0(成功),并确保我们不会因这些代码而退出。但是如果我们收到任何其他信息,那么我们会退出以匹配 github 操作在失败时通常执行的操作

超时时演示成功

演示因错误而失败

代码片段

    - name: no timeout
      run: timeout 10 python ./RunsForever.py || code=$?; if [[ $code -ne 124 && $code -ne 0 ]]; then exit $code; fi
    
    - name: timeout # We add or so that return code is not none-zero which causes pipeline to fail
      run: timeout 1 python ./RunsForever.py || code=$?; if [[ $code -ne 124 && $code -ne 0 ]]; then exit $code; fi
Run Code Online (Sandbox Code Playgroud)

之前的回应

只处理超时,但不继续超时运行而不报告失败。但OP希望它在超时时也能成功,因此提供了上面的更新。

您可以使用超时分钟数来做到这一点。使用您的代码作为示例

    - name: Run file with timeout
      timeout-minutes: 1 # Times out after 1 minute
      run: |
         cd /home/runner/work/repo_name/repo_name/
         echo "hi2"
         python RunsForever.py
         echo "hi3"
Run Code Online (Sandbox Code Playgroud)

要向作业添加超时,请使用以下资源:https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idtimeout-minutes

要为单个步骤添加超时,请参考以下资源:https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstepstimeout-minutes