DEV Community

Kay Gosho
Kay Gosho

Posted on

4 1

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

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)