Skip to content

Commit 7c67318

Browse files
authored
Merge pull request #6 from jonpugh/feature/wait-for
Add `wait-for` script
2 parents bf2df83 + 794afa7 commit 7c67318

File tree

4 files changed

+180
-52
lines changed

4 files changed

+180
-52
lines changed

.github/workflows/scripts.run-with-summary.yml

Lines changed: 0 additions & 52 deletions
This file was deleted.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Test the scripts.
2+
name: Test Scripts
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
env:
8+
# Allow running run-with-summary command.
9+
PATH: ./src:./bin:./vendor/bin:/usr/local/bin:/usr/bin:/bin
10+
11+
jobs:
12+
13+
run-with-summary:
14+
name: run-with-summary script
15+
runs-on: ubuntu-latest
16+
env:
17+
SUCCESS: "Script run-with-summary succeeded! :white_check_mark:"
18+
ERROR: "run-with-summary Failed! :x:"
19+
SUMMARY: |
20+
- Environment: https://pr${{ github.event.number }}.demo.site
21+
- Pull Request: ${{github.event.pull_request.html_url }}
22+
23+
# Show extra details
24+
DEBUG: yes
25+
26+
steps:
27+
- uses: actions/checkout@v4
28+
- name: run-with-summary success
29+
env:
30+
SUCCESS: "Directory List :white_check_mark:"
31+
run: |
32+
run-with-summary ls -la
33+
34+
- name: run-with-summary hide
35+
env:
36+
HIDE: true
37+
SUCCESS: Processes
38+
SUMMARY: This test should not show the process details table.
39+
run: |
40+
run-with-summary ps -aux
41+
42+
- name: run-with-summary failure
43+
continue-on-error: true
44+
env:
45+
ERROR: "run-with-summary Failed on purpose! :x:"
46+
run: |
47+
echo $GITHUB_STEP_SUMMARY
48+
run-with-summary ping w3.org -c5
49+
50+
wait-for:
51+
name: wait-for script
52+
runs-on: ubuntu-latest
53+
steps:
54+
- uses: actions/checkout@v4
55+
56+
- name: wait-for failure
57+
continue-on-error: true
58+
env:
59+
TIMEOUT: 2
60+
run: |
61+
run-with-summary wait-for bad-command
62+
63+
- name: wait-for success
64+
continue-on-error: true
65+
env:
66+
TIMEOUT: 5
67+
run: |
68+
run-with-summary wait-for sleep 2

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ jobs:
1919
steps:
2020
- uses: jonpugh/goatscripts@v1
2121
```
22+
## Scripts
23+
- `run-with-summary`: Runs a command and generates a markdown summary with output and execution details.
24+
- `wait-for`: Runs a command repeatedly until it passes.
2225

2326
## Run With Summary
2427

@@ -70,3 +73,27 @@ jobs:
7073
Set SUMMARY env var to add *any* markdown **you want** to the report file.
7174
```
7275
76+
## wait-for
77+
78+
runs a command until it passes or timeout is reached.
79+
80+
### Use cases:
81+
82+
- Docker containers that have to wait for a database server to start
83+
- Test scripts that have to wait for your project to initialize.
84+
- Better than "sleep": Sometimes you need to wait. Instead of sleeping a fixed number of seconds, run a command that will pass once the thing you need is ready.
85+
86+
```
87+
wait-for mysql-ready
88+
wait-for curl https://deploy-url/ready
89+
```
90+
91+
### Options
92+
93+
```
94+
# SLEEP: Length of time to wait in-between running the command.
95+
# CHAR: The character to print after every command run.
96+
# OUTPUT: Set to 'all' to print all output, set to 'out' to print just stdOut, set to "err" to print just stdErr.
97+
# TIMEOUT: Default: 30. Exit with an error if process doesn't pass within this time.
98+
# SILENT: Set to 1 to not print any output except the timeout exceeded error. Useful when running from other scripts. See wait-mysql
99+
```

src/wait-for

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env bash
2+
# wait-for
3+
#
4+
# @author Jon Pugh
5+
# Created Jan 2020 for opendevshop: https://github.com/opendevshop/devshop/blob/1.x/scripts/wait-for
6+
#
7+
# Runs any command over and over again until it passes, hiding all output but printing a character every time it runs.
8+
#
9+
# Use Environment variables for options:
10+
#
11+
# SLEEP: Length of time to wait in-between running the command.
12+
# CHAR: The character to print after every command run.
13+
# OUTPUT: Set to 'all' to print all output, set to 'out' to print just stdOut, set to "err" to print just stdErr.
14+
# TIMEOUT: Default: 30. Exit with an error if process doesn't pass within this time.
15+
# SILENT: Set to 1 to not print any output except the timeout exceeded error. Useful when running from other scripts. See wait-mysql
16+
#
17+
18+
# Document usage
19+
usage() {
20+
cat <<<EOF
21+
Usage:
22+
wait-for any-command any-arguments --any-options
23+
EOF
24+
}
25+
26+
# Set Environment
27+
set -e
28+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
29+
PATH="$DIR:$PATH"
30+
31+
# Prepare arguments and options.
32+
COMMAND=$@
33+
34+
# Environment Variables
35+
SLEEP=${SLEEP:-1}
36+
CHAR=${CHAR:-.}
37+
OUTPUT=${OUTPUT:-}
38+
TIMEOUT=${TIMEOUT:-30}
39+
SILENT=${SILENT:-""}
40+
41+
# Check for required variables
42+
if [ -z "${COMMAND}" ]; then
43+
usage
44+
exit 1
45+
fi
46+
47+
runCommand() {
48+
49+
# If $OUTPUT=out, hide errors.
50+
if [ "${OUTPUT}" == "out" ]; then
51+
$COMMAND 2> /dev/null
52+
53+
# If $OUTPUT=err, print only errors
54+
elif [ "${OUTPUT}" == "err" ]; then
55+
$COMMAND > /dev/null
56+
57+
# If $OUTPUT=all, just print
58+
elif [ "${OUTPUT}" == "all" ]; then
59+
$COMMAND
60+
61+
# Otherwise, hide output and errors
62+
else
63+
$COMMAND > /dev/null 2>&1
64+
fi
65+
}
66+
67+
if [ -z "${SILENT}" ]; then
68+
echo "Running command until it succeeds: $COMMAND"
69+
fi
70+
71+
while ! (runCommand)
72+
do
73+
# Exit with an error if timeout is reached.
74+
[ "$SECONDS" -gt "$TIMEOUT" ] && echo && echo "Timeout exceeded. Command failed for $SECONDS seconds." && exit 1
75+
76+
# Pause for $SLEEP seconds.
77+
sleep $SLEEP
78+
79+
# Print $CHAR without a new line.
80+
echo -n "$CHAR"
81+
done
82+
83+
if [ -z "${SILENT}" ]; then
84+
echo "Command completed after $SECONDS seconds."
85+
fi

0 commit comments

Comments
 (0)