caffein
caffein

Reputation: 617

How to change variable in gitlab cicd pipeline depending on branch

I need to call a bash script from my gitlab cicd pipeline. When I call it, the input parameter needs to change depending on whether or not this is a merge into master. Basically what I want is this:

if master:

test:
  stage: test
  script:
  - INPUT="foo"
  - $(myscript.sh $INPUT)

if NOT master:

test:
  stage: test
  script:
  - INPUT=""
  - $(myscript.sh $INPUT)

I'm trying to figure out a way to set INPUT depending on which branch the pipeline is running on. I know there are rules as well as only/except, but they don't seem to allow you to set variable, only test them. I know the brute force way is to just write this twice, one with "only master" and another with "except master", but I would really like to avoid that.

Thanks

Upvotes: 1

Views: 4559

Answers (2)

amBear
amBear

Reputation: 1196

Why not have two jobs to run the script and use rules to control when they are ran against master:

test:
  stage: test
  script:
  - if [$CI_COMMIT_REF_NAME == 'master']; then export INPUT="foo"; else INPUT=""; fi
  - $(myscript.sh $INPUT)

Upvotes: 1

rvalle
rvalle

Reputation: 21

I implement this kind of thing using yml anchors to extend tasks.

I find it easier to read and customization can include other things, not only variables.

For example:

.test_my_software:
  stage: test
  script:
    - echo ${MESSAGE}
    - bash test.sh

test stable:
  <<: *test_my_software
  variables:
    MESSAGE: testing stable code!
  only:
    - /stable.*/

test:
  <<: *test_my_software
  variables:
    MESSAGE: testing our code!
  except:
    - /stable.*/

you get the idea...

Upvotes: 2

Related Questions