How can I correctly capture and handle the exit status of the Docker command while still piping its output to both a file and the console?
The potential issue probably is that, when using a pipe (|) to send the output of one command to another, only the exit code of the last command in the pipeline is captured by $?, not the exit code of the original Docker command.
docker run tensorpod/pymimic3:latest \
bash -ic "pytest tests/test_metrics" 2>&1 | tee output.txt
What you can try to capture the correct exit code of the Docker command while still piping its output to both a file and the console
name: Test Docker Command
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Run test
run: |
docker_output=$(docker run tensorpod/pymimic3:latest \
bash -ic "pytest tests/test_metrics" 2>&1)
docker_exit_code=$?
echo "$docker_output" | tee output.txt
echo "Pipe status: ${PIPESTATUS[0]}"
echo "Exit status: $docker_exit_code"
if [ $docker_exit_code -ne 0 ]; then
echo "The command inside Docker failed."
exit 1
fi