How to setup manual job from feature branch that runs automatically on master?

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!

1 Like

thanks! this is exactly what i was looking for :slightly_smiling_face:

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.

  1. Right now, test results are not displaying in MR overwiev (like it was before).
  2. 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)
  3. 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?
Screenshot 2020-07-15 15.11.31

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