How to Set Up a Secure CI/CD Pipeline

DevOps
EmpowerCodes
Oct 31, 2025

Modern software delivery has shifted from periodic releases to continuous integration and continuous delivery (CI/CD) with rapid, frequent updates. While CI/CD accelerates time to market, it also introduces potential security risks if not implemented correctly. A single misconfigured pipeline can expose sensitive credentials, allow unauthorized code into production, or enable supply chain attacks.

To mitigate these risks, organisations must adopt secure CI/CD practices that integrate security controls into every stage of the pipeline. This enables DevSecOps, where development, security, and operations work as a unified approach to deliver secure, reliable software at speed.

This guide walks you through the steps and best practices to set up a secure CI/CD pipeline.

Why CI/CD Security Matters

A CI/CD pipeline handles source code, build artifacts, secrets, dependencies, containers, deployments, and infrastructure. Without proper security, each of these becomes an attack surface.

Key risks of an insecure CI/CD pipeline include:

  • Exposure of credentials, tokens, and keys

  • Malicious code injection into builds or releases

  • Dependency and supply chain vulnerabilities

  • Unauthorized access to pipeline or environments

  • Deployment of unverified or vulnerable artifacts

With growing supply chain attacks such as Codecov, SolarWinds, and dependency hijacking, securing CI/CD pipelines is no longer optional. Security must be embedded from the start, not added later.

Foundations of a Secure CI/CD Pipeline

Before integrating controls, organisations need to follow foundational security principles:

  • Security as Code: Security rules and checks should be defined as code for consistency.

  • Zero Trust: Never trust code, dependencies, or users by default. Always verify.

  • Least Privilege Access: Give pipeline components only the access they need.

  • Shift Left: Detect vulnerabilities early in development rather than at deployment time.

With these principles in place, you can begin implementing security across CI/CD stages.

Step-by-Step Guide to Setting Up a Secure CI/CD Pipeline

1. Secure Source Code Management

Security starts at the repository level. Your version control system (GitHub, GitLab, Bitbucket, Azure Repos) must enforce strict access and code validation rules.

Recommended practices:

  • Enable multi-factor authentication (MFA)

  • Use signed commits and enforce commit signing policies

  • Enable branch protection rules and pull request approvals

  • Conduct mandatory code reviews before merging

  • Scan commits for secrets exposure

Tools like GitGuardian, Gitleaks, or built-in secret scanning features help prevent credential leaks.

2. Secure Build Environments

Build infrastructure needs isolation and integrity. Compromised build agents or runners can inject malicious code into artifacts.

To secure build environments:

  • Use ephemeral build agents that reset after each run

  • Run builds in isolated containers or VMs

  • Do not install unnecessary tools or services on build agents

  • Restrict outbound network access where possible

  • Validate build tools and dependencies

Ephemeral environments prevent attackers from maintaining persistence in CI infrastructure.

3. Manage Secrets Securely

Hardcoding API keys, credentials, or tokens in pipeline configuration files is a common security mistake.

Best practices for secrets management:

  • Store secrets in a secure vault (Vault, AWS Secrets Manager, Azure Key Vault, GitHub or GitLab Secrets)

  • Use short-lived, automatically rotated tokens

  • Encrypt secrets in transit and at rest

  • Avoid sharing secrets across environments

The CI/CD pipeline should load secrets only when needed and ensure they are not logged or stored.

4. Implement Dependency and Supply Chain Security

Modern software depends heavily on third-party libraries. Attackers target dependencies to infiltrate applications.

Secure dependencies by:

  • Enabling Software Composition Analysis (SCA) tools such as Snyk, Dependabot, OWASP Dependency-Check, or Trivy

  • Using package-lock or version-lock files

  • Only using trusted and verified package registries

  • Blocking dependency downloads from unknown or unverified sources

Supply chain security must be continuous and automated at every build.

5. Add Security Testing to CI/CD

Security testing should begin early and occur automatically.

Types of automated security testing:

Test TypePurposeTools
Static Application Security Testing (SAST)Scan source code for vulnerabilitiesSonarQube, CodeQL, Checkmarx
Dynamic Application Security Testing (DAST)Test running apps for security flawsOWASP ZAP, Burp Suite
SCADetect vulnerable dependenciesSnyk, Dependabot, Trivy
IaC ScanningSecure infrastructure-as-codeCheckov, Terrascan, Tfsec
Container ScansScan images for vulnerabilitiesTrivy, Anchore, Clair

Automate these tools in your pipeline to block insecure builds from progressing.

6. Sign and Validate Artifacts

Attackers may attempt to tamper with build artifacts, containers, or packages.

Artifact security includes:

  • Sign all artifacts, binaries, and container images

  • Use immutable artifacts stored in artifact repositories

  • Verify signatures before deployment

Tools like Cosign, Notary, and Sigstore help ensure artifact authenticity.

7. Protect CI/CD with Access Controls and RBAC

Only authorized users and services should have access to CI/CD resources.

Access control recommendations:

  • Use Role-Based Access Control (RBAC)

  • Follow the principle of least privilege

  • Use separate identities for CI/CD bots and services

  • Restrict access to production environments

Audit access logs and enforce the separation of duties for sensitive operations.

8. Enable Continuous Monitoring and Audit Logging

Monitoring pipeline activity ensures visibility and helps detect anomalies or malicious behavior.

Monitor:

  • Pipeline execution logs

  • Access logs and failed authentication attempts

  • Artifact changes and deployments

  • Suspicious branch or configuration changes

Integrate logs with SIEM platforms like Splunk, ELK, or Datadog for real-time alerting.

9. Secure Deployment and Infrastructure

Deployments must follow secure rollout strategies to prevent exploitation or downtime.

Recommendations:

  • Use automated deployment with approvals

  • Implement network segmentation and least privilege access for production systems

  • Protect infrastructure with IaC security scanning

  • Use container security best practices such as non-root containers, minimal base images, and runtime policies

Zero-downtime deployment strategies such as blue-green or canary releases also reduce risk.

10. Incorporate Security into DevOps Culture

Technology alone doesn't secure a pipeline. Teams must adopt a DevSecOps mindset.

Ways to embed security culture:

  • Conduct regular security training for developers

  • Host threat modeling and security design discussions

  • Create reusable secure coding patterns

  • Reward proactive security improvements

Security must be shared by everyone, not siloed.

Common Mistakes to Avoid

  • Treating security as the last step in CI/CD

  • Hardcoding secrets in repositories or pipelines

  • Using persistent build servers without isolation

  • Relying only on manual security reviews

  • Failing to monitor and audit CI/CD systems

A secure pipeline is built incrementally through continuous improvements.

Conclusion

A secure CI/CD pipeline is essential to protecting software, data, and users. By integrating security into every stage of the pipeline, implementing defence-in-depth controls, automating security scans, and adopting strong access and secrets management, organisations can enable DevSecOps and deliver software confidently.

The goal is to create a pipeline that is fast, automated, and secure by design. Start with foundational security measures and expand over time to build a mature, resilient CI/CD security framework.