Sub topic 11
Using secrets in CI and deployment
This chapter assumes you can already load the key from an environment variable in your code and keep .env out of Git. It also assumes you have used GitHub (or a similar host) and have seen a YAML file. The examples use GitHub Actions and Docker. Other CI systems and hosting platforms have the same features under different names.
Why CI and deployment need their own copy of the key
Your .env file is git-ignored, so it never reaches the repository. That is the goal, but it means a CI runner that checks out your code has no key. The same is true of a server or container you deploy.
The wrong fix is to commit the key, or to put it in the workflow file. The right fix is to give each environment its own copy of the key through a secret store. A secret store is a feature of the CI or hosting platform that keeps values encrypted and hands them to your job or process as environment variables at run time. Your code does not change. It still reads LLM_API_KEY from the environment, exactly as it does on your laptop.
Adding a secret to GitHub Actions
In a GitHub repository, open Settings, then Secrets and variables, then Actions, and choose New repository secret. Name it LLM_API_KEY and paste the value. You can do the same from the terminal with the GitHub CLI, which prompts for the value so it does not land in your shell history:
gh secret set LLM_API_KEYOnce saved, nobody can read the value back through the interface. You can only replace or delete it. This is why you should store the original in a password manager at the moment the provider shows it.
Then reference the secret in a workflow:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run tests
run: ./run_tests.sh
env:
LLM_API_KEY: ${{ secrets.LLM_API_KEY }}GitHub substitutes the secret before the step starts. The workflow file contains only the reference, so it is safe to commit.
Scope the secret to the step that needs it
An env: block can sit at the workflow, job, or step level. Put it on the step. A key set at workflow level is visible to every step in every job, including third-party actions you may not have audited. A step-level key is visible only to that step's process.
Secret scopes: repository, environment, organization
GitHub offers three places to store a secret.
| Scope | Who can use it | Typical use |
|---|---|---|
| Repository secret | Every workflow in one repository | A low-limit key for running tests |
| Environment secret | Only jobs that declare that environment | A production key |
| Organization secret | Repositories you choose within the organization | A shared key for several related projects |
Environment secrets are the most useful for separating test from production. You create an environment such as production in the repository settings, add its secrets, and optionally require a reviewer to approve each deployment. A job opts in with the environment keyword:
permissions:
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v4
- name: Deploy
run: ./deploy.sh
env:
LLM_API_KEY: ${{ secrets.LLM_API_KEY }}Here secrets.LLM_API_KEY resolves to the production environment's value. A job without environment: production cannot read it. The permissions block limits the automatic GitHub token to read access, which reduces what a compromised step could do.
Common errors and gotchas
Secrets are not passed to workflows from forks. When someone opens a pull request from a fork, GitHub does not expose your repository secrets to the pull_request workflow. Tests that need the key will fail with "LLMAPIKEY is not set". That is the intended behavior, and it stops strangers from stealing your key with a modified workflow. Do not work around it with pull_request_target and a checkout of the pull request's code. That combination runs untrusted code with access to your secrets.
Log masking is exact-match only. GitHub replaces the literal secret value in logs with ***. If a script transforms the value, for example by base64-encoding it, splitting it, or URL-encoding it, the transformed text is not masked. Avoid printing anything derived from a secret.
Debug output leaks. Running a shell script with set -x, or an HTTP client with verbose logging, can print the Authorization header. Keep verbose logging off in steps that hold a key. For a check that is safe to run, print only a status code or the key's length, as you did in the local verification chapter.
Command-line arguments are visible. Passing the key as --api-key "$LLM_API_KEY" can expose it in process listings and in some tools' error messages. Prefer reading it from the environment inside the program.
Missing secrets resolve to an empty string. If you misspell the secret name in the workflow, GitHub does not fail. It passes an empty value. Your startup check (if not api_key: raise ...) is what catches this, so keep it.
Secrets are not available to every trigger. Scheduled workflows and push events on your own branches get secrets. Workflows triggered by Dependabot get a separate set of Dependabot secrets, so a dependency update pull request can fail on a missing key even though the repository secret exists.
Do your tests need the real key?
Often they do not. A unit test that checks how your code builds a prompt or parses a response should use a fake client and no key at all. Reserve real API calls for a small number of integration tests, and run those only on the main branch or on a schedule. This keeps costs predictable, makes pull requests from forks work, and limits how often the key is exposed to a runner.
Deployment: environment variables, not files in the image
On a hosting platform, set the key in the platform's environment or secrets settings. Most platforms, including Heroku, Render, Fly.io, Vercel, and the major cloud container services, store the value and inject it into your running process. The platform's interface differs, but the result is the same: os.environ["LLM_API_KEY"] works.
Containers
A Docker image is a set of layers that anyone with the image can inspect. Anything you put in an image, you should assume is readable by whoever can pull it. That rules out three habits:
- Writing
ENV LLM_API_KEY=...in a Dockerfile. - Passing the key as a build argument with
ARG, because build arguments are recorded in the image history. - Copying
.envinto the image withCOPY . ..
Instead, build the image with no key, and supply the key when the container starts. Add .env to .dockerignore so it can never be copied in by accident:
# .dockerignore
.env
.gitAt run time, pass the variable from the host environment:
export LLM_API_KEY="paste-your-key-here"
docker run --rm -e LLM_API_KEY myapp:latestWith no =value after -e LLM_API_KEY, Docker copies the variable from your current shell. The key never appears in the command line or the image. In production, an orchestrator or hosting platform performs the equivalent step from its own secret store.
Rotating a key in CI and deployment
A rotated key must be updated everywhere it is stored. Keep a short list of where each key lives: the GitHub repository and environment secrets, the hosting platform, and any teammate's local .env. When you create the new key, update each location, redeploy so running processes pick up the new value, and only then revoke the old key. Environment variables are read at process start, so a running server keeps using the old key until it restarts.
Exercise
A teammate's pull request from a fork fails in CI with "LLMAPIKEY is not set", although the repository secret exists and your own branches pass. Is this a bug, and what is the safest way to still test their change?
Show answer
It is not a bug. GitHub withholds repository secrets from workflows triggered by fork pull requests so a stranger cannot read them by editing the workflow. The safest options are to make the tests that need the key optional or mocked for fork runs, or to have a maintainer review the code and then run the full tests from a branch inside the repository. Do not switch to pullrequesttarget with a checkout of the fork's code, because that runs untrusted code with access to your secrets.
Exercise
Why is docker run --rm -e LLM_API_KEY myapp:latest safer than putting ENV LLM_API_KEY=... in the Dockerfile?
Show answer
The ENV line is stored in the image layers and metadata, so anyone who can pull or inspect the image can read the key, and it is hard to change without rebuilding. Passing the variable with -e supplies it only to the running container from the host environment, and the image itself contains no secret.