I have a lot of pipelines where I build things, and then the deployment runs on releases and is triggerd by a pipeline trigger. So to only run build/test jobs when pushing to gitlab, I have this:
job:
only:
refs:
- branches
But according to Gitlab’s CI/CD YAML syntax reference , only
and except
are deprecated, so I am trying to figure out how to do the same with rules.
After a deep read of the rules possibility, I am down to these two possibilities:
job:
rules:
- if: $CI_COMMIT_BRANCH
or
job:
rules:
- if: $CI_PIPELINE_SOURCE == 'push' && $CI_COMMIT_BRANCH
But I don’t really understand which would be better, and which is the correct translation. I asked a few llms, and their answers were confusing at best.
1
The following block means when the Git reference for a pipeline is a branch. It will be triggered when calling via api (instead of push), and also scheduled pipelines too.
job:
only:
refs:
- branches
so it is more than push (api call, scheduled pipelines, etc). Therefore the correct translation will be
job:
rules:
- if: $CI_COMMIT_BRANCH