DEV Community

loading...

Exit CircleCI jobs if changed files do not match specific pattern

Kay Gosho
A Full Stack Developer. CTO of Remotehour ( https://remotehour.com )
・1 min read

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

Discussion (0)