# Stage to pull and push image
job1:
stage: job1
allow_failure: true
script:
# Pull image and save success
- docker pull ${SOURCE_IMAGEURI}:${TAG}
...
- docker tag ${SOURCE_IMAGEURI}:${TAG} ${TARGET_IMAGEURI}:${TAG}
# Job might fail here due to not enough permissions which is what I want to happen
- docker push ${TARGET_IMAGEURI}:${TAG}
- echo "Error Message" > curldata.txt
artifacts:
when: always
paths:
- curldata.txt
job2:
stage: job2
script:
# Do something with the error that happened in job1
when: always
dependencies:
- job1
So above is a part of a job that pulls and pushes an image. Sometimes the image will fail to push though as a safety step due to lack of permissions. How would I capture the error that happens so that I can send that artifacts to the next feedback job. This job will send the information to the user so he/she knows that they didnt have enough permissions.
CodePudding user response:
You can tee
the (stderr) output of the command to a file and artifact the file.
script:
# ...
# show output of the command and store stderr to text file
- docker push ${TARGET_IMAGEURI}:${TAG} 2> >(tee stderr.txt)
artifacts:
paths:
- stderr.txt
when: always
If you need some logic to happen after the error, you can use and/or logic gates.
docker push ${TARGET_IMAGEURI}:${TAG} || echo "do this if it fails" > error.txt && exit 1
There's more to be said about robust error handling in bash, but those are some basic ideas you can use.