Best Practices Pipeline CI/CD dengan GitHub Actions

Pelajari cara membangun pipeline CI/CD yang efisien dan andal menggunakan GitHub Actions dengan berbagai best practices.

AA

Abiyyu Abidiffatir Al Majid

3 menit baca
Best Practices Pipeline CI/CD dengan GitHub Actions

Mengapa GitHub Actions?

GitHub Actions adalah platform CI/CD yang terintegrasi langsung dengan GitHub. Karena sebagian besar proyek open-source dan private repository sudah di GitHub, menggunakan GitHub Actions berarti tidak perlu layanan CI/CD pihak ketiga — semuanya ada di satu tempat.

Artikel ini membahas best practices yang saya pelajari dari mengelola pipeline untuk puluhan repository di lingkungan produksi.

Struktur Workflow yang Rapi

Pisahkan workflow berdasarkan fungsinya:

.github/workflows/
├── ci.yml          # lint, test, build on PR
├── deploy-staging.yml   # deploy ke staging
├── deploy-prod.yml      # deploy ke production
└── scheduled-tasks.yml  # cron jobs

Contoh workflow CI yang solid:

# .github/workflows/ci.yml
name: CI

on: pull_request: branches: [main, develop]

concurrency: group: ci-${{ github.ref }} cancel-in-progress: true

jobs: lint-and-test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4

- uses: actions/setup-node@v4 with: node-version: '20' cache: 'npm'

- run: npm ci

- name: Lint run: npm run lint

- name: Type check run: npm run type-check

- name: Test run: npm run test -- --coverage

- name: Upload coverage uses: actions/upload-artifact@v4 with: name: coverage path: coverage/

Perhatikan penggunaan concurrency — ini membatalkan workflow sebelumnya jika ada push baru, menghemat menit billing.

Caching untuk Kecepatan

Cache dependency dan build artifact untuk mempercepat pipeline secara signifikan:

- uses: actions/cache@v4
  with:
    path: |
      ~/.npm
      node_modules/.cache
      .next/cache
    key: ${{ runner.os }}-nextjs-${{ hashFiles('/package-lock.json') }}-${{ hashFiles('/.tsx') }}
    restore-keys: |
      ${{ runner.os }}-nextjs-${{ hashFiles('/package-lock.json') }}-
      ${{ runner.os }}-nextjs-

Dengan caching yang tepat, waktu build Next.js saya turun dari 3 menit menjadi kurang dari 40 detik.

Manajemen Secrets

Jangan pernah hardcode secrets. Gunakan GitHub Secrets dan pisahkan berdasarkan environment:

jobs:
  deploy:
    runs-on: ubuntu-latest
    environment: production
    steps:
      - name: Deploy
        env:
          DATABASE_URL: ${{ secrets.DATABASE_URL }}
          API_KEY: ${{ secrets.API_KEY }}
        run: ./deploy.sh

# Untuk secret yang perlu di-file (misal service account) - name: Setup credentials uses: google-github-actions/auth@v2 with: credentials_json: ${{ secrets.GCP_SA_KEY }}

Tips penting:

  • Gunakan Environment Protection Rules untuk production deployment
  • Aktifkan required reviewers* sebelum deploy ke production
  • Rotasi secrets secara berkala

Matrix Builds

Test di berbagai versi Node.js dan OS secara paralel:

test:
  runs-on: ubuntu-latest
  strategy:
    fail-fast: false
    matrix:
      node-version: [18, 20, 22]
      database: [postgres, mysql]
  steps:
    - uses: actions/checkout@v4
    - uses: actions/setup-node@v4
      with:
        node-version: ${{ matrix.node-version }}
    - run: npm ci
    - run: npm test
      env:
        DB_TYPE: ${{ matrix.database }}

Strategi Deployment

Gunakan environment-based deployment dengan approval gates:

deploy-production:
  needs: [lint-and-test, build]
  if: github.ref == 'refs/heads/main'
  runs-on: ubuntu-latest
  environment:
    name: production
    url: https://myapp.com
  steps:
    - uses: actions/download-artifact@v4
      with:
        name: build
        path: .next

- name: Deploy to Vercel uses: amondnet/vercel-action@v25 with: vercel-token: ${{ secrets.VERCEL_TOKEN }} vercel-org-id: ${{ secrets.VERCEL_ORG_ID }} vercel-project-id: ${{ secrets.VERCEL_PROJECT_ID }} vercel-args: '--prod'

Reusable Actions

Untuk menghindari duplikasi, buat reusable composite action:

# .github/actions/setup-project/action.yml
name: 'Setup Project'
description: 'Setup Node.js, install deps, restore cache'
runs:
  using: 'composite'
  steps:
    - uses: actions/setup-node@v4
      with:
        node-version: '20'
        cache: 'npm'
    - run: npm ci
      shell: bash

Lalu gunakan di setiap workflow:

steps:
  - uses: actions/checkout@v4
  - uses: ./.github/actions/setup-project

Kesimpulan

Pipeline CI/CD yang baik adalah investasi jangka panjang. Mulai dari yang sederhana — lint, test, build — lalu tambah caching, matrix builds, dan deployment gates seiring kebutuhan. Yang terpenting: pastikan pipeline kamu cepat (di bawah 5 menit untuk CI) agar developer tidak tergoda untuk melewati checks.

#CI/CD#GitHub Actions#DevOps#Automation
AA

Dibuat oleh

Abiyyu Abidiffatir Al Majid

Software Engineer passionate about building scalable web applications and sharing knowledge about modern web development, system design, and emerging technologies.

Artikel Terkait