# 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
所以上面是拉取和推送影像的作業的一部分。有時,由于缺乏權限,影像將無法推送作為安全步驟。我將如何捕獲發生的錯誤,以便我可以將該工件發送到下一個反饋作業。此作業會將資訊發送給用戶,以便他/她知道他們沒有足夠的權限。
uj5u.com熱心網友回復:
您可以tee將命令的 (stderr) 輸出輸出到檔案并制作該檔案。
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
如果您需要在錯誤之后發生一些邏輯,您可以使用和/或邏輯門。
docker push ${TARGET_IMAGEURI}:${TAG} || echo "do this if it fails" > error.txt && exit 1
關于bash 中強大的錯誤處理還有更多要說的,但這些是您可以使用的一些基本想法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/361834.html
