Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gitlab only: variables multiple

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

like image 509
Harts Avatar asked Sep 07 '25 04:09

Harts


1 Answers

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"
like image 169
DV82XL Avatar answered Sep 09 '25 04:09

DV82XL