Monorepo and executing jobs related to changed files

Hi! I’m still a newbie in ci/cd world and circleci in particular.
I have a repo with one service and I’m using the following configuration.

  • With all PRs the jobs build and test run.
  • When merging code in master, build, test and deploy.

Here is the configuration file.

version: 2.1

executors:
  custom-executor:
    docker:
      - image: circleci/node:lts

jobs:
  build:
    executor: custom-executor
    steps:
      - checkout
      - restore_cache:
          key: dependency-cache-{{ checksum "package-lock.json" }}
      - run: 
          name: Installing packages
          command: |
            npm install
      - save_cache:
          key: dependency-cache-{{ checksum "package-lock.json" }}
          paths:
            - node_modules
      - persist_to_workspace:
          root: /workspace
          paths: 
            - node_modules

  test:
    executor: custom-executor
    steps:
      - checkout
      - attach_workspace:
          at: /workspace
      - run:
          name: Unit Test
          command: npm run test

  deploy:
    executor: custom-executor
    steps:
      - checkout
      - attach_workspace:
          at: /workspace
      - run:
          name: Serverless
          command: | 
            ./node_modules/.bin/serverless deploy

workflows:
  version: 2
  build_test_deploy:
    jobs:
      - build
      - test:
          requires:
            - build
      - deploy:
          requires: 
            - build
            - test
          filters:
            branches:
              only: master

Now, I would like to migrate to a monorepo with the following structure:

|- main-folder
|-- application1
|-- application2

At a high level, changes can happen in main-folder and|or in any of the applications folder.
The problem: I just want to run the jobs for the folders where code changed.
Constraints:

  1. PRs should build and test just the service affected. If I change something in application2, jobs should not run for main-folder or application1, just for application2.
  2. If I change something in main-folder, it is opk if all jobs run, since we can presume it could impact application1 and application2.
  3. Some for the deploy job: it should execute just for the service that changed.

I don’t know if I am being clear. It is difficult to explain something that one does not know well enough.

Is there a way to identify through the files that changed which service or application should run particular jobs?

I will appreciate help and advice.