Andrew Metelkin
Andrew Metelkin

Reputation: 21

How can I pass variable from gitlab script

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?

Upvotes: 0

Views: 1323

Answers (1)

Adam Marshall
Adam Marshall

Reputation: 7649

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.

Upvotes: 1

Related Questions