Alexander Presber
Alexander Presber

Reputation: 6635

Gitlab CI: How to pass error message from script

In our .gitlab-ci.yml we have this job:

deploy:
  stage: deploy
  script:
    - ./scripts/deploy.sh $INSTANCE_NAME

and inside our scripts/deploy.sh we have

  if ! [ <some error condition> ]; then
    exit 1
  fi

When the error is actually triggered and we exit with status 1, we also would like to pass an error message, because otherwise we will just get an empty reason in our Gitlab job output:

ERROR: Job failed: Process exited with: 1. Reason was:  ()

How can we get an error message to appear inside the brackets instead?

Upvotes: 5

Views: 4964

Answers (1)

Gino Mempin
Gino Mempin

Reputation: 29637

How about printing the error message as part of the script before the exit 1?

do_something

# check if it succeeded
if [[ ! $? -eq 0 ]]; then
    print_error "The last operation failed."

    exit 1
fi

That would show up as:

enter image description here

That has the benefit of having the same error message appear whether you're running the script manually/locally or when running in a CI job.

Upvotes: 3

Related Questions