Prompt
How do I inject secrets into CI/CD pipelines at build time?
Latest observation
Injecting secrets into CI/CD pipelines at build time is usually done by pulling them from a secret manager at runtime and exposing them only to the job that needs them. The main goal is to avoid hardcoding secrets in source control, Dockerfiles, or build artifacts.
Recommended approaches
1. Use your CI/CD platform’s secret store
Most CI systems let you define encrypted secrets as environment variables or masked variables:
- GitHub Actions: Repository/organization secrets
- GitLab CI: CI/CD variables
- Jenkins: Credentials store
- CircleCI / Azure DevOps / Bitbucket Pipelines: built-in secret/variable stores
Then pass them into the build only for the relevant step.
Example:
steps:
- name: Build
env:
API_KEY: ${{ secrets.API_KEY }}
run: ./build.sh
2. Fetch secrets from a dedicated secret manager
For better security and rotation, use a centralized secret manager:
- HashiCorp Vault
- AWS Secrets Manager / SSM Parameter Store
- Azure Key Vault
- Google Secret Manager
- 1Password Secrets Automation
- Doppler / Infisical / Akeyless, etc.
Typical pattern:
- CI job authenticates using short-lived cloud identity/OIDC.
- Job requests the secret from the secret manager.
- Secret is exported only in-memory to the build step.
- Secret is never written to disk or committed to logs.
Example pattern:
export DB_PASSWORD="$(vault kv get -field=password secret/myapp/db)"
./build.sh
3. Use OIDC instead of long-lived cloud credentials
Prefer federated identity (OIDC) from CI to cloud provider rather than storing cloud access keys in CI secrets.
This lets the pipeline:
- authenticate using a signed OIDC token,
- assume a role,
- fetch secrets from the secret manager,
- use short-lived credentials.
This is one of the best practices for modern CI/CD.
4. Inject secrets as environment variables at runtime
For app build or test steps, inject secrets into the process environment:
export NPM_TOKEN="$NPM_TOKEN"
npm ci
npm test
Or in a Docker build, use BuildKit secrets:
# syntax=docker/dockerfile:1.4
FROM node:20
RUN --mount=type=secret,id=npm_token \
NPM_TOKEN=$(cat /run/secrets/npm_token) npm ci
Build command:
docker build --secret id=npm_token,env=NPM_TOKEN .
Important security practices
Don’t:
- hardcode secrets in Dockerfiles or code
- pass secrets as plain build arguments (
ARG) in Docker unless you know they won’t leak - echo secrets in logs
- store secrets in artifacts, images, or cache layers
- use long-lived cloud keys if OIDC is available
Do:
- use least privilege
- scope secrets per environment/job
- mask secrets in logs
- rotate secrets regularly
- use short-lived credentials whenever possible
- ensure secrets are only available to the specific step that needs them
Common build-time use cases
- private package registries (
npm,pip,maven,nuget) - signing keys
- integration test credentials
- deploy credentials or tokens
- cloud API access during build
Rule of thumb
If a secret is needed during build, try to:
- fetch it just-in-time,
- keep it in memory only,
- use it in the smallest possible scope,
- prevent it from ending up in the final artifact/image.
If you want, I can show you a secure example for a specific CI system like GitHub Actions, GitLab CI, Jenkins, or Azure DevOps.