---
title: "Secrets"
description: "Infisical is the source of every secret, GitHub only holds the key to read it."
url: https://pr-417-docs.preview-ritorno.findmalek.engineer/docs/secrets
markdown: https://pr-417-docs.preview-ritorno.findmalek.engineer/docs/secrets.md
---

# Secrets

Secrets live in the self hosted Infisical on The Keep (`secrets.findmalek.engineer`, see [the keep](https://pr-417-docs.preview-ritorno.findmalek.engineer/docs/infrastructure/the-keep.md)). `.env.example` stays the documented list of keys, Infisical holds the values. Containers never talk to Infisical: the deploy scripts read it once and hand the result to Dokploy.

## Flow

1. A deploy job (`staging`, `release`) or a preview logs in to Infisical with Universal Auth (`INFISICAL_CLIENT_ID` and `INFISICAL_CLIENT_SECRET`) and exports the environment as dotenv.
2. The script merges, from weakest to strongest: what Dokploy already holds, the Infisical export, the values it computes (public urls, `DEPLOY_ENV`, `SEED`, `MESSAGING_LIVE`, `ALLOW_PREVIEW_ORIGINS`, `S3_PREFIX`; for previews also the database url and the demo password).
3. The merged env is written to the Dokploy compose service (`compose.update`) and the service is deployed.

Without the four `INFISICAL_*` settings staging and production keep the env Dokploy already has, with the computed values refreshed on every deploy, and previews keep using the `PREVIEW_APP_ENV` secret. Values are never printed, the job summary only shows the number of keys.

## Environments

| Infisical env | Used by |
| --- | --- |
| `dev` | `pnpm dev` and the other local commands on a laptop |
| `staging` | the `staging` workflow, every merge to `main` |
| `prod` | the `release` workflow (merged release PR or a manual tag run) |
| `preview` | every PR preview, shared by all PRs (the per PR database, urls and passwords are computed) |

Previews, staging and dev must only hold log-only or test provider keys, they never send real messages.

## Locally

Install the Infisical cli and run `infisical login` once from the repo root. `.infisical.json` already names the instance and the project, so there is no `infisical init`. The instance is tailnet only.

`pnpm dev`, `dev:web`, `dev:dashboard`, `dev:worker`, `db:up`, `db:down`, `db:migrate`, `db:seed`, `db:studio` and `whatsapp:setup` run through `scripts/with-env.mjs`, which exports the `dev` environment. From strongest to weakest:

1. variables already set in your shell
2. Infisical `dev`
3. the root `.env`, optional: machine only settings such as `POSTGRES_PORT`
4. `DATABASE_URL`, when none of the above sets it: the one of `.env.example` (the local docker postgres) with any `POSTGRES_USER`, `POSTGRES_PASSWORD`, `POSTGRES_PORT` or `POSTGRES_DB` override applied

When the cli is missing, logged out or cannot reach Infisical, the command warns and continues with the rest. If the tailnet is only reachable through a socks proxy, set `INFISICAL_PROXY=socks5://127.0.0.1:1055`: it applies to the cli only, not to the apps. `INFISICAL_PROXY` is read from the shell only, not from `.env`.

`pnpm dev:secrets` is the strict variant: it fails instead of starting when `infisical run` cannot read the secrets (behind a proxy use `pnpm dev`). `pnpm secrets:pull dev .env.infisical` writes an environment to a file (needs the `INFISICAL_*` variables).

## Add a key

1. Declare it in `packages/env/src/api.ts` (or `next.ts`) and add it to `.env.example` with a comment. `pnpm env:check` runs in CI and fails when a key is in `.env.example` but not declared in `packages/env`, or when `docker-compose.deploy.yml` does not pass a declared key to the services that read it (`api` and `worker` for `api.ts`, `web` and `dashboard` for `next.ts`). Optional keys go in as `${KEY:-}`. `NEXT_PUBLIC_*` keys are also build args of `web` and `dashboard`, because `next build` inlines them.
2. Add the value to each Infisical environment that needs it. Run the `secrets-diff` workflow (Actions, run workflow) to list, per environment, the keys of `.env.example` that Infisical lacks, names only. Its `dokploy vs infisical` job lists, for staging and production, which keys Infisical would overwrite in the live Dokploy env (values compared as hashes, never printed). Run it before changing a value that is live.
3. Redeploy: staging on the next merge, production on the next release, or run the `staging` workflow by hand.

Keys the deploy scripts compute (urls, `DEPLOY_ENV`, `SEED`, ...) are not stored in Infisical, the list is `COMPUTED` in `scripts/env-keys.mjs`. `POSTGRES_PASSWORD` must be stored in `staging` and `prod`: it has to stay stable because the database volume keeps the first one.

## Rotate

1. Change the value in Infisical for the environment.
2. Redeploy that environment. For `BETTER_AUTH_SECRET` all sessions end, for `POSTGRES_PASSWORD` change the role password in the database first.
3. Machine identity: create a new client secret in Infisical, update the GitHub secret `INFISICAL_CLIENT_SECRET`, then revoke the old one.

## What stays in GitHub

- secrets: `INFISICAL_CLIENT_ID`, `INFISICAL_CLIENT_SECRET`, plus the credentials of the deploy target itself (`DOKPLOY_TOKEN`) and `GITHUB_TOKEN`
- variables: `INFISICAL_URL`, `INFISICAL_PROJECT_ID`, the Dokploy ids and public urls (see [deploy runbook](https://pr-417-docs.preview-ritorno.findmalek.engineer/docs/deploy-runbook.md))

Everything the apps read at runtime (database, auth, providers, storage, analytics keys) moves to Infisical. `PREVIEW_APP_ENV` and any other app secret in GitHub can be deleted once the `preview` environment is filled.

### Stale entries to delete

No workflow reads these, the values live in Infisical or are computed:

- secrets: `ADMIN_PASSWORD`, `BETTER_AUTH_SECRET`
- variables with localhost urls: `API_PUBLIC_URL`, `API_URL`, `DASHBOARD_URL`, `DOCS_URL`, `WEB_URL`
- other variables: `ADMIN_EMAIL`, `AI_MODEL`, `EMAIL_FROM`, `JOBS_WORKER`, `LOG_LEVEL`, `OPENOBSERVE_ORG`, `S3_REGION`, `SEED`, `WHATSAPP_TEMPLATE_LANGUAGE`, `WHATSAPP_TEMPLATE_NAME`
