The Settings That Didn't Survive a Deploy

2026-11-28来源 dev.toEnglish

The app in question is small: a Node.js service on one VM, deployed by CI after every merge into a release-directory layout with a current symlink. It has a settings screen where an admin can change a handful of values — the VAT rate, the public URL, mail server details, an API key for an external marketplace. Saving takes effect immediately, without a restart.

Two bugs in that settings screen showed up in the days around moving the app onto the server. They looked unrelated: one lost settings on deploy, the other broke outgoing mail at runtime. They turned out to be the same mistake made twice — a write path that didn't know who owned the value it was writing.

TL;DR: Configuration had three owners and the code acknowledged one. The checked-in template belongs to the deploy, which copies it fresh into the shared config directory on every release — so non-secret settings the app saved into that file were silently reset by the next merge. Values set in the environment belong to the operator — but the settings form didn't know that, showed them as empty, sent them back as empty strings, and the save handler responded by deleting them from process.env, switching off mail until the next restart. The fix gives each value one owner: the deploy owns the template, the app owns a local overrides file, and the environment owns whatever was set at process start — which the UI now shows as read-only.

Where Does Each Value Live?

Three places, merged in a fixed order at startup:

Source Written by Lifetime
configuration.json — the template the repository, copied by the deploy replaced on every release
configuration.local.json — local overrides the app, when an admin saves survives deploys, mode 0600
environment variables systemd, from a root-only env file survives everything until changed by an operator

The local file sits outside the release directory, in a shared config directory, so it survives the symlink switch. The template is copied into that same directory on every deploy, because the app reads both from one place:

mkdir -p "$CONFIG_DIR" cp -f "$REL_DIR/config/configuration.json" "$CONFIG_DIR/configuration.json" 

That line is correct. It's what makes a change to the template in Git actually reach the server. It's also what turned the first bug from a design flaw into data loss.

Why Did Saved Settings Disappear?

Because the save handler had one rule for where a value goes: secrets into the local file, everything else back into the template.

The reasoning made sense when the app ran from a single directory without a deploy pipeline. The template was the configuration; the local file existed only to keep passwords out of Git. Writing the VAT rate back into the template was simply updating the configuration.

In the release layout, "the template" is a file the deploy owns and overwrites. So:

  1. The admin sets the VAT rate to 7 % and the public URL. Saved. Takes effect immediately.
  2. Both values are now in configuration.json. The local file contains only the mail password.
  3. Someone merges a documentation change. CI deploys — the pipeline runs after every merge.
  4. cp -f puts the checked-in template back. VAT rate and URL are the repository defaults again.

Nothing errors. The settings screen reloads with the old values, and the only person likely to notice is the one who saved them — later, when the old rate turns up somewhere it shouldn't. The move-in documentation even said a saved setting "survives the next deploy". For secrets, it did.

What Did the Fix for That Look Like?

In the release layout, the template is read-only from the app's point of view. Everything an admin changes goes to the local file — not just secrets:

// simplified // Deploy layout: the template in CONFIG_DIR is replaced on every deploy. // Anything that differs from it (or is already local) belongs in the local file. const ownedByDeploy = Boolean(process.env.CONFIG_DIR); if (ownedByDeploy) { for (const [key, value] of Object.entries(next)) { const secret = secrets.has(key); if (secret && (value === '' || value == null)) continue; // empty secret = "unchanged" const sameAsTemplate = key in base && JSON.stringify(base[key]) === JSON.stringify(value); if (secret || key in local || !sameAsTemplate) nextLocal[key] = value; } writeJson(LOCAL_PATH, nextLocal, 0o600); return readConfig(); } 

Two details matter. A value equal to the template isn't copied into the local file, so a later change to the template in Git still reaches the server for anything nobody has overridden. And a key that's already local stays local even when it's set back to the template value — otherwise "reset to default" would silently hand ownership back to the deploy, and the next template change would move it without anyone asking.

Outside the release layout — local development, no CONFIG_DIR — the old behaviour stays, because there the template really is the only configuration.

Why Did Saving the VAT Rate Switch Off Mail?

The second bug arrived with the server move. Mail server credentials and the marketplace API key no longer lived in the config files at all; they came from an environment file only root can read, loaded by systemd. The app preferred the environment over the files, and mail worked — two-factor login codes arrived.

The settings screen didn't know any of that. It asked the config store for the current values, and the store only knew the files. So:

  1. The form showed mail host, port, user and sender as empty, and the password as "no value set". An admin looking at it would conclude the mail configuration was missing — while it was working.
  2. Saving sent every field back, including the empty ones as ''.
  3. The save handler accepted empty non-secrets and cleaned up the environment to match: delete process.env.SMTP_HOST, and the same for port, user, sender and the API key.

The next mail after any save — changing only the VAT rate was enough — went nowhere, because there was no longer a mail host in the process. Two-factor login codes stopped. The app's mailer falls back to a local log file when no transport is configured, so nothing crashed. Logging in became impossible until someone restarted the service, which reloaded the environment and made the problem disappear without a trace.

It was found by reading the settings API on the server, not by saving — on purpose: the acceptance test on the server deliberately didn't save any setting. Reading first showed the empty fields, and the empty fields explained what a save would do before one ever happened.

How Do You Make the UI Respect a Value It Doesn't Own?

Record ownership at startup and carry it through every layer:

  • At startup, the config module remembers which keys were already set in the environment before any file was read. That set never changes at runtime.
  • The settings API returns it alongside the values, and returns the actual effective value for those keys, with secrets masked as "set".
  • The save handler ignores those keys entirely — it neither writes them anywhere nor touches process.env for them.
  • The form shows them as locked, with a note that the server provides them, and doesn't send them back.

A test pins the original failure: with mail settings in the environment, saving only the VAT rate must leave every mail value in process.env untouched.

What's the Generalizable Lesson Here?

  • Every configuration value needs exactly one owner, and every write path has to know who it is. "Where do I write this?" is a question about ownership, not about which file is convenient.
  • A deploy that copies a template is a writer. Anything the app also writes into that file will be overwritten, silently, on whatever schedule your merges keep — after every merge, here.
  • An app writing its own overrides belongs in a file the deploy never touches. Store only what differs from the template, so template changes still flow through for everything nobody overrode.
  • Values from the environment are owned by the operator. A UI that can't see where a value came from will display it as missing and, sooner or later, "fix" it.
  • A form that sends back fields the user never touched is a write of those fields. Empty-string round trips are the most common way a settings screen destroys configuration it was only displaying.
  • When you can't yet say what an action does on production, read before you write. Here the read-only look at the settings API predicted the outage without causing it.
  • The same ownership question decides how the deploy itself treats shared directories, and why a preflight runs against a copy with its own config: whatever the release doesn't own, it mustn't overwrite.

原文:https://www.a8q9.cn/en/article/the-settings-that-didn-t-survive-a-deploy-f30c99.html

相关文章