Gitlab CI: Fail pipeline if variable not set?
Asked Answered
S

2

10

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.

Switchblade answered 6/7, 2020 at 15:21 Comment(0)
P
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

Phoneme answered 6/7, 2020 at 15:25 Comment(3)
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 exitSwitchblade
$options doesn't have default value if not set? you should use it in if condition instead of nullChondrule
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.Phoneme
B
0

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.

Bradwell answered 7/3 at 6:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.