CircleCI Field Guide
GitHub Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Parameters as Envars

Parameters as Environment Variables

CirleCI allowed parameters types include string and env_var_name which are both suitable to pass parameters intended to be used as environment variables, but serve different use cases.

Use Case: Passing values from a Dynamic Config (a.k.a. Setup Job) to downstream pipeline

Use Case: Capturing values passed from API or UI Trigger.

Use Case: Change the lookup name of variables used in job (i.e. reassign ACCESS_KEY_WEST, ACCESS_KEY_EAST)

Configuration (.circleci/config.yml)

This configuration consumes the parameter and declares it an Environment Variable in the job.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
version: 2.1

parameters:
  circle_pr_number:
    type: string
    description: Will be provided by setup job
    default: "" #empty on not PR jobs
  circle_pr_reponame:
    type: string
    description: Will be provided by setup job
    default: "" #empty on not PR jobs
  circle_pr_username:
    type: string
    description: Will be provided by setup job
    default: "" #empty on not PR jobs
  circle_pull_request:
    type: string
    description: Will be provided by setup job
    default: "" #empty on not PR jobs

# Single job will echos values
# In continue job they will be parameters
jobs:
  build:
    docker:
      - image: cimg/base:stable
    resource_class: small
    steps:
      - checkout
      - run: |
          # Just yse parameter anywhere in config
          echo "The PR # is << pipeline.parameters.circle_pr_number >>"

          # OR Set ENV VAR to value (for use in scripts, other commands)
          export CIRCLE_PR_NUM=<< pipeline.parameters.circle_pr_number >>

          # OR Save to $BASH_ENV to let subsequent steps use env var
          echo "export CIRCLE_PR_NUM=<< pipeline.parameters.circle_pr_number >>"   >> $BASH_ENV