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

Dynamic Config Pr Vars

Passing PR Info in Dyanmic Config

If using Dynamic Config, you will realize that some PR based variables we capture for you (CIRCLE_PR_NUMBER, etc) are present in the initial setup job, but not the continue job.

This gist shows how to capture & pass those values as parameters to the continue workflow as well!

from: @aaronstillwell

Setup Workflow (config.yml)

The setup is a single job that grabs these values and passes them as JSON to next workflow.

Your job would also do additional logiv here like which modules to build, jobs to exclude, etc.

 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
40
41
42
43
44
version: 2.1

# Mark config as setup, and declare continuation orb
setup: true 
orbs:
  continuation: circleci/continuation@0.1.2

# Single job will pull current vars, and pass as JSON to continue job.
# TIP: A single job named 'build' does not need a `workflows` stanza!
jobs:
  build:
    docker:
      - image: cimg/base:stable
    resource_class: small
    steps:
      - checkout

      # Run steps can be any shell - including PYTHON!! 
      # Use python to parse list of desired values, and create JSON payload.
      - run:
          name: generate json
          shell: /usr/bin/env python3
          command: |+
            import os
            import json
            env_var_to_save = [
              "CIRCLE_PR_NUMBER", 
              "CIRCLE_PR_REPONAME",
              "CIRCLE_PR_USERNAME",
              "CIRCLE_PULL_REQUEST"
            ]
            pipeline_vars = {}
            for var in env_var_to_save:
              if os.environ.get(var) != None:
                pipeline_vars[var.lower()] = os.environ[var]
              else:
                pipeline_vars[var.lower()] = ""
            pipeline_vars['IS_PR'] = len(pipeline_vars['circle_pull_request']) > 0
            with open('params.json', 'w') as fp:
              fp.write(json.dumps(pipeline_vars))            
      # Pass the generated JSON parameters and alternate config.yml to run
      - continuation/continue:
          configuration_path: config_continue.yml
          parameters: params.json

Continue Workflow (config_continue.yml)

This single continue job shows the various ways you can access the passed parameters. Note their declaration and default value of empty string. This allows not PR jobs to run when those values were not passed.

 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