1

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/"

Yogesh
  • 126
  • 9

2 Answers2

1

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Instead of "variables" section, is it possible to set the variable value within "script" section? – Yogesh Dec 19 '22 at 08:48
  • Actually, in my case the log file path will get generated dynamically within script section. So, I can't set it variable section. – Yogesh Dec 19 '22 at 08:54
  • @Yogesh I have edited the answer to address your comment. – VonC Dec 19 '22 at 08:59
0

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

jobB:
  stage: B
  variables:
    LOG_PATH: "/opt/testlogs/"
  ...
Bastian
  • 438
  • 4
  • 11
  • Instead of "variables" section, is it possible to set the variable value within "script" section? – Yogesh Dec 19 '22 at 08:48
  • Ok, I set up a pipeline with your script and it works like expected. I've only change the global Variables section to lowercase "variable". When your log file path is generated dynamically within script section, why do you want to set it globally? Or is the global value the default value and you want to change it in some cases, like in job B? – Bastian Dec 19 '22 at 10:38
  • Yes. There are 3 stages (executed in last) in which log file location is generated dynamically and rest of the stages using default location. – Yogesh Dec 19 '22 at 10:52