We are trying to develop a GitLab ci pipeline. We have three Jobs: A,B and C. We want to execute the C job only in the case when either A or B is executed.
How to define such job C?
I have tried using needs
but it give error as the needed job is not executed:
Found errors in your .gitlab-ci.yml:
'C' job needs 'A' job, but 'A' is not in any previous stage.
'C' job needs 'B' job, but 'B' is not in any previous stage.
My attempt:
C:
stage: push
image: alpine
tags:
- DOCKER
needs:
- job: B
artifacts: true
There is no concise, built-in way to do this that I am aware of, but the needs: optional
keyword may be helpful in conjunction with rules.
I could provide a more complete example if you were to post the rules of jobs A and B. Let's pretend that these are the rules defined for jobs A and B.
# Job runs only on the main branch
A:
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
- when: never
B:
rules:
- if: $CI_COMMIT_BRANCH =~ /feature\/*/i
- when: never
C:
stage: push
image: alpine
tags:
- DOCKER
needs:
- job: A
optional: true
artifacts: true
- job: B
optional: true
artifacts: true
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
- if: $CI_COMMIT_BRANCH =~ /feature\/*/i
- when: never
Beware that if the rules defined allow for a situation where job C is triggered without triggering either of the dependent jobs(A and B) it will not fail automatically since the dependent jobs are considered optional.