Can reusable commands have output?

I have been struggling to figure out whether reusable commands can have an output.

In our monorepo, we are trying to implement a workflow where a service only gets deployed when they got changed. The logic is fairly simple: we run git diff $BASE_SHA $HEAD_SHA | grep '^service1/.*$', and if it has any output, then service1 will needs to be deployed. However, we’ve been struggling on implementing this in circleci.

So ideally, it would be something like this:

commands:
  check-diff:
    parameters:
      regex:
        type: string
    steps:
      - run:
        command: git diff $BASE_SHA $HEAD_SHA | grep <<parameters.regex>>
      - if run above has any result, output true, else output false

jobs:
  deploy-service1:
    steps:
      - check-diff:
        regex: '^service1/.*$'
      - when:
        condition:
          equal: [true, <<check-diff.output>>]
        steps:
           deploy-service-1

  deploy-service2:
    steps:
      - check-diff:
        regex: '^service2/.*$'
      - when:
        condition:
          equal: [true, <<check-diff.output>>]
        steps:
           deploy-service-2

Any help would be much appreciated, thank you!

Hi @dolphinigle how is it going!. Well, I have not seen anything like this but have you tried this orb CircleCI Developer Hub - circleci/path-filtering, it will check if a folder has changed. Also, you can check out this gist on how to use it Path filtering example · GitHub

1 Like