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

Dynamic Config Pr Labels

GitHub Label Controlled Workflows

You can use github labels to control CircleCI build behavior.

Explanation

With Dynamic Config we can inspect the incoming payload to understand if it is related to any open PRs, and then take special action based on the applied labels.

from: @ryanpedersen42

Example

Setup Workflow (config.yml)

The setup is a single job that calls GitHub for any labels, and uses some JQ (on all cimg convience images) to convert to CCI parameters.

Your job can also decide to ignore non-PR builds, or apply an alternate configuration file.

 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
45
46
47
48
49
50
51
52
53
54
version: 2.1

setup: true

jobs:
  generate-config:
    executor: base
    steps:
      - checkout
      - run: 
          name: check tags
          command: |
            #
            # Dynamic Config allows us to pass wholly distinct config files, depending on PR or non-PR flow.
            #
            if [[ -z ${CIRCLE_PULL_REQUEST} ]]; then
                echo "no PR is open, use normal config"
                FINAL_CONFIG_PATH=".circleci/continue.yml"
            else
                echo "Using PR pipeline. Call Github for current Labels:"
                FINAL_CONFIG_PATH=".circleci/continue_pr.yml"
                RESPONSE=`curl \
                          -H "Accept: application/vnd.github+json" \
                          -H "Authorization: Bearer $GH_TOKEN" \
                          https://api.github.com/repos/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/issues/${CIRCLE_PULL_REQUEST##*/}/labels` | tee /tmp/response.txt
                LABELS=`jq -c '.[] /tmp/response'
                [ "$LABELS" ] || echo "No Labels defined"
            fi

            echo $LABELS | jq -s  --arg continuation "$CIRCLE_CONTINUATION_KEY" \
                --arg config "$(cat $FINAL_CONFIG_PATH)" \
                '{"continuation-key": $continuation, 
                "configuration": $config,
                
                "parameters": (if . == null then empty else ( map({ (.[].name): true}) | add ) end)  
                } | with_entries( select( .value != null ) )  ' | tee /tmp/continue-body.json
                                    
      - run: 
          name: continue API call
          command: |
            curl --request POST \
              --url https://circleci.com/api/v2/pipeline/continue \
              --header 'content-type: application/json' \
              --data-binary @/tmp/continue-body.json            

workflows:
  setup-workflow:
    jobs:
      - generate-config

executors:
  base:
    docker:
      - image: cimg/base:stable

Continue Workflow - PR Only (continue_pr.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
version: 2.1

workflows:
  all_labels:
    jobs:
      - always_run
      
  bug_workflow:
    when: << pipeline.parameters.bug >>
    jobs:
      - some_bug_job
      - always_run
  
  enhancement_workfow:
    when: << pipeline.parameters.enhancement >>
    jobs:
      - some_enhancement_job

jobs:
  some_bug_job:
    executor: base
    steps:
      - checkout
  some_enhancement_job:
    executor: base
    steps:
      - checkout
  always_run:
    executor: base
    steps:
      - checkout

executors:
  base:
    docker:
      - image: cimg/base:stable

#
# The list of possible (finite) GH Tags is defined here as booleans
#
parameters:
  bug:
    type: boolean
    default: false
  enhancement:
    type: boolean
    default: false
  documentation:
    type: boolean
    default: false 
  help wanted:
    type: boolean
    default: false
  good first issue:
    type: boolean
    default: false
  invalid:
    type: boolean
    default: false
  question:
    type: boolean
    default: false 
  wontfix:
    type: boolean
    default: false

Continue Workflow - Non PR (all pushes) (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
version: 2.1

workflows:
  all_commits:
    jobs:
      - always_run

executors:
  base:
    docker:
      - image: cimg/base:stable

jobs:
  always_run:
    docker:
      - image: cimg/base
    resource_class: small
    steps:
      - checkout