Skip to content

feat(compose): expose deployment context for interpolation#2189

Merged
raphaeltm merged 4 commits into
mainfrom
sam/httpsgithubcomdefanglabssamplesblobmainsamplessel-4h9n5j
Jul 21, 2026
Merged

feat(compose): expose deployment context for interpolation#2189
raphaeltm merged 4 commits into
mainfrom
sam/httpsgithubcomdefanglabssamplesblobmainsamplessel-4h9n5j

Conversation

@defang-sam

@defang-sam defang-sam Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Description

Make the resolved deployment context available to Compose interpolation after stack selection:

  • DEFANG_PROVIDER: the selected provider
  • DEFANG_STACK: the selected stack name

The values are opt-in and are not injected into service environments unless referenced explicitly. Selected context takes precedence over Compose env files, while arbitrary process DEFANG_* variables remain filtered out.

Agent/MCP deployments reload Compose after interactive stack/provider selection so they use the same resolved interpolation context as direct CLI deployments. The reload preserves relative working directories, custom Compose paths, project names, and missing-config retry behavior.

This enables samples such as self-improving-mastra to pass provider and stack metadata into the deployed service without requiring portal-supplied configuration.

The canonical README now documents the interpolation behavior and regenerates the CLI/npm README copies. Also includes a formatter-only alignment correction in fixup.go required for the repository-wide lint gate.

Linked Issues

N/A

Testing

  • go test -race -short ./...
  • go vet ./...
  • golangci-lint run
  • make -B src/README.md

Checklist

  • I have performed a self-review of my code
  • I have added appropriate tests
  • I have updated the Defang CLI docs and/or README to reflect my changes, if necessary

@defang-sam
defang-sam Bot requested a review from lionello as a code owner July 21, 2026 13:53
Comment thread src/pkg/agent/tools/deploy.go Outdated

@lionello lionello left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In deploy, we have the sc stack params. We get a loader from the caller. That loader did not take sc into account. Why?

@defang-sam

defang-sam Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor Author

You're right — this was the missing path.

The caller constructs the initial loader before SetupProvider runs, but SetupProvider may interactively select a stack, mutate sc.Stack, and load that stack's environment. So that incoming loader cannot reliably contain the final provider/stack context.

The initial LoadProject remains the pre-selection validation/discovery pass. After SetupProvider, deploy now rebuilds the loader from the resolved absolute working directory and the original project/file parameters, adds the resolved DEFANG_PROVIDER and DEFANG_STACK interpolation values, and reloads the project. That second project is what goes to ComposeUp.

I also removed the questionable shadowing: the preliminary project result is explicitly discarded, and project is declared at the final load where it is first used. The regression test covers a relative working directory and verifies that the deployed project contains the resolved provider and stack values.

@raphaeltm
raphaeltm merged commit 2de1e4a into main Jul 21, 2026
14 checks passed
@raphaeltm
raphaeltm deleted the sam/httpsgithubcomdefanglabssamplesblobmainsamplessel-4h9n5j branch July 21, 2026 16:41
lionello pushed a commit to lionello/self-improving-mastra that referenced this pull request Jul 22, 2026
Bring this repo in line with DefangSamples/sample-self-improving-mastra-template
(the source this repo was generated from), which has since fixed the Fargate
health check and moved self-publish onto Defang's resolved deployment context.

- todo-app/Dockerfile: bind Next standalone to 0.0.0.0 so the `curl localhost`
  container health check passes on Fargate (the runtime injects HOSTNAME and
  would otherwise leave loopback unbound); exec keeps node as PID 1 for SIGTERM.
- compose.yaml / .env.{aws,gcp}: self-publish now reads ${DEFANG_PROVIDER} and
  ${DEFANG_STACK} from Defang's deployment context instead of PUBLISH_PROVIDER/
  PUBLISH_STACK committed in the per-cloud env files.
- agent/src/publish.ts, README.md: match the deployment-context mechanism.

NOTE: the ${DEFANG_PROVIDER}/${DEFANG_STACK} support requires a Defang CLI that
includes DefangLabs/defang#2189; v3.12.0 does not have it.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Sw7j9FYnbChZJbYArxbhuo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants