Yogesh
Yogesh

Reputation: 303

Gitlab - Update Pipeline variable dynamically

I have created a Gitlab Pipeline. I want to use variable for which I can set the value dynamically.

e.g. I have created a variable with default value which will be used in each stage of the pipeline. But for Stage B I want to use different value for that variable. reference code as below.

jobA:
  stage: A
  allow_failure: true
  script:
    - echo "$LOG_PATH"
    - echo "jobA" > ./completedA
  artifacts:
    paths:
      - ./completedA
jobB:
  stage: B
  allow_failure: true
  script:
    - LOG_PATH="/opt/testlogs/"
    - echo "${LOG_PATH}"
    - exit 1
    - echo "jobB" > ./completedB
  artifacts:
    paths:
      - ./completedB

stages:
  - A
  - B
Variables:
  LOG_PATH: "/opt/test/" 

Current output for the variable:

For Stage A, Value of LOG_PATH is "/opt/test/"

For Stage B, Value of LOG_PATH is "/opt/test/"

Expected output for the variable:

For Stage A, Value of LOG_PATH is "/opt/test/"

For Stage B, Value of LOG_PATH is "/opt/testlogs/"

Upvotes: 1

Views: 1585

Answers (2)

VonC
VonC

Reputation: 1329092

Looking at the "Create a custom CI/CD variable in the .gitlab-ci.yml " section, you might need to set the variable in the variables: section of the job

jobB:
  variables:
    LOG_PATH: "/opt/testlogs/"
  stage: B
  allow_failure: true
  script:
    - echo "${LOG_PATH}"
    - exit 1
    - echo "jobB" > ./completedB
  artifacts:
    paths:
      - ./completedB

Instead of "variables" section, is it possible to set the variable value within "script" section?
In my case, the log file path will get generated dynamically within script section. So, I can't set it variable section.

That is what is tested in "How to set variable within 'script:' section of gitlab-ci.yml file"

Check if the approach illustrated in "How to set gitlab-ci variables dynamically?", using artifacts.

Upvotes: 1

Bastian
Bastian

Reputation: 518

You can set the variable inside the job, this will overwrite the global variable (see docs)

jobB:
  stage: B
  variables:
    LOG_PATH: "/opt/testlogs/"
  ...

Upvotes: 0

Related Questions