I have gitlab-ci similar to this:
stages:
- test
- build
- deploy
tests:
stage: test
build:
stage: build
only:
- master
deployment:
stage: deploy
only:
- master
Is there a way to allow at the same time:
- always build and deploy from master
- add manual action to build and deploy from any other branch ?
I know there is a when: manual
https://docs.gitlab.com/ee/ci/yaml/#when , but is it possible to combine it like
when: manual or master
?
Combining when and only is not a good solution, because it will stop automatic build/deploy from master
1 Like
Michael -
There wasnât an easy way to do this until the introduction of ârulesâ which allows for more fine-grained control over when jobs run. Rules
will enable you to have more straightforward, more intuitive cases of âif / if else / elseâ than only:
does.
To accomplish your goal, you add these two rules to the job:
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
when: always
- if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH
when: manual
Iâve illustrated this in a demo repo where you can see the default branch pipeline will deploy, whereas the feature branch pipeline has the job but requires you to âplayâ it manually.
Hope that helps!
3 Likes
thanks! this is exactly what i was looking for
1 Like
Hi @olearycrew
I started using rules, as you suggested, I noticed some changes:
Below you can see example MR. When opening merge requests, my pipeline is splited into two parts, regular, and detached. Regular (three step) has test, build and deploy. Detached (upper) has only build and deploy.
- Right now, test results are not displaying in MR overwiev (like it was before).
- For some reason, deploy step (after manual build) is always failed (with error: The deployment job is older than the previously succeeded deployment job, and therefore cannot be run)
- Merging is not possible, until all manual actions are completed (this will probably be fixed with âSkipped pipelines are considered successfulâ in Settings->General->Merge Requests)
I tried to find a root of âdetached pipelineâ behaviur. The âdetachedâ label popup mentions âPipelines for Merged Resultsâ which is premium feature (not used by me, there is no âmerge trainsâ or âpipelines for merged resultsâ in my settings)
Can you help me with that?
update:
even with checking âskipped pipelines are considered successfulâ all the manual steps has to run before merge request is merged. Is this a bug?
probably related to https://gitlab.com/gitlab-org/gitlab/-/issues/34756
I think I need incorporate something like this:
.default_rules: &default_rules
rules:
- if: $CI_MERGE_REQUEST_ID
- if: $CI_COMMIT_BRANCH == 'master'
.deploy_rules: &deploy_rules
rules:
- if: $CI_COMMIT_BRANCH == 'master'
when: always
- if: $CI_MERGE_REQUEST_ID
when: never
in the gitlab-ci.yml