11

Is there a way fail my entire pipeline if the user triggered the pipeline and didn't set a variable called options with a value?

I've tried things like only and rules but they just skip the job instead of failing all jobs.

2 Answers 2

7

Yes, though the way you fail would be dependent on the system your runner's based on.

For example, in a Linux/bash based runner, all you need is exit 1 (as opposed to exit 0) to stop execution and fail the pipeline

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks I've given it a try but I can't get the pipeline to exit am I doing something wrong with this if in my script section? script: - if [$options == null]; then exit 1; fi The next line echos out options so I can see it's null but the if didn't exit
$options doesn't have default value if not set? you should use it in if condition instead of null
Yep, MoonHorse hit the nail on the head here. Just do use if [ -z $options]; to make it a condition that checks if it's either unset, or null. This StackOverflow Question has a good summary of the different sorts of checks you could use here, but the approach above looks to be the best bet if you're looking to catch cases where the variable is unset or null.
1

Testing the options environment variable in the script is one possibility. Another is to use the options variable to include (or exclude) a "killer" job from the pipeline. For example:

verify-options-set:
  stage: .pre
  image: node:20
  tags:
    - linux-docker
  rules:
    - if: '$options == null'
  script:
    - echo "Define "options" to run the pipeline!"
    - exit 1

If the options environment variable is not set, this job (which always fails) is included in the pipeline. If it is set, the job is ignored.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.