Sub topic 13
Securing pipelines and handling secrets
A pipeline holds some of the most powerful credentials in a project. It can read your source, build your artifacts, and write to production. This chapter assumes you can read simple YAML, know basic Git, and have seen the workflow files from earlier chapters. It covers the working model of pipeline security and the errors that cause most leaks.
What you are protecting
A pipeline runs code on a machine (a runner) with access to secrets. A secret is any value that grants access, such as a cloud key, a deploy token, or a database password. Anyone who can change what the pipeline runs can usually reach those secrets. That is why the pipeline definition, covered in the pipeline-as-code chapter, is itself a security boundary.
Store secrets outside the code
Never commit a secret to the repository. Git history keeps it even after you delete the file, and anyone with read access can recover it. Use the secret store built into your CI system, or an external vault, and inject values at run time.
deploy:
needs: build
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v4
- run: ./scripts/deploy.sh dist/
env:
DEPLOY_TOKEN: ${{ secrets.DEPLOY_TOKEN }}Two details matter here. The secret is passed only to the step that needs it, not to the whole job. And it is tied to the production environment, so you can restrict it to that environment and require approval before the job can use it.
Common practices
- Least privilege. Give each token only the permissions the job needs. A test job rarely needs write access to anything.
- Short-lived credentials. Prefer tokens that expire in minutes. Many cloud providers support federated identity (OpenID Connect), where the pipeline proves its identity and receives a temporary credential, so no long-lived key is stored at all.
- Scoped per environment. Use separate secrets for staging and production, so a staging leak cannot touch production.
- Rotation. Change secrets on a schedule and immediately after any suspected exposure.
- Pinned dependencies. Third-party actions and images run with your pipeline's access. Pin them to a specific version, or better a commit hash, rather than a moving tag.
Common errors
| Mistake | Why it hurts | Fix |
|---|---|---|
| Printing environment variables with `env` or `set -x` | Masking only catches exact matches, and transformed values escape it | Never echo secrets, and avoid debug tracing near them |
| Running untrusted pull requests with secrets | A fork can change the workflow to send secrets elsewhere | Withhold secrets from forks, and require review before running them |
| One broad token for everything | One leak exposes every system | Use narrow tokens per job and environment |
| Secrets baked into build artifacts or images | Artifacts are copied and stored widely | Inject secrets at deploy time, not build time |
| Unpinned third-party actions | A compromised update runs in your pipeline | Pin versions and review updates |
Protect the path to production
Secrets are only half the picture. Also restrict who can change the pipeline and who can approve a release. Protect the main branch, require code review for changes to workflow files, and use environment approvals for production. These controls keep one compromised account from deploying whatever it likes.
Exercise
A teammate adds echo $DEPLOY_TOKEN to a workflow to debug a failing deploy. The CI system masks the value in logs. Why is this still a problem, and what should be done if the log ran?
Show answer
Masking only hides exact matches of the stored value, so encoded or split output can still expose it, and anyone with log access could copy it. Remove the debug line, treat the token as compromised, revoke it, and issue a new one.
Exercise
Your pipeline runs tests on pull requests from forks. Should those runs receive the production deploy token? Explain.
Show answer
No. A fork's author controls the code and can edit the workflow to send the token elsewhere. Withhold secrets from fork runs and keep production secrets in an environment that only trusted branches can use.