Extends with rules

Hi all!

I’m faced with an issue I’m not able to solve.
So I want to move a part of a configuration in a separate file to use across the jobs.
Here is it:

.base_only:
  rules:
    - if: '$CI_COMMIT_BEFORE_SHA == "0000000000000000000000000000000000000000"'
      when: never
    - if: $CI_COMMIT_TAG
    - if: $CI_COMMIT_BRANCH
    - when: never

There I define basic rules for a job run.
Next, I want to use it within a job with its own when: manual rule.
I can’t use it on a job level - I’m getting an error from lint saying when can’t be used there.
This does not work:

test-one:
  extends:
    - .base_only
  stage: build
  script: echo "Test one";false
  when: manual

When I use rules within job too the most recent rules is used:

test-one:
  extends:
    - .base_only
  stage: build
  script: echo "Test one";false
  rules:
    - when: manual

So after merge, I just want to get the job with following definition:

test-one:
  stage: build
  script: echo "Test one";false
  rules:
    - if: $CI_COMMIT_REF_NAME =~ /^release.*$/i
      when: manual
    - if: $CI_COMMIT_REF_NAME =~ /^hotfix.*$/i
      when: manual
    - if: $CI_COMMIT_TAG
      when: manual
    - when: never

Is it possible? Or there are no possibilities to merge rules?

2 Likes

I am asking myself the same question right now.
Did you find a solution ?

From the GitLab Documentation, You can use extends to merge hashes but not arrays. Since rules are listed as arrays, I suppose it can’t be merged. Would be nice to have such a feature though.

As @xingrz1993 said extends does not merge arrays. To merge arrays You’ll need to use yaml anchors or !reference.

But there is a problem here too since !reference does add the array nested.

Works are being done to fix this. Hopefully the feature with land in the 14.2.

You can find the problem described in this issue, also some possible solutions and workarounds:

These issues are about the same problem almost:

1 Like