4 separate CircleCI configurations for setup, frontend testing, server operations, and merging

27 Views Asked by At

I have 4 separate CircleCI configurations for setup, frontend testing, server operations, and merging. The push-to-repo config determines which downstream jobs to run based on detected changes in frontend and server folders. I aim to trigger e2e tests, container building/publishing, and code promotion to main based on specific scenarios:

  • Frontend changes only: Run e2e tests, push to main if successful.
  • Server changes only: Build/publish docker image, push to main (order doesn't matter).
  • Both frontend and server changes: Run e2e tests, then container build/publish and push to main(in this case i want it to run the publish to main only when e2e test is a success).

this is the main config file it checks if there are changes in frontend and server folder and merges the config files based on that. and i use the continuation-config orb to run the next config

version: 2.1
setup: true

orbs:
  the-great:
    orbs:
      continuation: circleci/[email protected]

    commands:
      frontend-and-server-diff:
        steps:
          - checkout

          - run:
              name: Fetch branches
              command: |
                git fetch origin
                git branch -a
                git checkout main

          - run:
              name: Frontend diff
              command: |
                if git diff --quiet main..main-test-branch -- ./app; then 
                  echo "No changes in specified folder."
                else 
                  echo "frontend" >> ./modules.txt
                  echo 'export CHANGES_DETECTED=true' >> "$BASH_ENV"
                  echo "Changes detected continuing with build";
                fi

          - run:
              name: Server diff
              command: |
                if git diff --quiet main..main-test-branch -- ./server; then 
                  echo "No changes in specified folder."
                else 
                  echo "server" >> ./modules.txt
                  echo 'export CHANGES_DETECTED=true' >> "$BASH_ENV"
                  echo "Changes detected continuing with build";
                fi

          - run:
              name: add merge file
              command: echo $CHANGES_DETECTED

          - run:
              name: add merge file
              command: |
                if [[ $CHANGES_DETECTED == true ]] ; then 
                  echo "push" >> ./modules.txt
                else 
                  echo "no merging" 
                fi

          - store_artifacts:
              path: ./modules.txt

      merge-configs:
        parameters:
          modules:
            type: string
            default: ./modules.txt
          frontend-config-loc:
            type: string
            default: ./frontend-config.yml
          server-config-loc:
            type: string
            default: ./server-config.yml
          merge-config-loc:
            type: string
            default: ./push-config.yml
          continue-config:
            type: string
            default: .circleci/continue-config.yml
        steps:
          - checkout

          - run:
              name: Merge configs
              command: |
                # If `modules` is unavailable, stop this job without continuation
                if [ ! -f "<< parameters.modules >>" ] || [ ! -s "<< parameters.modules >>" ]
                then
                  echo 'Nothing to merge. Halting the job.'
                  circleci-agent step halt
                  exit
                fi

                # Convert a list of dirs to a list of config.yml
                sed -i -e 's/^/.circleci\//; s/$/-config.yml/g' "<< parameters.modules >>"

                xargs -a "<< parameters.modules >>" yq -y -s 'reduce .[] as $item ({}; . * $item)' | tee "<< parameters.continue-config >>"

          - store_artifacts:
              path: .circleci/continue-config.yml

    jobs:
      the-greatest-showman:
        docker:
          - image: cimg/python:3.9
        steps:
          - checkout
          - run:
              name: Install yq
              command: pip install yq
          - frontend-and-server-diff
          - merge-configs
          - continuation/continue:
              configuration_path: .circleci/continue-config.yml

workflows:
  the-greatest:
    jobs:
      - the-great/the-great-diff:
          filters:
            branches:
              only:
                - main-test-branch

the frontend config file that has e2e-tests job

and a job called create-docker-image-and-publish-to-dockerhub in the server folder

and the final config has a job publish-to-main-branch

I can't figure out how to process the push-to-repo job when changes are in the frontend folder.

0

There are 0 best solutions below