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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Instead of "variables" section, is it possible to set the variable value within "script" section?
Actually, in my case the log file path will get generated dynamically within script section. So, I can't set it variable section.
@Yogesh I have edited the answer to address your comment.
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/"
  ...

3 Comments

Instead of "variables" section, is it possible to set the variable value within "script" section?
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?
Yes. There are 3 stages (executed in last) in which log file location is generated dynamically and rest of the stages using default location.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.