Build a secure CI/CD pipeline with GitHub Actions: OIDC to cloud (no secrets), Terraform plan and apply gates, SAST, dependency and container scanning.
A secure pipeline removes long-lived secrets, checks code and infrastructure automatically, and requires approval before production changes.
1. Authenticate to the cloud without secrets
Use OpenID Connect so the workflow gets short-lived tokens. Example for AWS:
permissions:
id-token: write
contents: read
steps:
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/gha-deploy
aws-region: eu-west-1Restrict the role trust policy to your repository and branch (sub claim such as repo:org/app:ref:refs/heads/main). Azure and Google Cloud support the same pattern with federated credentials.
2. Pin and minimize
- Pin third-party actions to a full commit SHA.
- Set
permissions:to least privilege at workflow level. - Use protected branches, required reviews and signed commits.
3. Test and scan on every pull request
- Unit tests and linting.
- SAST (CodeQL or Semgrep).
- Dependency scanning (Dependabot,
npm audit,pip-audit). - Secret scanning with push protection.
- Container image scan, for example
trivy image --exit-code 1 --severity HIGH,CRITICAL app:${{ github.sha }}.
4. Infrastructure as code with gates
terraform fmt -check
terraform validate
terraform plan -out=tfplanPost the plan to the pull request, scan it with Checkov or tfsec, and apply only from the main branch using an environment with required reviewers. Store state remotely with locking and encryption.
5. Build once, promote the same artifact
Build the image once, tag with the commit SHA, sign it (Cosign) and generate an SBOM. Promote the identical digest through dev, staging and production; never rebuild per environment.
6. Deploy safely
Use rolling, blue/green or canary releases, automated smoke tests, and automatic rollback on failed health checks. Keep a documented manual rollback.
7. Observe
Emit deployment markers to monitoring, track DORA metrics (deployment frequency, lead time, change failure rate, time to restore) and review failures blamelessly.
Our DevOps engineers design and harden pipelines like this, including DevSecOps controls.
devopssecure ci cd pipeline
