In Gitlab there is the needs
keywors for jobs with which I can create dependencies.
What is really unclear to me is how I can make a job b
depend on job a
but only run it when job a
succeeds (no failure). This seems like a basic idea but I cannot find anything related in the docs to make this work?
A:
stage: build
script:
- "false"
B:
stage: build
needs: ["A"]
script:
- "true"
In the above gitlab.yaml
pipeline, job B
runs after A
but also when A
is unsuccessful. I dont want to run B
when the before jobs in the DAG are failed.
How can we do this?
Well, that’s the default behaviour even though the documentation is not maybe very clear on it. Does your setup not work this way with your example pipeline? This is the output from my test:
B
is skipped when A
fails.
The pipelines overview documentation mentions that (emphasis mine):
Stages, which define how to group jobs together. Stages run in sequence, while the jobs in a stage run in parallel. For example, an early stage could have jobs that lint and compile code, while later stages could have jobs that test and deploy code. If all jobs in a stage succeed, the pipeline moves on to the next stage. If any job in a stage fails, the next stage is not (usually) executed and the pipeline ends early.
needs
works similarly, even if it’s not mentioned.
The idiomatic way of doing this is by separating each job into different stages like this:
stages:
- first
- second
A:
stage: first
script:
- echo "A"
B:
stage: second
script:
- echo "B"
Assuming you have a good reason to want to do everything in a single stage, one possible way would be by using the dotenv artifact:
stages:
- first
A:
stage: first
script:
- echo "A"
- echo "SUCCESS=false" > job.env
artifacts:
reports:
dotenv: job.env
B:
stage: first
needs: ["A"]
script:
- >
if $SUCCESS == "true"; then
echo "B"
fi
If you want to exit the job with a “failure” status code you can just include an exit 1
in the script.