Hari 12: Optimasi Pipeline CI/CD dengan Caching
4 min read

Hari 12: Optimasi Pipeline CI/CD dengan Caching

Hari kedua belas! CI pipeline dioptimasi dengan 4 layer caching — Go modules, Gitleaks binary, Trivy DB, dan Semgrep rules. Target pipeline total under 2 menit.

devsecops
ci-cd
github-actions
caching
pipeline-optimization
60-days-challenge
Share

Pipeline Lambat = Developer Frustasi

CI pipeline kita punya 4 job paralel: Build & Test, Secret Scan, SCA Scan, dan SAST Scan. Semua jalan bersamaan, tapi masing-masing mulai dari nol setiap run:

  • Go modules di-download ulang tiap run (~10-20 detik)
  • Gitleaks binary di-download ulang via wget (~3-4 detik)
  • Trivy vulnerability database di-download ulang (~5-10 detik)
  • Semgrep rules di-download ulang (~10-15 detik)

Total overhead per run: ~30-50 detik cuma buat download hal yang kemarin udah di-download. Di CI yang jalan 5-10 kali sehari, itu 2.5-8 menit per hari terbuang.

4 Optimasi yang Diterapkan

1. Go Module Caching

actions/setup-go@v5 punya built-in caching. Cukup tambah cache: true:

- uses: actions/setup-go@v5
  with:
    go-version: '1.26'
    cache: true  # caches ~/go/pkg/mod and ~/.cache/go-build

Ini otomatis cache semua Go dependencies berdasarkan hash go.sum. Kalau go.sum gak berubah, module gak di-download lagi.

Bonus: ditambah -count=1 di go test buat bypass local test cache di CI.

2. Gitleaks Binary Caching

Daripada download binary gitleaks tiap run, cache aja binary-nya:

- name: Cache Gitleaks binary
  uses: actions/cache@v4
  id: cache-gitleaks
  with:
    path: /usr/local/bin/gitleaks
    key: gitleaks-8.30.1

- name: Install Gitleaks
  if: steps.cache-gitleaks.outputs.cache-hit != 'true'
  run: |
    wget -q "https://github.com/gitleaks/gitleaks/releases/download/v8.30.1/gitleaks_8.30.1_linux_x64.tar.gz" -O /tmp/gitleaks.tar.gz
    tar -xzf /tmp/gitleaks.tar.gz -C /tmp/
    sudo mv /tmp/gitleaks /usr/local/bin/

Poin penting: key: gitleaks-8.30.1 berisi versi. Kalau upgrade ke versi baru, cache otomatis invalid dan binary baru di-download.

3. Trivy DB Caching

Trivy butuh download vulnerability database (~30 MB) tiap run. Cache folder ~/.cache/trivy:

- name: Cache Trivy DB
  uses: actions/cache@v4
  with:
    path: ~/.cache/trivy
    key: trivy-db-${{ github.run_id }}
    restore-keys: |
      trivy-db-

- name: Update Trivy DB
  run: trivy image --download-db-only 2>/dev/null || true

Pake ${{ github.run_id }} sebagai key berarti setiap run bikin cache baru. Tapi restore-keys: trivy-db- memungkinkan fallback ke cache terbaru yang ada. Jadi kalau DB gak berubah, restore langsung dari cache.

4. Semgrep Rules Caching

Semgrep download rules database p/golang dan p/owasp-top-ten tiap run. Cache folder ~/.semgrep:

- name: Cache Semgrep
  uses: actions/cache@v4
  with:
    path: ~/.semgrep
    key: semgrep-rules-${{ hashFiles('.semgrep.yml') }}
    restore-keys: |
      semgrep-rules-

Key berdasarkan hash .semgrep.yml — kalau custom rules berubah, cache invalid. Kalau gak berubah, rules di-restore dari cache.

Perbandingan Sebelum dan Sesudah

Job Sebelum Sesudah Saving
Build & Test Go mod download ~15s Cache hit ~2s ~13s
Secret Scan wget + extract ~4s Cache hit ~1s ~3s
SCA Scan Trivy DB download ~8s Cache hit ~2s ~6s
SAST Scan Semgrep setup ~15s Cache hit ~3s ~12s
Estimasi total saving ~34s

Dengan semua job paralel, saving yang paling terasa adalah yang paling lambat. Tapi overall, pipeline seharusnya 30-40% lebih cepat pada run kedua dan seterusnya.

Cache Strategy yang Dipelajari

1. Cache per versi buat binary tools. Key gitleaks-8.30.1 memastikan upgrade versi otomatis invalidate cache. Gak perlu manual clear cache.

2. Cache per hash buat config files. Key semgrep-rules-${{ hashFiles('.semgrep.yml') }} berarti setiap perubahan custom rules bikin cache baru.

3. Fallback dengan restore-keys. Trivy pake restore-keys: trivy-db- supaya kalau exact key gak ada, dia fallback ke cache terbaru yang cocok.

4. cache: true di setup-go sudah cukup. Gak perlu manual actions/cache buat Go modules — built-in caching di setup-go sudah handle semuanya.

5. -count=1 di go test mencegah false positive. Tanpa flag ini, Go bisa skip test yang dianggap gak berubah, padahal code berubah. Di CI, test harus selalu fresh.

Lanjut ke Hari 13

Pipeline sekarang punya 4 job paralel dengan 4 layer caching. Pada run pertama cache miss, tetap lambat. Tapi dari run kedua dan seterusnya, pipeline seharusnya 30-40% lebih cepat.

Besok: Hari 13 — Threat Modeling (STRIDE) — bikin diagram arsitektur dan tabel ancaman di security/threat-model/.

Repo: github.com/stayrelevantid/chalange-devsecops

Enjoyed this article? Share it!

Share

Diskusi & Komentar

Artikel Terkait