In my gitlab CI pipeline I am generating and triggering a child pipeline like so:
generate-child-pipeline:
stage: make_child_pipeline
needs:
- job: image-build
optional: true
script:
- ./generate_pipeline.sh
artifacts:
paths:
- child_pipeline-gitlab-ci.yml
rules:
- <assorted rules>
tags:
- my_tag
trigger-child-pipeline:
stage: make_child_pipeline
needs:
- job: generate-child-pipeline
trigger:
include:
- artifact: child_pipeline-gitlab-ci.yml
job: generate-child-pipeline
rules:
- !reference [generate-child-pipeline, rules]
Which works fine if I ensure that conditions are such that the rules
cause the jobs to get added to my pipeline. However, if those conditions are not met, it appears that the trigger-child-pipeline
job still gets added to my pipeline (despite having exactly the same rules as generate-child-pipeline
. And then the pipeline fails with a yaml
error because trigger-child-pipeline
is present in the pipeline but generate-child-pipeline
isn't:
'trigger-child-pipeline' job needs 'generate-child-pipeline' job, but 'generate-child-pipeline' is not in any previous stage
So it seems like there is some other reason why the trigger job is getting added to my pipeline, aside from the rules I have set. What could that possibly be? Are trigger jobs just added to pipelines by default or something? I'm not sure how to get the proper behaviour out of this. My child pipeline should simply not get triggered if the rules are not met, or that was the behaviour I expected anyway. Not sure what else could logically make sense...
Edit: I tried adding
- if: $CI_PIPELINE_SOURCE == $CI_PIPELINE_SOURCE
when: never
to the end of the rules for trigger-child-pipeline
, and this did nothing. However, if I add this and delete all the other trigger-child-pipeline
rules, then indeed trigger-child-pipeline
doesn't get added to the pipeline and we are fine. But I have no idea what is going on, because I am using a reference to set the rules to be the same as generate-child-pipeline
, which seems to be behaving correctly, so I don't see how one job could be added to the pipeline but not the other.
It's always tempting to blame things on gitlab-ci bugs, but that's kind of what this is feeling like. Maybe gitlab is incorrectly checking the dependencies for the trigger job before checking whether it is actually part of the pipeline, or some weird issue like that...
Ok so I figured it out. Turns out I had forgotten a few things about how my
rules
worked, and actually they had some variable expansions in them. I deleted the variables from my example because I thought they weren't relevant, but they were the problem. I had them defined in one job and not the other, causing them to effectively have different rules despite how it appears on the surface.So ok it all makes sense and trigger jobs don't do anything weird with rules, and there is no weird gitlab bug here.