DEV Community

Kay Gosho
Kay Gosho

Posted on

Exit CircleCI jobs if changed files do not match specific pattern

As of Circle CI 2.1, we can define custom commands and new CircleCI CLI.

I found the following code is useful to exit CircleCI jobs when some specific file/directory was changed in the latest commit:

# .circleci/config.yml

commands:
  check-changed-files-or-halt:
    parameters:
      pattern:
        type: string
    steps:
      - run: git show -m HEAD --name-only --pretty="" | egrep -q '<< parameters.pattern >>' || circleci step halt

jobs:
  test:
    executor: node
    steps:
      - attach-workspace
      - check-changed-files-or-halt:
          pattern: ^(src|test)|(.js|ts)$
      - run: yarn install
      - run: yarn test
Enter fullscreen mode Exit fullscreen mode

However, the command git show -m HEAD --name-only --pretty="" only shows the latest commit's change, so use with merging Pull Request with tasks such as deployment.

Ref: https://discuss.circleci.com/t/ability-to-return-successfully-from-a-job-before-completing-all-the-next-steps/12969/7

Top comments (0)