0

Colleagues, I have a variable that I get inside the gitlab task with a bash script.

add_token_uat:
    stage: add_token
    script:
        - |
          token=$(curl -X 'POST' 'https://xxxx.xxxx.uat.fgislk.xxxxxx.xxx/subsystem/token/get' -H 'accept: */*' -H 'Content-Type: application/json' -d '{
            "subsystem": "XXXX",
            "authKey": "XXXXX"
          }' -sk | grep -Po '{"token":"\K[^","]+')
    rules:
        - if: $STAND == "uat"

How to pass the token variable further in the pipeline? I want to use this variable in the next task:

add_subsystem_microservices_configs:
    stage: add_subsystem_microservices_configs
    script:
        - ansible-playbook roles/add_subsystem.yml --extra-vars "@vars/${SUBSYSTEM}.yml" -D
    when: manual

How to pass variable to add_subsystem.yaml file?

1 Answer 1

1

You can pass variables to subsequent jobs by uploading it as a specific kind of job artifact artifacts:reports:dotenv.

For example, the first job below puts a variable into a .env file (each line has NAME=value), then stores the file as a dotenv report artifact. Then, in subsequent jobs, there will be a variable $NAME which holds the value from the file, which you can then use as needed.

build:
  stage: build
  script:
    - echo "BUILD_VARIABLE=value_from_build_job" >> build.env
  artifacts:
    reports:
      dotenv: build.env

deploy:
  stage: deploy
  variables:
    BUILD_VARIABLE: value_from_deploy_job
  script:
    - echo "$BUILD_VARIABLE"  # Output is: 'value_from_build_job' due to precedence
  environment: production

This example and more details are found in the Pass an environment variable to another job section of the Variables Documentation.

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

Comments

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.