version: 2.1

setup: true

orbs:
    continuation: circleci/continuation@0.2.0
    path-filtering: circleci/path-filtering@0.1.3

jobs:
    merge_config:
        executor: continuation/default
        steps:
            - checkout
            - run:
                name: Install yq (v4)
                command: |
                  wget https://github.com/mikefarah/yq/releases/download/v4.27.3/yq_linux_amd64
                  sudo mv yq_linux_amd64 /usr/local/bin/yq
                  echo "93c1f7fdbc86b3fba453164703e2eb90d3760344  /usr/local/bin/yq" > /tmp/checksum
                  sha1sum -c /tmp/checksum
                  sudo chmod +x /usr/local/bin/yq
            - run:
                  name: Merge all files
                  command: |
                      yq eval-all '. as $item ireduce ({}; . *+ $item )' .circleci/*/*.yml .circleci/*/*/*.yml .circleci/*/*/*/*.yml > .circleci/merged_config.yml
                      cat .circleci/merged_config.yml
            -   path-filtering/set-parameters:
                    base-revision: master
                    mapping: |
                        upgrades/.* run-database-tests true
                    output-path: /tmp/pipeline-parameters.json
            -   run:
                    name: Debug parameters
                    command: cat /tmp/pipeline-parameters.json
            -   continuation/continue:
                    configuration_path: .circleci/merged_config.yml
                    parameters: /tmp/pipeline-parameters.json

workflows:
    version: 2

    merge_config_workflow:
        jobs:
            - merge_config
