在github工作流程中运行带有超时的命令 [英] Run command with timeout in github workflow

查看:71
本文介绍了在github工作流程中运行带有超时的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于以下代码的GitHub操作.我有一个文件可以永久运行,但是在需要时会被用户打断.我尝试使用 timeout ,但是它不起作用,并给出一些奇怪的消息.

I have a GitHub action that resembles the code below. I have a file that is meant to run forever but get interrupted by a user when needed be. I've tried using timeout but it does not work, and gives some weird message.

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

A small caveat to this, is that if the process is timed-out, I want this not to raise an error, so that the action continues and reports success. But if the python script itself fails, I want this to be reported, as running it for a while for debugging is the point of running it within the action.

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/

如何使超时正常工作?当前错误消息是:

How can I have timeout working properly? The current error message is:

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.

我不明白这可能是问题所在.我预计该问题与超时功能隐藏 RunsForever.py 的输出有关,但实际上 timeout 本身似乎不起作用.我尝试使用 apt-get 安装,也无济于事.

I don't understand what could be the issue. I expected the problem to be related to the timeout function hiding output from RunsForever.py, but actually timeout itself seems not to work. I've tried installing with apt-get, also to no avail.

是否有一些调试程序可以运行该调试程序?还有其他方法可以杀死进程,以便在预定时间后有效地中断它吗?

Is there some debugging that could get this running? Is there any other method to kill a process so I effectively interrupt it after a predetermined amount of time?

推荐答案

更新

对于每条评论,我已经更新了响应,以提供添加超时的正确方法,并且在超时时仍然成功,同时还支持正常故障.

Per comment I have updated the response to provide the proper way of adding timeout and still succeed when timing out while also supporting normal failure.

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

Basically we check for error codes 124 (timeout) and 0 (success) and make sure that we don't exit on those codes. But if we receive anything else then we exit to match what github actions normally does on failures

超时成功演示

演示错误失败

代码段

    - 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

上一个响应

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

Only handled timing out but not continue to run on timeout without reporting failure. But OP wanted it to succeed on timeout as well so provided update above.

您可以通过使用超时分钟来做到这一点.以您的代码为例

You can do this by using timeout-minutes. Using your code as an example

    - 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"

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

For adding timeout to the job here is the resource: 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

For adding timeout for a single step here is the resource: https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstepstimeout-minutes

这篇关于在github工作流程中运行带有超时的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆