Sub topic 11
Using secrets in CI and deployment
This chapter assumes you can read a short YAML file, use Git and the command line, and know that your code reads the key from an environment variable such as LLM_API_KEY. A .env file stays on your machine, so CI and production need another way to supply the key.
Why CI needs its own secret store
A CI (continuous integration) system runs your tests on a remote machine every time you push code. That machine does not have your .env file, because you kept it out of Git. You also must not commit the key just to make the tests pass.
CI platforms solve this with a secret store. You paste the key into the platform's settings once. The platform encrypts it and injects it into jobs as an environment variable. Your code does not change, because it already reads os.environ or process.env.
Adding the secret in GitHub
In a GitHub repository, open Settings, then Secrets and variables, then Actions. Create a repository secret named LLM_API_KEY. After you save it, GitHub never shows the value again. You can only replace or delete it.
Then reference it in the 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 }}The workflow file holds only the secret's name. The value is filled in when the job runs. GitHub also masks the value in job logs and replaces it with ***.
Scope the secret to the step that needs it
The env: block above sits on one step. Only that step can read the key. If you put env: at the top of the workflow, every step gets it, including third-party actions you did not write. Give the key to the fewest steps possible.
Use a separate key for CI, as the previous chapter recommended. Give it a low spend limit. Automated tests that call a paid model can run up costs quickly, so consider mocking the provider in most tests.
Common mistakes
- Echoing the secret. Masking only catches the exact string. If a script prints the key in a changed form, such as base64 or with characters split apart, the mask will not match it.
- Pull requests from forks. GitHub does not pass repository secrets to workflows triggered by forks. This is deliberate. Do not work around it with settings that run untrusted code with secrets, because that lets a stranger's pull request read your key.
- Writing the key into build output. Do not bake it into a Docker image, for example with
ARGorCOPY .env. Anyone who can pull the image can extract it from the image layers. - Debug mode. Shell tracing such as
set -xprints expanded commands and can expose values.
Deployment
Production platforms follow the same pattern. Hosting services and container platforms have a settings page or command for environment variables. You set LLM_API_KEY there, and the platform provides it to your running process. Your code is identical in development, CI, and production. Only the source of the variable differs.
Treat these stores as the right place for small projects. If many people or services need keys, or you need audit logs and automatic rotation, the final chapter covers dedicated secret managers.
Exercise
A teammate adds env: LLM_API_KEY: ${{ secrets.LLM_API_KEY }} at the top level of the workflow so that every step can use it. Why might you ask them to move it, and where should it go?
Show answer
A top-level env: gives the key to every step in every job, including third-party actions that do not need it. Move it to the env: block of the single step (such as "Run tests") that calls the LLM provider, so the fewest steps can read the key.