Reputation: 1311
I have a GitLab CI pipeline with a 'migration' job which I want to be added only if certain files changed between current commit and master
branch, but in my current project I'm forced to use GitLab CI pipelines for push
event which complicates things.
Docs on rules:changes clearly states that it will glitch and will not work properly without MR (my case of push
event), so that's out of question.
Docs on rules:if states that it only works with env variables. But docs on passing CI/CD variables to another job clearly states that
These variables cannot be used as CI/CD variables to configure a pipeline, but they can be used in job scripts.
So, now I'm stuck. I can just skip running the job in question overriding the script
and checking for file changes, but what I want is not adding the job in question to pipeline in first place.
Upvotes: 2
Views: 2855
Reputation: 4400
Sadly, to add/remove a Gitlab job based on variables created from a previous job is not possible for a given
pipeline
A way to achieve this is if your break your current pipeline to an upstream and downstream
The upstream will have 2 jobs
Upstream
check_val:
...
script:
... Script imposes the logic with the needed checks
... If true
- echo "MY_CONDITIONAL_VAR=true" >> var.env
... If false
- echo "MY_CONDITIONAL_VAR=false" >> var.env
artifacts:
reports:
dotenv: var.env
trigger_your_original_pipeline:
...
variables:
MY_CONDITIONAL_VAR: "$MY_CONDITIONAL_VAR"
trigger:
project: "project_namespance/project"
The downstream would be your original pipeline
Downstream
...
migration:
...
rules:
- if: '$MY_CONDITIONAL_VAR == "true"'
Now the MY_CONDITIONAL_VAR
will be available at the start of the pipeline, so you can impose rules to add or not the migration
job
Upvotes: 1
Reputation: 40861
While you can't add a job alone to a pipeline based on the output of a script, you can add a child pipeline dynamically based on the script output. The method of using rules:
with dynamic variables won't work because rules:
are evaluated at the time the pipeline is created, as you found in the docs.
However, you can achieve the same effect using dynamic child-pipelines feature. The idea is you dynamically create the YAML for the desired pipeline in a job. That YAML created by your job will be used to create a child pipeline, which your pipeline can depend on.
Upvotes: 3