Skip to content

Why does my Amplify monorepo build trigger on every commit even though frontend folder has no changes

0

I have a monorepo structure with three main folders:

frontend (Next.js app), cdk (AWS CDK Python code), backend (AWS Lambda functions) I’m using AWS Amplify to host only the Next.js app, and I’ve set up the following:

Monorepo Settings:

appRoot in the build spec is set to frontend. The environment variable AMPLIFY_MONOREPO_APP_ROOT is set to frontend (visible in the Amplify Console for all branches). Build Specification (in Amplify console):

version: "1.0"
applications:
  - appRoot: frontend
    frontend:
      phases:
        preBuild:
          commands:
            - npm install
        build:
          commands:
            - npm run build
      artifacts:
        baseDirectory: .next
        files:
          - "**/*"
      cache:
        paths:
          - node_modules/**/*

Despite verifying that no changes are made to frontend (for example, when I only commit changes to the cdk folder), Amplify still triggers a full build and deploy for every commit. When I run:

git diff --name-only HEAD~1 HEAD

I can confirm the changed files are only in cdk/lib/... or other folders, not in frontend. I have also:

Ensured there are no rebase merges or force pushes. Checked that no amplify.yml file exists in the repo root that might override my build spec. Confirmed that the environment variables at the branch level also show AMPLIFY_MONOREPO_APP_ROOT=frontend. Why is Amplify still building the Next.js app on every commit instead of skipping when there’s no change in frontend folder? Is there a way to force Amplify to only build when actual changes occur in the designated monorepo root?

asked 2 years ago564 views

1 Answer
0

The issue you're experiencing with AWS Amplify triggering builds for your monorepo on every commit, even when there are no changes in the frontend folder, is likely due to how Amplify handles monorepo configurations by default.

While you've correctly set up the monorepo settings with the appRoot and AMPLIFY_MONOREPO_APP_ROOT pointing to the frontend folder, Amplify's default behavior is to trigger builds for all commits to the connected branch, regardless of which files have changed.

To address this issue and make Amplify more selective about when it triggers builds for your monorepo, you can implement a custom build trigger using AWS CodeBuild in combination with Amazon EventBridge (formerly CloudWatch Events). This approach allows you to create a more granular rule that only initiates builds when changes occur in the frontend directory.

Here's a high-level overview of how you can set this up:

  1. Create an Amazon EventBridge rule that listens for CodeCommit repository state changes.
  2. Set up a Lambda function that checks which files have changed in the commit.
  3. If changes are detected in the frontend folder, the Lambda function can trigger the Amplify build using the AWS SDK.

This custom solution gives you more control over when builds are triggered, ensuring that Amplify only rebuilds and redeploys your Next.js app when there are actual changes in the frontend directory.

It's worth noting that while Amplify's CI/CD works well with monorepos, including support for tools like Nx and Yarn Workspaces, the default behavior is to build on every commit. The custom approach described above allows you to fine-tune this behavior to better suit your specific monorepo structure and build requirements.

Remember to implement appropriate security measures and follow AWS best practices when setting up this custom build trigger solution.
Sources
Introducing the Next Generation of AWS Amplify’s Fullstack Development Experience | Front-End Web & Mobile
Trigger AWS CodeBuild builds automatically - AWS CodeBuild

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.