|
| 1 | +# 🛡️ Checkpoint 18 — Secure CI/CD Pipelines and Branch Protection |
| 2 | + |
| 3 | +## 🎯 Objective |
| 4 | + |
| 5 | +Understand how modern DevSecOps teams secure their CI/CD pipelines by integrating automated security scanning, branch protection rules, and deployment safeguards. |
| 6 | + |
| 7 | +This checkpoint focused on answering an important question: |
| 8 | + |
| 9 | +> "How do companies prevent insecure code from reaching production?" |
| 10 | +
|
| 11 | +To answer that question, I explored GitHub Actions, Trivy container scanning, branch protection rules, and security gates that automatically block deployments when critical vulnerabilities are detected. |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +# 📚 Topics Covered |
| 16 | + |
| 17 | +- GitHub Actions workflows |
| 18 | +- CI/CD pipelines |
| 19 | +- Docker image scanning with Trivy |
| 20 | +- Security gates |
| 21 | +- Branch protection rules |
| 22 | +- Required status checks |
| 23 | +- Pull request enforcement |
| 24 | +- CodeQL integration |
| 25 | +- Secure deployment workflows |
| 26 | +- GitHub repository rules |
| 27 | + |
| 28 | +--- |
| 29 | + |
| 30 | +# 🤔 Questions Explored |
| 31 | + |
| 32 | +- What happens when a CI pipeline fails? |
| 33 | +- Can GitHub Actions automatically stop deployments? |
| 34 | +- Why did GitHub still allow merging even when Trivy failed? |
| 35 | +- What is the difference between automation and enforcement? |
| 36 | +- How do companies protect their `main` branch? |
| 37 | +- How do pull requests interact with CI/CD pipelines? |
| 38 | +- Why do workflows need both `push` and `pull_request` triggers? |
| 39 | +- How do security scanners prevent vulnerable containers from being deployed? |
| 40 | + |
| 41 | +--- |
| 42 | + |
| 43 | +# 🔬 Labs and Experiments |
| 44 | + |
| 45 | +## 1. Added Trivy Container Scanning |
| 46 | + |
| 47 | +Integrated Trivy into the GitHub Actions workflow. |
| 48 | + |
| 49 | +```yaml |
| 50 | +- name: Scan Docker image with Trivy |
| 51 | + uses: aquasecurity/trivy-action@master |
| 52 | + with: |
| 53 | + image-ref: my-first-image |
| 54 | + format: table |
| 55 | + severity: CRITICAL |
| 56 | + exit-code: 1 |
| 57 | +``` |
| 58 | +
|
| 59 | +--- |
| 60 | +
|
| 61 | +## 2. Configured Security Gates |
| 62 | +
|
| 63 | +The pipeline was configured to fail automatically whenever Trivy detected critical vulnerabilities. |
| 64 | +
|
| 65 | +Pipeline flow: |
| 66 | +
|
| 67 | +```text |
| 68 | +Push code |
| 69 | + ↓ |
| 70 | +Install dependencies |
| 71 | + ↓ |
| 72 | +Run tests |
| 73 | + ↓ |
| 74 | +Build Docker image |
| 75 | + ↓ |
| 76 | +Scan image with Trivy |
| 77 | + ↓ |
| 78 | +Critical vulnerability found? |
| 79 | + |
| 80 | +YES → Stop deployment ❌ |
| 81 | + |
| 82 | +NO → Continue ✅ |
| 83 | +``` |
| 84 | + |
| 85 | +--- |
| 86 | + |
| 87 | +## 3. Created Branch Protection Rules |
| 88 | + |
| 89 | +Protected the `main` branch by enabling: |
| 90 | + |
| 91 | +- Require pull requests before merging |
| 92 | +- Require status checks to pass |
| 93 | +- Require pull request approvals |
| 94 | +- Require conversation resolution |
| 95 | +- Restrict branch deletion |
| 96 | +- Block force pushes |
| 97 | + |
| 98 | +Required checks: |
| 99 | + |
| 100 | +- CodeQL |
| 101 | +- Node CI |
| 102 | + |
| 103 | +--- |
| 104 | + |
| 105 | +## 4. Simulated a Broken Pipeline |
| 106 | + |
| 107 | +Intentionally broke the test suite: |
| 108 | + |
| 109 | +```javascript |
| 110 | +if (2 + 2 !== 5) { |
| 111 | + throw new Error('❌ Test failed!'); |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +GitHub Actions correctly failed: |
| 116 | + |
| 117 | +```text |
| 118 | +Error: Process completed with exit code 1. |
| 119 | +``` |
| 120 | + |
| 121 | +The pull request merge button became disabled. |
| 122 | + |
| 123 | +--- |
| 124 | + |
| 125 | +## 5. Fixed Workflow Triggers |
| 126 | + |
| 127 | +Updated the workflow configuration: |
| 128 | + |
| 129 | +Before: |
| 130 | + |
| 131 | +```yaml |
| 132 | +on: push |
| 133 | +``` |
| 134 | +
|
| 135 | +After: |
| 136 | +
|
| 137 | +```yaml |
| 138 | +on: |
| 139 | + push: |
| 140 | + pull_request: |
| 141 | +``` |
| 142 | +
|
| 143 | +This ensured that the CI pipeline runs on both pushes and pull requests. |
| 144 | +
|
| 145 | +--- |
| 146 | +
|
| 147 | +## 6. Tested Security Enforcement |
| 148 | +
|
| 149 | +Observed that: |
| 150 | +
|
| 151 | +- Failing CI pipelines prevent pull requests from being merged. |
| 152 | +- Trivy blocks the workflow when critical vulnerabilities are found. |
| 153 | +- Branch protection rules enforce repository policies. |
| 154 | +- CodeQL and GitHub Actions work together to secure deployments. |
| 155 | +
|
| 156 | +--- |
| 157 | +
|
| 158 | +# 🧠 Key Concepts |
| 159 | +
|
| 160 | +## CI/CD Automation vs Enforcement |
| 161 | +
|
| 162 | +Automation: |
| 163 | +
|
| 164 | +```text |
| 165 | +Code pushed |
| 166 | + ↓ |
| 167 | +Run tests |
| 168 | + ↓ |
| 169 | +Run security scans |
| 170 | +``` |
| 171 | + |
| 172 | +Enforcement: |
| 173 | + |
| 174 | +```text |
| 175 | +Security scan fails |
| 176 | + ↓ |
| 177 | +GitHub blocks merge |
| 178 | +``` |
| 179 | + |
| 180 | +CI workflows alone do not protect a repository. |
| 181 | + |
| 182 | +Branch protection rules transform warnings into enforced policies. |
| 183 | + |
| 184 | +--- |
| 185 | + |
| 186 | +## Security Gates |
| 187 | + |
| 188 | +A security gate is an automated checkpoint that determines whether code is allowed to continue through the deployment pipeline. |
| 189 | + |
| 190 | +Examples: |
| 191 | + |
| 192 | +- Unit tests |
| 193 | +- CodeQL |
| 194 | +- Trivy |
| 195 | +- Pull request approvals |
| 196 | +- Branch protection rules |
| 197 | + |
| 198 | +--- |
| 199 | + |
| 200 | +## DevSecOps Pipeline |
| 201 | + |
| 202 | +Final pipeline architecture: |
| 203 | + |
| 204 | +```text |
| 205 | +Developer |
| 206 | + ↓ |
| 207 | +GitHub Push |
| 208 | + ↓ |
| 209 | +GitHub Actions |
| 210 | + ↓ |
| 211 | +Run Tests |
| 212 | + ↓ |
| 213 | +Build Docker Image |
| 214 | + ↓ |
| 215 | +Trivy Scan |
| 216 | + ↓ |
| 217 | +CodeQL Analysis |
| 218 | + ↓ |
| 219 | +Branch Protection Rules |
| 220 | + ↓ |
| 221 | +Docker Hub / Production |
| 222 | +``` |
| 223 | + |
| 224 | +--- |
| 225 | + |
| 226 | +# 💭 Reflection |
| 227 | + |
| 228 | +This checkpoint completely changed how I think about deployments. |
| 229 | + |
| 230 | +Before this lab, I assumed that CI/CD pipelines automatically prevented insecure code from reaching production. |
| 231 | + |
| 232 | +I learned that: |
| 233 | + |
| 234 | +- Security scanners only detect problems. |
| 235 | +- CI workflows only automate tasks. |
| 236 | +- Branch protection rules enforce security policies. |
| 237 | +- Deployment safety comes from combining automation and enforcement. |
| 238 | + |
| 239 | +One of the biggest realizations was understanding that companies do not rely solely on developers to make correct decisions. |
| 240 | + |
| 241 | +Instead, they build systems that automatically prevent dangerous code from being merged or deployed. |
| 242 | + |
| 243 | +I also learned that security policies depend on context: |
| 244 | + |
| 245 | +- Banking and healthcare systems may block all critical vulnerabilities. |
| 246 | +- Startups may allow deployments while generating warnings. |
| 247 | +- DevSecOps is ultimately about balancing security and productivity. |
| 248 | + |
| 249 | +--- |
| 250 | + |
| 251 | +# 🚀 Next Checkpoint |
| 252 | + |
| 253 | +- OWASP Security Practices |
| 254 | +- Secure coding principles |
| 255 | +- Common web vulnerabilities |
| 256 | +- Applying OWASP concepts to GitHub Actions, Docker, and CI/CD pipelines |
| 257 | + |
| 258 | +Phase 4 is almost complete. |
0 commit comments