Skip to content

Commit

Permalink
Merge pull request #7 from cytopia/fail-command
Browse files Browse the repository at this point in the history
Add fail command
  • Loading branch information
cytopia authored Nov 17, 2022
2 parents 5c750d3 + 5ec5a44 commit f08c7d9
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
42 changes: 39 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,53 @@ on:
pull_request:

jobs:
build:
name: test
test-default:
name: test success
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: retry
uses: ./
with:
command: true

test-fail-command-1:
name: test fail command 1
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: retry
uses: ./
with:
command: false
retries: 2
pause: 1
fail_command: |
echo "This command has failed" \
&& echo "Failed command concatenated"
continue-on-error: true

test-fail-command-2:
name: test fail command 2
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: retry
uses: ./
with:
command: true
command: false
retries: 2
pause: 1
fail_command: |
sudo sysctl -w net.ipv4.ip_forward=1 \
&& sudo systemctl restart docker
continue-on-error: true
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ This action allows you to retry shell command for a few times in case they fail.

The following inputs can be used to alter the Docker tag name determination:

| Input | Required | Default | Description |
|-----------|----------|----------|-------------------------------------------|
| `retries` | No | `10` | How many times to retry on failure. |
| `pause` | No | `10` | How many seconds to wait between retries. |
| `command` | Yes | `` | Shell command to execute |
| `workdir` | No | `` | Switch to this working directory prior executing the shell command |
| Input | Required | Default | Description |
|----------------|----------|----------|-------------------------------------------|
| `retries` | No | `10` | How many times to retry on failure. |
| `pause` | No | `10` | How many seconds to wait between retries. |
| `command` | Yes | `` | Shell command to execute. |
| `workdir` | No | `` | Switch to this working directory prior executing the shell command. |
| `fail_command` | No | `` | Shell command to execute on every failure of given `command`. |


## :arrow_backward: Outputs
Expand Down Expand Up @@ -48,6 +49,12 @@ jobs:
pause: 10
command: |
docker build -t test .
# Builing docker sometimes fails on GH Action runner due to network issues.
# We ensure that forwarding is reenabled and the daemon is restarted
# Before retrying the docker build command
fail_command: |
sudo sysctl -w net.ipv4.ip_forward=1 \
&& sudo systemctl restart docker \
```
Expand Down
10 changes: 10 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ inputs:
description: 'Shell command to execute'
required: true
default: 'true'
fail_command:
description: 'Shell command to execute on every failure of given command (The fail_command will always succeed via: || true)'
required: false
default: ''
workdir:
description: 'Switch to this working directory prior executing shell command'
required: false
Expand All @@ -37,6 +41,11 @@ runs:
echo "[SUCC] ${n}/${RETRIES}";
return 0;
fi;
if [ -n "${FAIL_COMMAND}" ]; then
echo "Executing fail command:";
echo "${FAIL_COMMAND}";
eval "${FAIL_COMMAND}" || true;
fi;
sleep ${PAUSE};
echo "[FAIL] ${n}/${RETRIES}";
done;
Expand All @@ -51,3 +60,4 @@ runs:
PAUSE: ${{ inputs.pause }}
COMMAND: ${{ inputs.command }}
WORKDIR: ${{ inputs.workdir }}
FAIL_COMMAND: ${{ inputs.fail_command }}

0 comments on commit f08c7d9

Please # to comment.