CI / Secrets and trust
Fill the .env of each folder
Your repository has many examples or apps, each with its own .env.example, and you copy each key into its .env by hand. src env pull --all writes the .env of each folder from its .env.example, with the values of a CI environment.
4 min read
With twenty examples, the copying takes a long time, and one wrong value breaks a run. With the pull, each folder gets only the keys that its own example names.
You need:
src, logged in, in a clone of the repository.- To pull: read access to the values of the environment. With the default settings, people with write access read
developmentandpreview. - To set the values: admin access to the repository.
1. Make a .env.example
You wrote an example, and its .env works. Other people must see which keys it needs, but never your values. In the folder of the example, run:
src env create --example
Read the new .env.example, then commit it. The command keeps the comments, the blank lines, the order of the keys and export. It removes every value:
# .env
# The database of the example
DATABASE_URL=postgres://localhost:5432/react
export API_URL=http://localhost:3000
# .env.example
# The database of the example
DATABASE_URL=
export API_URL=
- To read another file, add
-f .env.local. The example still goes to.env.example, in the same folder. - If a
.env.exampleexists, the command does not replace it. Add--forceto replace it. - If a comment of the file still holds a secret, like a token, the command refuses the file. It names the line and the kind of secret, never the value. Remove the secret, then run the command again.
- The command works on your machine only. It makes no call to the forge and needs no login.
Tip
To make the example of each .env in the repository at once, run src env create --example --all. It reads each .env file in the work tree, tracked by git or not. It does not look in node_modules or in folders whose name starts with a dot. If it skips or refuses a file, it does the other files, then exits 1.
2. Put the values in the environment once
An admin of the repository sets each key one time, in the development environment. Use a secret for a token or a password, and a variable for a value that is not secret:
src secret set DATABASE_URL --env development --host db.example.com --header X-Db-Url < dev-url.txt
src variable set API_URL --env development --body http://localhost:3000
CI environments has more about these commands.
3. Pull the values
In your clone, run:
src env pull --all
✓ Wrote 3 values of development to examples/react/.env
! examples/vue: STRIPE_KEY is not in development: the file keeps the lines of .env.example
✓ Wrote 2 values of development to examples/vue/.env
- The pull reads the
.env.examplefiles that git tracks. - It uses
development. To use another environment, add--env preview. - It reads only the keys of the folders that it writes. The audit trail gets only those reads.
- Each
.envis readable by you only. Keep it out of git: the forge refuses a push that adds a.envfile.
Done
Each .env starts with a header line, then has the lines of its .env.example with the values:
# The development environment of acme/examples, for the keys of .env.example, from src env pull --all. Keep it out of git.
# The database of the example
DATABASE_URL='postgres://db.example.com:5432/react'
export API_URL='http://localhost:3000'
Each example runs with its own keys, and nobody copied a value by hand.
What happens when
- A key is not in the environment. Its line stays as it is in
.env.example, and the pull names the folder and the key. Set the key in the environment, then pull again with--force. - A
.envexists. The pull does not change it. It writes the other folders, then exits 1. To replace the file, add--force. - git tracks the
.env. The pull never writes it, also with--force, and exits 1.
Who can read the values
- The readers of the environment. With the default settings, people with write access read
developmentandpreview. - Nobody reads the values of
productionback. - A token with repositories reads the values only when it holds
secrets:read. - Each secret value read goes to the audit trail. Admins see it with
src secret audit --env development.
A script or an agent that needs only some keys can name them: see Read only some keys from a script or an agent.