ghoul
ghoul

Reputation: 1120

How to use variables in gitlab-ci.yml file

I'm trying to use variables in my gitlab-ci.yml file. This variable is passed as a parameter to a batch file that'll either only build or build and deploy based on parameter passed in. I've tried many different ways to pass my variable into the batch file but each time the variable is treated more like a static string instead.

I've read gitlabs docs on variables but cant seem to make it work.

  - build  

variables:
  BUILD_PUBLISH_CONFIG_FALSE: 0
  BUILD_PUBLISH_CONFIG_TRUE: 1

# BUILD ===============================
build: &build
    stage: build  
    tags: 
      - webdev  
    script:       
      - ./build.bat %BUILD_CONFIG%

build:branch:
  <<: *build
  variables:
    BUILD_CONFIG: $BUILD_PUBLISH_CONFIG_FALSE
  only:
    - /^(feature|hotfix|release)\/.+$/

build:branch:
  <<: *build
  variables:
    BUILD_CONFIG: $BUILD_PUBLISH_CONFIG_TRUE
  only:
    - /^(stage)\/.+$/

build:branch:
  <<: *build
  variables:
    BUILD_CONFIG: $BUILD_PUBLISH_CONFIG_TRUE
  only:
    - /^(master)\/.+$/

When watching gitlab's ci script execute, I expect ./build.bat 0, or ./build.bat 1. Each time it prints out as ./build.bat %BUILD_CONFIG%

Upvotes: 2

Views: 4258

Answers (2)

yves b.
yves b.

Reputation: 1

I suppose you only required to define a generic job, which parameters defined in derived jobs. The submitted script is rather complex for the question. This example below works fine, using a hidden job, and extends. The values set to variables in derived jobs, are applied for the common script :

.unittests: # hidden job extended below - unit test and coverage
  stage: test
  variables:
    UNITTEST_OUTPUT: 'D00_DATA/D04_RESULTS'
  script:
    - echo "Launching unittest ..."
    - python3 -m xmlrunner discover -s $UNITTEST_PATH --output-file $UNITTEST_OUTPUT/$TEST_OUTPUT_FILE_NAME

data_tests:
  extends: .unittests
  variables:
    UNITTEST_PATH: 'D01_DATA_ENG'
    TEST_OUTPUT_FILE_NAME: 'TEST-D01_DATA_ENG.xml'

model_tests:
  extends: .unittests
  variables:
    UNITTEST_PATH: 'D02_MODEL_ENG'
    TEST_OUTPUT_FILE_NAME: 'TEST-D02_MODEL_ENG.xml'

Upvotes: 0

BrunoAFK
BrunoAFK

Reputation: 126

When you place variables inside job, that mean that you want to create new variable (and thats not correct way to do it). You want to output content of variable setup on top? Can u maybe add that to echo? or something like that? I didn't get it what you are trying to achieve.

https://docs.gitlab.com/ee/ci/variables/#gitlab-ciyml-defined-variables

Upvotes: 1

Related Questions