I have a pipeline in gitlab-ci (for a python repo), and I try to manage my cases and conditions for the different jobs with as less code duplication as possible.
Assume there are dozen of jobs.
I actually have 3 options I want the pipeline to run, but wish to support for future options. Not all jobs need to run in the same options.
- open a new merge request/push into a branch in a merge request
- master push (deploy package), happens after every merge request.
- Schedule trigger weekly.
I think the best option is to define 3 variables, one for each option, and then each job rules can be defined by the three. For example:
update-lockfile:
stage: build
script:
- echo "Running Update lockfile"
- task conda:update-lockfile
rules:
- if: $ONLY_CI_JOB == "False" && $SCHEDULE_TASK != "update_lockfile"
when: never
- changes:
- rattler.recipe/*
The update-lockfile
job will not run if we are not in a MR or if its not schedule, and if it is one of them, it will run only if there was a change in the recipe. Every job can have different rules but it will be short, only based on these 3 variables.
The problem in this method:
How do I define the variables? Stupid as it sounds, if I set them in the variables
section, I cannot use if/else
statements, whish I wanted to use, like:
- |
if [$CI_PIPELINE_SOURCE == 'merge_request_event'] || ([$CI_PIPELINE_SOURCE == 'push'] &&
[$CI_COMMIT_BRANCH == '$CI_DEFAULT_BRANCH']); then
- export ONLY_CI_JOB="true"
fi
If I use this if statement in the before_script
it actually first go through the rules to decide which jobs to run, and only then execute it, so it doesn’t help.
And lastly, trying to set it in the variable, but not with if/else
but something like:
ONLY_CI_JOB=[$CI_PIPELINE_SOURCE == 'merge_request_event'] || ([$CI_PIPELINE_SOURCE ==
'push'] && [$CI_COMMIT_BRANCH == '$CI_DEFAULT_BRANCH']
Doesn’t work because it doesn’t let me set variables as Boolean.
Any idea of a nice way to do it? Without setting it again and again in every job’s rules?