I have got a GitLab CI pipeline where I am trying to run backend builds only when there are changes in the corresponding directory. For this I am using the "changes"-rule to check for any modifications.
The backend_build
and backend_deploy
jobs both check for modifications, but the deployment also needs the backend_build
to be run successfully first, for which I am using the "needs" keyword.
Now when changes are done in another folder (e.g. "frontend") I am getting the following error:
'backend_deploy' job needs 'backend_build' job, but 'backend_build' is not in any previous stage
image: maven:3.9-amazoncorretto-17
stages:
- build
- deploy
.backend_build_rules:
rules:
- changes:
- backend/**/*
- .gitlab-ci.yml
backend_build:
stage: build
extends: .backend_build_rules
script:
- echo "Backend building..."
backend_deploy:
stage: deploy
extends: .backend_build_rules
script:
- echo "Backend deploying..."
needs:
- job: backend_build_job
frontend_build:
stage: build
...
frontend_deploy:
stage: deploy
...
Does anyone know how I can successfully combine those two keywords?
You can overcome this behavior via needs:optional keyword:
backend_deploy:
stage: deploy
extends: .backend_build_rules
script:
- echo "Backend deploying..."
needs:
- job: backend_build_job
optional: true
With this setting the backend_deploy
job will always wait for backend_build
job, when backend_build
presents in the pipeline. According to your configuration backend_deploy
and backend_build
jobs have the same rules so the case when only backend_deploy
will be in the pipeline should not be possible.