0

I want to add a step to check for circular dependencies in our bitbucket pipeline, but I'm struggling to come up with a good way to grab the output to throw an error or not

Here are the bitbucket steps I've tried

        - step:
            name: Check Circular Dependencies
            caches:
              - node
            image: node:20.10.0
            script:
              - cd dependency-check
              - npm install
              - cd ..
              - OUTPUT=$(npx madge --circular harmonee-ui/src/App.tsx)
              - echo "$OUTPUT"
              - if echo "$OUTPUT" | grep -q "No circular dependency found!"; then echo "No circular dependencies found"; else echo "Circular dependencies found!" && exit 1; fi

And I also tried putting the output into a txt and grepping that

        - step:
            name: Check Circular Dependencies
            caches:
              - node
            image: node:20.10.0
            script:
              - cd dependency-check
              - npm install
              - cd ..
              - npx madge --circular harmonee-ui/src/App.tsx > dependency_check.txt
              - cat dependency_check.txt
              - if grep -q "✔ No circular dependency found!" dependency_check.txt; then echo "No circular dependencies found"; else echo "Circular dependencies found!" && exit 1; fi

Madge is working as expected, but I can't seem to grep the output easily in the pipeline. Any ideas on what I'm going wrong here and how to fix it? Better ways to test for circular dependencies in CI are also welcome

2
  • 1
    Get what you want to do working in a shell script first and THEN add the additional complexity of calling it from that script: block. It looks to me like the ONLY thing that matters in your example is the if ... grep... last line, nothing above it is relevant to the problem you're asking for help to solve, so just try to get that working on input that looks like whatever npx madge --circular harmonee-ui/src/App.tsx outputs and if you run into a problem with that then ask a question about just that.
    – Ed Morton
    Commented Jun 3 at 10:39
  • Fair enough, this is reasonable advice. I didn't phrase my question quite correctly. It would have helped to mention I was having problems saving the output of the madge script to a txt file. The grep command was actually not really the issue.
    – maximosis
    Commented Jun 5 at 3:47

1 Answer 1

0

I figured out a solution. I used the tee command to save the output and print it to the terminal. I also added some flags to the madge script to clean up the output a bit

- npx madge --circular --no-spinner --no-color harmonee-ui/src/App.tsx 2>&1| tee dependency_check.txt
- echo "Checking for circular dependencies..."
- if grep -q "Found" dependency_check.txt; then cat dependency_check.txt && exit 1; else echo "No circular dependencies found"; fi

Not the answer you're looking for? Browse other questions tagged or ask your own question.