Skip to content

feat: add b2c mrt env var push command#308

Draft
vmarta wants to merge 3 commits intomainfrom
vm/mrt-env-var-push
Draft

feat: add b2c mrt env var push command#308
vmarta wants to merge 3 commits intomainfrom
vm/mrt-env-var-push

Conversation

@vmarta
Copy link
Copy Markdown

@vmarta vmarta commented Mar 30, 2026

Storefront Next developers need to sync their local .env file to a Managed Runtime environment before or after a deployment. Today this is a manual process: look up each variable, compare it against what's in MRT, and call b2c mrt env var set one by one.

This command automates that workflow. It belongs in b2c-cli (rather than the storefront-next CLI) because pushing env vars to MRT is useful to any project deployed on MRT, not just storefront-next storefronts.

TODOs:

  • Review and polish code changes
  • Discuss whether it's ok to add dotenv dependency

What's changed

Adds b2c mrt env var push — a new command alongside the existing list, set, and delete commands.

Behaviour:

  1. Reads and parses a local .env file (default: .env, configurable with --file)
  2. Filters out variables whose keys match excluded prefixes (default: MRT_, configurable with --exclude-prefix)
  3. Fetches the current remote env vars via mrt env var list
  4. Computes and displays a diff (add / update / unchanged / remote-only)
  5. Prompts for confirmation unless --yes is passed
  6. Pushes each changed variable with per-variable success/failure reporting
  7. Prints a final summary; returns { pushed, failed, skipped } in --json mode

New files:

  • src/commands/mrt/env/var/push.ts — oclif command
  • src/utils/mrt/env-var-diff.ts — pure diff utilities (filterByPrefix, computeEnvVarDiff, formatEnvVarDiffSummary)
  • test/commands/mrt/env/var/push.test.ts — 8 unit tests
  • test/utils/mrt/env-var-diff.test.ts — 19 unit tests

How to test

Prerequisites: a Managed Runtime project you can write to, and a valid ~/.mobify credentials file (b2c login if needed).

# 1. Build the CLI
cd packages/b2c-cli && pnpm build

# 2. Verify the command is discoverable
./bin/run.js mrt env var push --help

# 3. Create a test .env file
cat > /tmp/test.env <<'DOTENV'
PUBLIC__test__value=hello
DOTENV

# 4. Dry-run: inspect the diff, then decline the prompt
./bin/run.js mrt env var push \
  --project <your-project> --environment <your-environment> \
  --file /tmp/test.env

# 5. Push without prompt
./bin/run.js mrt env var push \
  --project <your-project> --environment <your-environment> \
  --file /tmp/test.env --yes

# 6. Confirm the variable was pushed
./bin/run.js mrt env var list \
  --project <your-project> --environment <your-environment>

# 7. Run unit tests
pnpm test:unit

Reads a local .env file, diffs against remote MRT env vars, shows a
summary, prompts for confirmation (skippable with --yes), and pushes
changed variables one at a time with per-variable success/failure
reporting.

New files:
- src/commands/mrt/env/var/push.ts — oclif command extending MrtCommand
- src/utils/mrt/env-var-diff.ts — pure diff utilities (filterByPrefix,
  computeEnvVarDiff, formatEnvVarDiffSummary)
- test/commands/mrt/env/var/push.test.ts — 8 mocha tests
- test/utils/mrt/env-var-diff.test.ts — 19 mocha tests

Adds dotenv dependency for .env file parsing.
@github-actions github-actions bot added the needs-3pl-review PR introduces net-new third-party dependencies and needs discussion label Mar 30, 2026
@clavery clavery added 3pl-approved Maintainer approved net-new third-party dependency additions and removed needs-3pl-review PR introduces net-new third-party dependencies and needs discussion labels Apr 1, 2026
@clavery
Copy link
Copy Markdown
Collaborator

clavery commented Apr 1, 2026

We're using nodes native dotenv support and it comes with some programmatic APIs.

@vmarta can you explore if https://nodejs.org/api/util.html#utilparseenvcontent is sufficient. It's available in node 21+ which is good for us and sfnext

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3pl-approved Maintainer approved net-new third-party dependency additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants