How to deploy only changed services in a monorepo
Use separate Docker Compose build contexts so a frontend change can ship without redeploying the API. Rebuild both when they share build inputs.
A frontend change should not force a new API build. On Gregale, production Docker Compose project deploys compare the source for each service's declared Docker build context. Give independent services separate contexts. If both builds need a shared package, include that package in both contexts so neither service is left running stale code.
| Change | Build contexts | Expected production deploy |
|---|---|---|
| No source or configuration changes | Separate service directories | Neither service |
Edit a file only in frontend/ | ./frontend and ./api | Frontend only |
| Edit a shared file both Dockerfiles copy | Repository root for both services | Both services |
Edit a file only in frontend/ | Repository root for both services, including the edited file | Both services, even if only the frontend Dockerfile copies it |
Start with independent build contexts
Download the runnable Compose starter, extract it into a fresh directory, and run the commands from there with a disposable Gregale workspace. The example creates two public HTTP services that return synthetic version markers only. Don't put secrets in the build tree or reuse an existing project's name.
services:
frontend:
build: ./frontend
ports: ['8080:8080']
api:
build: ./api
ports: ['8081:8080']
Pick a new project name. Inspect the dry run and confirm that it plans exactly two new services, not updates to existing apps. Then deploy and wait for both services to go live.
PROJECT="demo-mono-$(openssl rand -hex 6)"
gregale deploy --project --name "$PROJECT" --source=worktree --dry-run
gregale deploy --project --name "$PROJECT" --source=worktree --yes
Keep plan files, deploy receipts and logs outside the uploaded worktree. A redirection such as > plan.json creates the file before the CLI scans the source. If that file lands in an uploaded build context, the next deploy may no longer be a no-op. For example, capture the unchanged plan in a private directory outside this demo:
RECEIPTS="$(mktemp -d /tmp/gregale-monorepo-receipts.XXXXXXXX)"
gregale --json deploy --project --name "$PROJECT" --source=worktree --dry-run \
> "$RECEIPTS/unchanged-plan.json"
Record the full IDs and slugs of the two created apps and their deployments. Use the actual app slugs from the apply receipt. For example, if the created slugs are the project name plus the service name:
gregale release list --app "$PROJECT-frontend" --limit 10
gregale release list --app "$PROJECT-api" --limit 10
The first deploy after a change to source hashing can refresh saved hashes once on an existing project. Start with a fresh project for this drill.
Check the no-op and frontend-only change
Repeat the dry run without editing any files. The expected plan is no deployments. If you apply that same unchanged source, the deployment history of both apps should stay unchanged.
Now edit just the frontend's version marker:
printf 'frontend-v2\n' > frontend/revision.txt
gregale deploy --project --name "$PROJECT" --source=worktree --dry-run
The frontend should be the only service selected. Apply with the same deploy command but without --dry-run, then check that the frontend has a new deployment ID and the API's newest deployment ID is unchanged. The Deployments API lists revisions for a single app. The starter's JSON at /ready identifies the running service and its revision. A deployment can be live at 1% traffic. Wait for the new release to reach 100% before expecting the stable URL to consistently show the new marker. Don't infer that the API was skipped solely from a fast build or from its URL still working.
Rebuild both services when shared code changes
A Dockerfile with context: ./frontend cannot copy files from the sibling shared/ directory. Extract a second, fresh copy of the starter into a different directory, change into that directory, and pick a new project name. Do not reuse the first project. Run the helper before the first deploy. It sets both build contexts to the repository root and wires shared/version.txt into both images:
SHARED_PROJECT="demo-mono-shared-$(openssl rand -hex 6)"
bash use-shared-context.sh
gregale deploy --project --name "$SHARED_PROJECT" --source=worktree --dry-run
# Confirm the plan creates exactly two new services before applying.
gregale deploy --project --name "$SHARED_PROJECT" --source=worktree --yes
That first deploy establishes the shared-context baseline. Wait for both services to reach 100% and confirm that both return "shared":"shared-v1". Next change only the shared marker:
printf 'shared-v2\n' > shared/version.txt
gregale deploy --project --name "$SHARED_PROJECT" --source=worktree --dry-run
Both services should be selected. Apply without --dry-run, then check that each app gets a new deployment and returns "shared":"shared-v2" after applying. This is build-context change detection, not a dependency graph inferred from imports or Dockerfile COPY statements. With both contexts set to the root, an uploaded frontend-only file can select both services even when the API Dockerfile does not copy it. Give a service the repository-root context only when it needs files outside its own directory.
What we verified on Gregale
We ran both build-context layouts from the downloadable starter in disposable Gregale production projects. The unchanged apply created no deployments. The frontend-only change produced a new frontend deployment and marker while the API's deployment history stayed unchanged. In the repository-root layout, editing only the shared marker produced a new deployment for each service, and both returned shared-v2. We removed the test apps and projects. The frontend-only row for two root contexts describes the scanner’s behavior from source inspection, not a fourth hosted test.
Where this applies
This walkthrough covers production project deploys from local Compose source with explicit Dockerfile contexts. It does not demonstrate GitHub push behavior, named-stage deploys, applies that bundle new secrets or settings, or selective rebuilding for Railpack or workspace discovery. The multi-service reference explains those differences. In an existing project, don't use --only to force the expected result: the point is to check what Gregale selects on its own.
When finished, remove the two recorded disposable apps in each project, then remove the two projects. Deleting a project alone detaches its apps; it does not delete them. Confirm each app belongs to the expected disposable project and matches its recorded ID before deleting it. Delete the local receipts when you no longer need them.
Questions
- How do I deploy only changed services in a monorepo?
For Gregale production Docker Compose projects, give each independent service its own Docker build context. Run a dry run to see which services would deploy before applying the source.
- What happens when both services use a shared package?
Include the shared package in the build contexts of the services that copy it. Changes in a shared repository-root context can rebuild both services; Gregale does not infer the dependency graph from imports.
- How can I tell whether the API was skipped?
Compare the API’s deployment history before and after applying the frontend change. The existing API deployment ID should stay the same; a fast build alone is not evidence.