Search code examples
npmgrepcontinuous-integrationbitbucket-pipelinesmadge

Checking for circular dependencies with madge in bitbucket CI


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


Solution

  • 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