circlecicircleci-workflows

How to use value of variable from one job to another


Objective: Trying to pass one step variable in Job A to Job B in CircleCI or if possible to create an environment variable programmatically to a CircleCI context

What I have done so far:

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

But I read documentation that each "run" runs on own shell and, I saw export to bash as a work around, but I am using windows executor… so it won't 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

Can you suggest how to achieve this or how to create environment variable during build (not through UI manually with this $key so that I refer to that in next job in workflow

Update:

I have used below but getting parsing error:

  name: "Create Context"
  command:
    curl --request POST \
    --url https://circleci.com/api/v2/context \
    --header 'authorization: Basic xxxxxxxxxxxxxxxxxxxxxxxxxx' \
    --header 'content-type:' 'application/json' \
    --data '{"name":"string","owner":{"id":"string","type":"organization"}}'

Error:

Unable to parse YAML
mapping values are not allowed here
 in 'string', line 23, column 36:
                --header 'authorization: xxxxxxxxxxxxxxx ... 

Solution

  • You could use the CircleCI API v2: https://circleci.com/docs/api/v2/#operation/addEnvironmentVariableToContext