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



我有一个类似于下面代码的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 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安装,但也没有用。

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

更新

根据评论,我已经更新了响应,以提供添加超时的正确方式,并且在超时时仍然成功,同时也支持正常故障。

基本上,我们检查错误代码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

以前的响应

只处理超时,但不在超时时继续运行而不报告失败。但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"

要向作业添加超时,这里有资源:https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idtimeout-分钟

对于添加单个步骤的超时,这里有资源:https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstepstimeout-分钟

最新更新