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.
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.
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?
UPDATE
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.
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
DEMO SUCCESS ON TIMEOUT
DEMO FAILURE ON ERROR
Code Snippet
- 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
PREVIOUS RESPONSE
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"
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With