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

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay