Dynamically set parameter values of matrix

Sure. It looks like this:

I have one “base” config: config.yml, where I used the continuation orb.

  1. I calculate the versions I need
  2. I save those in an env var and pass it into a bash script (can be any script)
  3. I use the continuation orb to specify the next config.
  • As you notice, I leave parameters with the default empty { } as my target versions are in the format of "[“v1.0.0”, “v1.2.1”, “v.1.3.1”] and parameters accepts only a JSON object. It didn’t make sense for me to make it a JSON object for this and it was easier to setup. (if you know a smarter way let me know!)
# this allows us to use CircleCI's dynamic configuration feature
setup: true

orbs:
  continuation: circleci/continuation@0.3.1

workflows:
  setup:
    jobs:
      - setup

jobs:
  setup:
    <<: *defaults
    resource_class: small
    steps:
      - checkout
      - run:
          name: calculate versions
          command: |
            # mock code to calculate TARGET_VERSIONS
            echo "export TARGET_VERSIONS='$TARGET_VERSIONS'" >> "$BASH_ENV"
      - run:
          name: Generate Pipeline continue_config.yml file
          command: |
            ./scripts/ci/generate_test_config.sh ${TARGET_VERSIONS}
      - continuation/continue:
          configuration_path: .circleci/continue_config.yml
          parameters: '{}'

Then in the continue_config.yml config I have everything I need, and in the place of the matrix, I have left an empty but it doesn’t matter what you put there as it will be replaced by the script:

workflows:
  test:
    jobs:
      - test:
          matrix:
            parameters:
              #              This value will be replaced by the generate_test_config.sh script in the CI
              target-version: [ ]
jobs:
  test:
    resource_class: medium
    parameters:
      target-version:
        type: string
    steps:
      ....

now in the bash script itself. you can do anything, I just used yq to edit the one line I needed in place:

#!/usr/bin/env bash
set -o errexit -o pipefail

target_versions="$TARGET_VERSIONS"

echo "Installing yq..."
curl -s -L -o yq "https://github.com/mikefarah/yq/releases/download/v4.31.2/yq_linux_amd64"
chmod +x ./yq
mkdir -p "${HOME}/.local/bin"
mv ./yq "${HOME}/.local/bin/"

# We use yq to replace the target-version key, this is the only edit in place we need
yq e ".workflows.test.jobs[0].test.matrix.parameters.\"target-version\" = $target_versions" -i .circleci/continue_config.yml

# (optionally install aws cli tool too and validate the config you generated)
# circleci config validate .circleci/continue_config.yml