Replace only/except with rules, whole pipeline not running

I am having a devil of a time trying to replace a simple except rule with a rule. I am trying to exclude a job from running on the test and production branches (and during scheduled runs), but the whole pipeline is not running when merging to test and I do not understand why. Here is the pertinent CI code:

workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

job-name:
  stage: analyze
  rules:
    - if: '$CI_COMMIT_REF_NAME =~ /(production|test)/'
      when: never
    - if: $CI_PIPELINE_SOURCE == "schedule"
      when: never
    - when: on_success

The workflow rules were added to prevent duplicate merge/branch pipelines. As I noted it seems the restriction in the job rules is killing the entire pipeline, and I do not understand why this is.

Hi @powellbc

I think this issue is probably your workflow rules:

workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

Here, you are restricting your pipelines to only run when the source is a merge request event or the branch is the default branch.

You say that the pipeline won’t run when “merging to test”. Does that mean that after you have merged an MR to the test branch, you are not getting a pipeline running on test? If so, that’s because if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH would exclude that case, if test is not your default branch.

1 Like

Thanks so much for the guidance. I suppose to fix I might want to use - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH || $CI_COMMIT_BRANCH == "test" || $CI_COMMIT_BRANCH == "production" or something similar?

I also am wondering the MergeRequest-Pipelines template would be what I should use?

Well, you don’t have to use the templates, you can make your own rules, and as you’ve probably noticed, they are just bits of Bash code. I would give it a go and see how you get on. I keep a throwaway repo that I use for testing this sort of thing, which makes it a little easier to rebase your “real” commit history.

Awesome, thanks for the guidance in getting me started. For whatever reason ci rules have been flummoxing me since I started trying to use them.

There are lots of little details that can trip you up, but when you get the hang of rules they are really powerful. Good luck!