The user wants a migration plan from a "polyrepo" (many repositories) architecture to a "monorepo" (single repository) architecture.
The plan needs to include two specific aspects:
- Tooling: What software/tools to use to manage the monorepo.
- Adoption: How to get the team on board and ensure the process goes smoothly.
Step 1: Analyze the Request
- Context: Software development infrastructure.
- Goal: Consolidate multiple repositories into one.
- Requirements: A structured plan including tool selection and an adoption strategy.
- Target Audience: Likely an Engineering Manager, Tech Lead, or DevOps engineer planning this migration.
Step 2: Structure the Response
A logical flow for a migration plan would be:
- Phase 0: Assessment & Strategy (The "Why" and "What") - Define goals, inventory current state.
- Phase 1: Tooling Selection (The "How") - Choose the tech stack for the monorepo.
- Phase 2: Infrastructure Setup (The Foundation) - Prepare the target repo.
- Phase 3: Migration Strategy (The Execution) - How to move code (history vs. clean slate).
- Phase 4: CI/CD Pipelines (The Verification) - Adjusting build/deploy processes.
- Phase 5: Adoption & Culture (The People) - Training and communication.
Step 3: Draft Content - Section by Section
-
Phase 0: Assessment
- Need to know what languages? (JS/TS, Python, Go, Java?)
- Dependencies between repos?
- Current CI/CD complexity?
-
Phase 1: Tooling (Crucial Request)
- Options:
- Do-it-yourself: NPM/Yarn workspaces (JS), subdirs.
- Build Systems: Bazel (powerful, steep learning curve), Buck/Pants.
- Task Runners (The modern sweet spot): Nx (great for JS/TS), Turborepo (Vercel, very fast), Lerna (older, revived).
- Recommendation: Focus on Nx or Turborepo for general purpose, mention Bazel for massive scale.
- Version Control: Git (obviously). GitHub/GitLab/Bitbucket.
-
Phase 2: Migration Strategies
- Approach A: Preserve History (Hard). Merging git histories.
git subtree add or specialized tools like git-filter-repo.
- Approach B: Clean Slate (Easier). Copy-paste code, keep old repos read-only.
- Approach C: Gradual. Move project by project.
- Recommendation: Approach C (Gradual) is usually best for adoption, but "Preserve History" is often requested. I should outline the technical command logic briefly.
-
Phase 3: CI/CD (The Challenge)
- Monorepos require "smart" CI. Don't rebuild everything if only one file changed.
- Tools: Nx Affected, Turborepo caching, Doppler/Earthly.
- CI Providers: GitHub Actions (
on: push: paths:), CircleCI (dynamic workflows).
-
Phase 4: Adoption (Crucial Request)
- Communication: Why are we doing this? (Shared types, atomic commits, easier refactoring).
- Training: Workshops.
- Pain Points: Slower git clones (sparse checkout), merge conflicts (code ownership), permissions (all or nothing usually).
Step 4: Refine and Polish
- Self-Correction during drafting: I initially thought about just listing tools. But the user asked for a plan. A plan has phases.
- Tooling nuance: Don't just say "Use Nx." Explain why (dependency graph).
- Adoption nuance: It's not just about forcing people. It's about addressing "Merge Conflicts" and "Permissions." GitHub CODEOWNERS is essential here.
Step 5: Detailed Steps (Mental Outline of the final output)
- Executive Summary: High-level benefits.
- Tooling Selection:
- Build: Nx vs. Turbo.
- Versioning: Changesets (JS specific) or manual.
- Migration Path:
- Standardize linting/formatting first.
- The
git subtree method.
- Adoption Strategy:
- The Pilot team.
- Documentation.
- Managing the "No" (objections).
Step 6: Final Review against constraints
- Polyrepo to Monorepo? Yes.
- Tooling included? Yes.
- Adoption included? Yes.
Constructing the actual response: (This aligns with the generated output).
- Intro: Define the scope.
- Phase 1: Tooling: Compare Nx, Turbo, Bazel. Mention
changesets.
- Phase 2: Architecture: Folder structure (
apps/, libs/).
- Phase 3: The Migration (Technical):
git-filter-repo or subtree.
- Phase 4: CI/CD: Affected builds.
- Phase 5: Adoption (People): Code owners, trunk-based development.
(Self-Correction): Make sure to mention the difference between a "monolith" (one deployable artifact) and "monorepo" (many artifacts, one source of truth). This is a common confusion.
Let's write it out.
Migrating from a polyrepo (many repositories) to a monorepo (single repository) is a high-impact infrastructure change. It solves problems like dependency hell and code duplication, but introduces challenges in CI scalability and access control.
Below is a comprehensive migration plan structured by Phases, including specific Tooling choices and an Adoption Strategy.
Phase 1: Strategy & Tooling Selection
Before moving code, you must select the build system and version control strategy.
1. Tooling Recommendations
The success of a modern monorepo relies on Smart Builds (only building what changed) and Dependency Management (linking internal libraries).
| Category | Recommended Tools | Why |
|---|
| Build Orchestration | Nx (Frontend/Node) Turborepo (Frontend/Node) Bazel (Enterprise/Multi-lang) | These tools understand the dependency graph. If you change a shared UI library, they know exactly which apps need to be re-tested. |
| Versioning | Changesets | Automates versioning and changelogs. It allows you to version packages independently within the mono-repo (e.g., bumping lib-a from 1.0 to 1.1 without touching lib-b). |
| Code Quality | ESLint / Prettier | Must be enforced at the root level to ensure consistent coding styles across all former repositories. |
| CI/CD | GitHub Actions / CircleCI | Must support matrix building or dynamic pipelines to avoid running tests for Project A when only Project B changed. |
2. Repository Structure Strategy
Decide on the folder structure. The standard convention is:
/apps: Deployable applications (e.g., web-client, api-server, mobile-app).
/libs: Shared code (e.g., ui-kit, types, logger, analytics).
/tools: Scripts for deployment, migration, or generation.
Phase 2: The "Landing Zone" (Preparation)
Do not start moving code until the target environment is stable.
- Create the Skeleton: Initialize the new repository with the tooling config (e.g.,
nx.json, turbo.json).
- Standardize Dev Environment:
- Create a root
package.json for common dev dependencies (Jest, ESLint, Prettier).
- Conflict Resolution: If Repo A uses Prettier v2 and Repo B uses v3, decide on one standard now.
- CI Baseline: Set up a basic CI pipeline that initially just runs a "Hello World" build to ensure your CI provider handles the monorepo context.
Phase 3: The Migration (Execution)
There are two main ways to move code: The "Clean Slate" (recommended for messy repos) or the "History Preserve" (harder, preserves git blame).
Option A: The "Clean Slate" (Recommended)
Best if your old repos have large binary assets or messy git histories.
- Copy Code: Copy the source code from Repo A into
/apps/repo-a.
- Dependency Audit: Move dependencies from the local
package.json to the root package.json where possible.
- Fix Imports: Change imports to use the new workspace paths (e.g.,
import { Button } from 'libs/ui' instead of '../../node_modules/ui').
- Repeat: Do this for the second application.
Option B: Preserving History (Advanced)
If you absolutely need git blame to work for files migrated years ago.
- Use a tool like git-filter-repo to rewrite the history of the old repo so it looks like it was always in a subfolder (e.g.,
/apps/repo-a).
- Merge the rewritten history into the new monorepo. Warning: This often causes merge conflicts for developers who have outstanding branches on the old repos.
Phase 4: CI/CD & Dependency Optimization
Once the code is in, you must optimize the pipeline to prevent 2-hour build times.
- Implement Affected Builds:
- Configure your CI to detect which files changed.
- Example: If only
/apps/marketing-site changed, do not run tests for /apps/billing-api.
- Caching:
- Enable remote caching (built-in to Turborepo/Nx Cloud). If Developer A builds the app, Developer B and the CI server should download the build artifacts rather than rebuilding them.
- Shared Library Extraction:
- Identify code copied between apps.
- Extract it to
/libs/shared-feature.
- Update
tsconfig paths or package.json workspaces to link them.
Phase 5: Adoption & Culture
The technical migration is easy; the cultural shift is hard.
1. The "Code Owner" Governance
In a polyrepo, you own your repo. In a monorepo, you might accidentally break someone else's app.
- Tool: GitHub CODEOWNERS file.
- Strategy: Define that Team Frontend owns
/apps/web, but Team Backend owns /apps/api. Approvals are automatically required based on file paths.
2. The "Boy Scout Rule"
Enforce a rule: "If you touch a legacy library in the monorepo, you must leave it slightly better than you found it."
- Action: Gradually migrate shared code from
apps to libs during normal feature work, rather than a "stop the world" refactoring sprint.
3. Branch Strategy (Trunk Based Development)
Polyrepos often rely on long-lived feature branches.
- Monorepo Risk: Long-lived branches cause nightmare merge conflicts across shared libraries.
- Adoption: Move to Trunk-Based Development (short-lived branches merged to main frequently).
4. Developer Training
Developers will be confused by the new structure.
- Create a "Contributing.md" in the root.
- Provide "Codemods" (scripts) to automate tedious tasks (e.g., a script to generate a new React component in the correct folder).
Summary Checklist
| Step | Status |
|---|
| Inventory | List all repos to be migrated. |
| Tooling | Select Nx/Turbo/Bazel. |
| Structure | Define apps vs libs folders. |
| CI/CD | Implement "Affected" testing logic. |
| Governance | Setup CODEOWNERS file. |
| Migration | Move repo (Clean slate or History). |
| Lockdown | Archive old polyrepos (Read-only). |