How to use value of variable from one job to another?

Hi,

I have config like below, but as part of step I need to store one string value in step and use it in next job.

but I read documentation that each run runs on own shell , I saw export to bash as work around but I am using windows executor… so it wont be option

version: 2.1
jobs:
  self-hosted-agent-test:
    machine: true
    resource_class: xxxxxxx/devops-self-hosted-agent
    
    steps:
      - checkout
      - run:           
          command: az login --service-principal -u $xxxxxx -p $xxxxx --tenant $xxxx
      - run:           
          command: az group create --location $xxxxx --name $xxxxxxxx
      - run:           
          command: az storage account create --name $xxxxxxxx --resource-group $xxxxxxx --location $xxxxx --sku Standard_LRS
      - run:          
          command: az storage container create --name xxxxxx --account-name $xxxxxxxx
      - run:          
          command: Connect-AzAccount --service-principal -u $xxx -p $xxx --tenant $xxxx
      - run: 
          shell: powershell.exe
          command: $key=(Get-AzStorageAccountKey -ResourceGroupName $xxxxxxxxx -AccountName $xxxxxxx)[0].Value

  self-hosted-agent-test1:
    machine: true
    resource_class: xxxxxxx/devops-self-hosted-agent    
    steps:
      - checkout
      - run: 
          name: Storage key persistance check
          command: Write-Host $key   
workflows:
  my-workflow:
    jobs:
      - self-hosted-agent-test:
          context:
            - abcd
      - self-hosted-agent-test1

Now I need to use $key value in job self-hosted-agent-job1 but its not working , in fact i wanted that job 1 $key should be added as env variable in existing context named “abcd” but i read somewhere that is not possible

Please suggest.

You can create a text file in the shared workspace and then source that file in other jobs that have an attach_workspace step. Well, in a POSIX shell (like bash) you use source. In Powershell, you need to look up what the equivalent is.

Something like this:

jobs:
  job1:
    steps:
      - run:
          command: |
            export MY_VAR1="some string"
            export MY_VAR2="some other string"
            mkdir -p /tmp/workspace
            echo "export MY_VAR1=\"$MY_VAR1\"" >> /tmp/workspace/env_vars
            echo "export MY_VAR2=\"$MY_VAR2\"" >> /tmp/workspace/env_vars
      - persist_to_workspace:
          root: /tmp/workspace
          paths:
            - /tmp/workspace/env_vars

  job2:
    steps:
      - attach_workspace:
          at: /tmp/workspace
      - run:
          command: |
            source /tmp/workspace/env_vars
            echo "$MY_VAR1"
            echo "$MY_VAR2"

Thank you Let me give a try

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.