After having to manually update my repo because I forgot to change the version number... twice, I decided it was time for some CI magic.
Here's the deal: if I make a Merge Request into main
, I expect the application version number to have changed in 3 different files.
Here is my code so far:
stages:
- test
- deploy
workflow:
# some rules
unit_test:
stage: test
script:
# run UT
# This is the part the question is about
check_version:
stage: test
rules:
- if: '$CI_PIPELINE_SOURCE' == "merge_request_event" && '$CI_MERGE_REQUEST_TARGET_BRANCH_NAME' == '$CI_DEFAULT_BRANCH'
changes:
- composer.json
- resources/index.php
- Doxyfile
deploy:
stage: deploy
script:
# deployment script
rules:
- if: '$CI_COMMIT_BRANCH' == '$CI_DEFAULT_BRANCH'
If I understood things correctly, what I did is "If it's a Merge Request into main
, check for changes in composer.json
, resources/index.php
and Doxyfile
".
First: is it correct?
Second: what do I do to allow pushes and Merge Requests in other branches? Is there some kind of "else" that follows the "if"?
According to the documentation for the rules
keyword, the way you have it is correct based on the description of what you want. If a pipeline is run for a Push event, for a different branch, or if no changes are included for those three files, the check_version
job will not be added to the pipeline (you won't even see it in the pipeline diagram), and all other jobs will run based on their own rules
clause, if any, or an explicit when
clause.
However, others working on your project might find this way confusing, so personally I always add an explicit when:never
in these cases:
...
check_version:
stage: test
rules:
- if: '$CI_PIPELINE_SOURCE' == "merge_request_event" && '$CI_MERGE_REQUEST_TARGET_BRANCH_NAME' == '$CI_DEFAULT_BRANCH'
changes:
- composer.json
- resources/index.php
- Doxyfile
- when: never
...
This will end up with the same result as your example in your question, but in my opinion is more clear. When this is evaluated during the push/merge request/trigger/etc. event, if the if
conditional passes, the job is added to the pipeline. Otherwise, it [explicitly] isn't.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With