I am trying to run parallel commands on Azure using the bash script. The script is working correctly when all are successful. When one of them fails, the other processes are properly killed but the step is marked as successful. What am I missing?
- job: main
pool:
vmImageL 'ubuntu-latest'
steps:
# some steps before
- bash: |
PIDS=()
my_command_1 &
PIDS =($!)
my_command_2 &
PIDS =($!)
my_command_3 &
PIDS =($!)
for pid in "${PIDS[@]}"; do
wait $pid
done
# some steps after
The same script used in CircleCI and on GitHub Actions is working.
CodePudding user response:
Found the issue.
By adding set -eu
at the beginning of the bash script block, we ensure the step will return the error code when any of the commands fail.
CodePudding user response:
Explicitly checking exit status is likely a safer bet. Although "set -eu" may work for the immediate use case, it can have unexpected behavior:
https://mywiki.wooledge.org/BashPitfalls#set_-euo_pipefail
Explicit check:
for pid in "${PIDS[@]}"; do
wait $pid
if [ $? -ne 0 ]; then
exit 1
fi
done