CI/CD: Add only: default-branch shorthand for rules.if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH
Everyone can contribute. Help move this issue forward while earning points, leveling up and collecting rewards.
Proposal
I'm proposing to enhance the developer experience by adding another keyword to the CI/CD definition under the only/except rule:
It should be possible to specify
only:
- default-branch
Which would have the same effect as
rules:
- if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH
(correspondingly with except and $CI_COMMIT_REF_NAME != $CI_DEFAULT_BRANCH)
Reasoning
Many projects achieve the same result by specifying the name of the default branch directly:
only:
- main
But a lot of .gitlab-ci.yml files are being generated by copy-pasting from different repos or samples. Although better solutions than that exist, there's no denying that copy-paste is one of the primary methods of generating a yaml file.
This has the caveat that while using samples or templates, one has to update the branch name, which is easily overlooked.
With many projects currently switching (or having switched) from master to main as the default branch name, it's also likely that using either one as an educated guess will cause issues for any repo that does not follow the selected default.
To remediate, we can use variables:
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
This has the intended effect.
But I'd argue that it's legibility is not optimal. Since this is such a generic use-case, I'd argue that this rule could be abstracted into the above syntax, which would massively improve developer experience and readability.