4

is there anyway to do multiple AND variable expression?

let's say

    .template1:
      only:
        variables:
          - $flag1 == "true"
    
    .template2:
      only:
        variables:
          - $flag2 == "true"
    
    job1:
      extends:
        - .template1
        - .template2
      script: echo "something"

How will this get evaluated?

  • Is this going to result in only:variables overwriting each other thus template2 is the final result?
  • or is this going to result in a combined variables such that it becomes an OR statement
    only:
      variables:
        - $flag1 == "true"
        - $flag2 == "true"

Is there anyway to make it as and AND statement instead? keeping the templating system, and without using rules: if since using rules if has its own quirk, triggering multiple pipeline during merge request

2
  • Just following up. Did you solve your problem? Commented Aug 21, 2020 at 3:18
  • yeah.. it makes sense to use different attribute to make it works. Thank you Commented Aug 23, 2020 at 13:58

1 Answer 1

2

Problem

Any time two jobs get "merged", either using extends or anchors, GitLab will overwrite one section with another. The sections don't actually get merged. In your case, you're extending from two jobs, so GitLab will completely overwrite the first variables section with the second.

Solution

One way to achieve your desired result is by defining the variables in your template jobs. The problem you will have then is the two variables sections will overwrite each other. So..

You can use a before_script section to define the variable in the 2nd template. This approach works for your specific case of 2 templates. You can use script and after_script if you need a third template, buy you'd have to use a more advanced approach if you need more templates than that.

    .template1:
      # We can define a variables section here, no problem
      variables:
        flag1: "true"
    
    .template2:
      # You can't define a second variables section here, since it will overwrite the first
      # Instead, define the environment variable directly in a before-script section
      before_script:
        - export flag2="true"
    
    job1:
      extends:
        - .template1
        - .template2
      only:
        variables:
          - $flag1 == "true"
          - $flag2 == "true"
      script: echo "something"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.