Security in the pipeline (DevSecOps)
DevSecOps means security checks run inside the same pipeline as your tests, on every change, instead of a security review weeks after release. Each check is small and automatic. Together they catch leaked passwords, vulnerable libraries, risky infrastructure and over-powered CI jobs before any of it reaches production.
Shift left: catch it where it's cheap
A leaked key caught on your laptop costs a minute. The same key found in production logs costs a rotation, an investigation and maybe a breach notice. "Shift left" means moving each check as early (as far left in the pipeline) as it can go:
- On your machine: a pre-commit hook that refuses to commit secrets.
- On every push and pull request: dependency, code and config scans in CI.
- On every image: a vulnerability scan before it's pushed (Docker chapter 6).
- In production: patching, least privilege and monitoring (DevOps chapter 7).
Secrets never go in Git
Anything committed stays in the history, even after you delete it in the next commit, and anyone who clones the repo gets it. Bots scan public GitHub for keys within minutes. So:
- Keep secrets in
.envfiles that are in.gitignore, and commit a.env.examplewith the names but no values. - In CI, use the platform's secret store (GitHub Actions secrets, GitLab CI variables), never a value in the workflow file.
- Run a scanner before each commit. gitleaks knows hundreds of token formats; with the
pre-commitframework it runs automatically:# .pre-commit-config.yaml repos: - repo: https://github.com/gitleaks/gitleaks rev: v8.21.2 # pin a release hooks: - id: gitleaks $ pip install pre-commit $ pre-commit install # from now on, every git commit is checked - Turn on GitHub's secret scanning with push protection (Settings, then Code security). It blocks pushes that contain known token formats.
Dependencies: most of your code isn't yours
A typical app pulls in hundreds of packages, and vulnerabilities are found in them every week. Check them on every build, and let a bot open pull requests for updates:
$ pip-audit # Python: checks installed packages against known vulnerabilities
$ npm audit --audit-level=high # Node: exits non-zero on high or critical issues
# .github/dependabot.yml: weekly update PRs for packages, base images and actions
version: 2
updates:
- package-ecosystem: "pip"
directory: "/"
schedule: { interval: "weekly" }
- package-ecosystem: "docker"
directory: "/"
schedule: { interval: "weekly" }
- package-ecosystem: "github-actions"
directory: "/"
schedule: { interval: "weekly" }
Small, frequent updates are easy to review and test. A year of skipped updates turns into one scary upgrade nobody wants to do.
Your own code: static analysis
SAST (static application security testing) reads source code for risky patterns: SQL built by string concatenation, user input passed to a shell, disabled TLS checks. CodeQL is free for public repositories on GitHub (Security tab, then Code scanning; the "default setup" needs no YAML at all). Semgrep is another popular choice. Expect some false alarms; review each finding, fix or dismiss it with a reason.
Infrastructure as code: scan the config too
A Terraform file can open SSH to the whole internet or make a bucket public without a single line of app code changing. Config scanners catch that before apply:
$ trivy config . # Terraform, Kubernetes YAML, Dockerfiles, Helm charts
main.tf:12
HIGH: Security group rule allows ingress from public internet (0.0.0.0/0) to port 22
Checkov does the same job. Run it in the pull request, next to terraform plan, so the reviewer sees both.
Least privilege for the pipeline itself
CI holds the keys to production, so it's a favourite target. Four habits shrink the damage if a job is ever compromised:
- Give the workflow token only what it needs. Put
permissions: contents: readat the top of every workflow and add more (likepackages: write) only to the job that needs it. - Pin third-party actions to a commit SHA, not a tag:
uses: some/action@8f4b7f8.... Tags can be moved. In March 2025 the tags of a widely used action, tj-actions/changed-files, were pointed at malicious code that printed CI secrets into build logs; workflows pinned to a SHA weren't affected. Dependabot can keep pinned SHAs up to date. - Use OIDC instead of stored cloud keys. With
permissions: id-token: write, the job gets a short-lived token from GitHub, swaps it for temporary AWS credentials, and nothing long-lived is stored anywhere. The IAM role's trust policy decides which repository and branch may use it. - Protect deploys with environments. A GitHub environment called
productionwith required reviewers makes a person approve before any job using its secrets can run.
Putting it together
name: checks
on: [push, pull_request]
permissions:
contents: read
jobs:
security:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with: { fetch-depth: 0 } # full history, so gitleaks sees every commit
- name: Secrets
uses: gitleaks/gitleaks-action@v2
env: { GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" } # repos owned by an organisation also need a free GITLEAKS_LICENSE
- name: Dependencies
run: pip install pip-audit && pip-audit -r requirements.txt
- name: Terraform and Kubernetes config
run: |
curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin
trivy config --severity HIGH,CRITICAL --exit-code 1 .
- name: Image
run: |
docker build -t app:${{ github.sha }} .
trivy image --severity HIGH,CRITICAL --ignore-unfixed --exit-code 1 app:${{ github.sha }}
Make the security job a required check on the main branch (branch protection), and a pull request can't merge until it's green.
Common mistakes
- Scanning but never failing the build. A report nobody reads changes nothing. Fail on what you can act on (high severity, fix available) and track the rest.
- Failing on everything on day one. The team learns to ignore red builds. Start strict on new findings only, then work through the old ones.
- Secrets in logs.
echo $TOKENorset -xin a CI script prints them. GitHub masks secrets it knows about, but not values derived from them. - One admin key for everything. Separate credentials per environment and per job, each with the narrowest permissions that work.
- Forgetting the runtime. A clean scan at build time doesn't stay clean. Rebuild and redeploy regularly so base images pick up fixes.