I have bash script that executes multiple times in single a job. If the script fails I want to exit out of the job and mark the job as failed.
plan.sh
#!/bin/bash
terraform init
terraform plan -out=${PLAN_FILE_NAME}
gitlab job
plan:
before_script:
- chmod +x ${CI_PROJECT_DIR}/gitlab/deploy/plan/plan.sh
- export PATH="$PATH:${CI_PROJECT_DIR}/gitlab/deploy/plan"
stage:plan
script:
- cd ${FOLDER1}
- plan.sh
- cd ${FOLDER2}
- plan.sh
- cd ${FOLDER2}
- plan.sh
artifacts:
- "${FOLDER1}/${PLAN_FILE_NAME}"
- "${FOLDER2}/${PLAN_FILE_NAME}"
- "${FOLDER3}/${PLAN_FILE_NAME}"
currently if the command terraform init or terraform plan fails for a folder, still the script keeps executing for the next folder and finally its marking the job as successful.
I want to exit out of job and mark the job as failed when there is an error during terraform init or terraform plan.
What would be a better way to handle this?
if ! terraform init; then exit 1; fietc etc#!/bin/bashto#!/bin/bash -eor addset -eas the next line of the script. (The latter is somewhat more robust, in that it also works if you explicitly run the script withbash plan.shinstead of the otherwise requiredbash -e plan.sh. But you are simply runningplan.shso this point is moot here.)