Docker dan CI/CD adalah kombinasi yang sangat powerful. Dengan mengintegrasikan Docker ke GitHub Actions, setiap kali kamu push kode, pipeline akan otomatis: menjalankan tes, membangun Image, mempush ke registry, dan men-deploy ke server. Semua tanpa sentuhan manual.
Di artikel penutup seri Docker ini, kita bangun pipeline CI/CD lengkap dari nol.
Alur CI/CD dengan Docker
Push ke GitHub
│
▼
┌─────────────┐
│ CI: Test │ ← jalankan unit test di container
└──────┬──────┘
│ (jika test lulus)
▼
┌─────────────┐
│ CI: Build │ ← build Docker Image
└──────┬──────┘
│
▼
┌─────────────┐
│ CI: Push │ ← push Image ke registry (GHCR)
└──────┬──────┘
│ (hanya untuk tag/main branch)
▼
┌─────────────┐
│ CD: Deploy │ ← SSH ke server, pull image baru, restart
└─────────────┘
Workflow 1: Test dan Build pada Setiap Pull Request
.github/workflows/ci.yml:
name: CI
on:
pull_request:
branches: [main, develop]
push:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
services:
# Spin up PostgreSQL untuk testing
postgres:
image: postgres:16-alpine
env:
POSTGRES_USER: test
POSTGRES_PASSWORD: testpass
POSTGRES_DB: testdb
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Jalankan migrasi test
run: npx prisma migrate deploy
env:
DATABASE_URL: postgresql://test:testpass@localhost:5432/testdb
- name: Jalankan test
run: npm test
env:
NODE_ENV: test
DATABASE_URL: postgresql://test:testpass@localhost:5432/testdb
JWT_SECRET: test-secret-untuk-ci
build:
needs: test
runs-on: ubuntu-latest
if: github.event_name == 'push'
steps:
- uses: actions/checkout@v4
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build Image (test bahwa build tidak error)
uses: docker/build-push-action@v5
with:
context: .
push: false # hanya build, tidak push
cache-from: type=gha
cache-to: type=gha,mode=max
Workflow 2: Build, Push, dan Deploy saat Tag Dirilis
.github/workflows/release.yml:
name: Release
on:
push:
tags:
- 'v*' # trigger saat push tag seperti v1.0.0, v2.1.3
env:
REGISTRY: ghcr.io
IMAGE_NAME: $
jobs:
build-push:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
outputs:
image_tag: $
steps:
- uses: actions/checkout@v4
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login ke GitHub Container Registry
uses: docker/login-action@v3
with:
registry: $
username: $
password: $
- name: Ekstrak metadata untuk tagging
id: meta
uses: docker/metadata-action@v5
with:
images: $/$
tags: |
type=semver,pattern=
type=semver,pattern=.
type=semver,pattern=
type=sha,prefix=sha-
type=raw,value=latest
- name: Build dan Push Image
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: $
labels: $
cache-from: type=gha
cache-to: type=gha,mode=max
# Build untuk multi-arch (amd64 dan arm64)
platforms: linux/amd64,linux/arm64
deploy:
needs: build-push
runs-on: ubuntu-latest
environment: production # butuh approval manual (opsional)
steps:
- name: Deploy ke server via SSH
uses: appleboy/ssh-action@master
with:
host: $
username: $
key: $
script: |
set -e
cd /var/www/myapp
# Pull image terbaru
echo "$" | \
docker login ghcr.io -u $ --password-stdin
docker compose -f docker-compose.prod.yml pull api
# Deploy dengan zero-downtime
docker compose -f docker-compose.prod.yml up -d api
# Jalankan migrasi
docker compose -f docker-compose.prod.yml exec -T api \
npx prisma migrate deploy
# Bersihkan image lama
docker system prune -f
echo "Deploy selesai: $"
Secrets yang Dibutuhkan
Tambahkan di GitHub Repository → Settings → Secrets and variables → Actions:
| Secret | Keterangan |
|---|---|
SERVER_HOST |
IP atau domain server production |
SERVER_USER |
Username SSH (biasanya ubuntu atau root) |
SSH_PRIVATE_KEY |
Private key SSH untuk autentikasi ke server |
GITHUB_TOKEN sudah otomatis tersedia tanpa perlu ditambahkan manual.
Cache Build untuk Mempercepat Pipeline
Layer caching di GitHub Actions menggunakan type=gha (GitHub Actions cache) secara signifikan mempercepat build saat tidak ada perubahan di layer awal:
- name: Build dan Push
uses: docker/build-push-action@v5
with:
cache-from: type=gha # ambil cache dari GitHub Actions
cache-to: type=gha,mode=max # simpan cache ke GitHub Actions
Build pertama: ~5 menit. Build berikutnya (kode berubah, Dockerfile tidak): ~1 menit.
Notifikasi Slack/Discord (Opsional)
- name: Notifikasi deploy berhasil
if: success()
uses: 8398a7/action-slack@v3
with:
status: success
text: ":rocket: Deploy $ berhasil ke production!"
env:
SLACK_WEBHOOK_URL: $
- name: Notifikasi deploy gagal
if: failure()
uses: 8398a7/action-slack@v3
with:
status: failure
text: ":x: Deploy $ GAGAL! Cek log segera."
env:
SLACK_WEBHOOK_URL: $
Kesimpulan Seri Docker
Dengan pipeline CI/CD yang terintegrasi Docker dan GitHub Actions, seluruh siklus dari kode ke production menjadi otomatis dan reproducible. Developer cukup fokus menulis kode dan membuat tag release — sisanya ditangani pipeline.
Seri tutorial Docker ini telah mencakup:
- Pengenalan Container dan Docker
- Membuat Dockerfile
- Docker Compose untuk multi-container
- Volume dan Network
- Docker Registry
- Deploy ke production
- Development environment
- Docker Security
- Database di Docker
- CI/CD dengan GitHub Actions
Selamat membangun dan mendeploy dengan Docker!
Kiki/🎮🍉⌨️🍩💻