GitHub Actions adalah platform CI/CD yang terintegrasi langsung ke GitHub — tidak perlu setup Jenkins, CircleCI, atau tool terpisah lainnya. Setiap kali ada push, PR, atau event lain di repository, GitHub Actions bisa otomatis menjalankan test, build aplikasi, push Docker image, bahkan deploy ke server production.
Konsep Dasar GitHub Actions
Event (push, PR, schedule)
↓
Workflow (.github/workflows/*.yml)
↓
Job 1 Job 2
(runs-on: ubuntu) (runs-on: ubuntu)
↓ ↓
Step 1: checkout Step 1: checkout
Step 2: setup node Step 2: docker build
Step 3: npm test Step 3: docker push
- Workflow — file YAML yang mendefinisikan seluruh pipeline
- Event — trigger yang menjalankan workflow
- Job — sekumpulan steps yang berjalan di satu runner
- Step — satu perintah atau action yang dijalankan
- Action — unit kode reusable (dari marketplace atau buatan sendiri)
- Runner — VM yang menjalankan job (ubuntu-latest, windows-latest, macos-latest)
Workflow CI Lengkap untuk Node.js
.github/workflows/ci.yml:
name: CI
on:
push:
branches: [main, develop]
pull_request:
branches: [main, develop]
jobs:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- run: npm ci
- run: npm run lint
test:
name: Test
runs-on: ubuntu-latest
needs: lint # jalankan setelah lint berhasil
services:
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-retries 5
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Jalankan migrasi
run: npx prisma migrate deploy
env:
DATABASE_URL: postgresql://test:testpass@localhost:5432/testdb
- name: Jalankan test
run: npm test -- --coverage
env:
NODE_ENV: test
DATABASE_URL: postgresql://test:testpass@localhost:5432/testdb
JWT_SECRET: ci-test-secret
- name: Upload coverage
uses: codecov/codecov-action@v4
with:
token: $
build:
name: Build Docker Image
runs-on: ubuntu-latest
needs: test
if: github.event_name == 'push'
steps:
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3
- uses: docker/login-action@v3
with:
registry: ghcr.io
username: $
password: $
- uses: docker/metadata-action@v5
id: meta
with:
images: ghcr.io/$
tags: |
type=ref,event=branch
type=sha,prefix=sha-
type=raw,value=latest,enable=$
- uses: docker/build-push-action@v5
with:
context: .
push: true
tags: $
cache-from: type=gha
cache-to: type=gha,mode=max
Workflow CD — Deploy ke Production
.github/workflows/deploy.yml:
name: Deploy to Production
on:
push:
tags: ['v*']
jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
environment:
name: production
url: https://api.namakamu.com
steps:
- uses: actions/checkout@v4
- name: Deploy via SSH
uses: appleboy/ssh-action@master
with:
host: $
username: $
key: $
script: |
cd /var/www/api
docker compose pull
docker compose up -d --no-deps api
docker compose exec -T api npx prisma migrate deploy
docker system prune -f
- name: Notifikasi Slack
if: always()
uses: 8398a7/action-slack@v3
with:
status: $
text: |
Deploy $ ke production: $
env:
SLACK_WEBHOOK_URL: $
Matrix Strategy — Test di Banyak Versi Sekaligus
jobs:
test:
strategy:
matrix:
node-version: [18, 20, 22]
os: [ubuntu-latest, windows-latest]
runs-on: $
name: Test Node $ on $
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: $
- run: npm ci
- run: npm test
Ini akan menjalankan 6 job paralel (3 versi Node × 2 OS).
Caching Dependencies
- uses: actions/cache@v4
with:
path: ~/.npm
key: $-node-$
restore-keys: |
$-node-
# Atau lebih simple dengan cache bawaan setup-node
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm' # otomatis cache node_modules
Secrets dan Environment Variables
env:
# Variable biasa (aman ditampilkan di log)
NODE_ENV: production
APP_PORT: 3000
steps:
- name: Deploy
env:
# Secret — tidak pernah tampil di log
DATABASE_URL: $
JWT_SECRET: $
run: ./deploy.sh
Tambahkan secrets di Repository Settings → Secrets and variables → Actions.
Workflow Reusable dan Composite Actions
# .github/workflows/reusable-test.yml
on:
workflow_call:
inputs:
node-version:
type: string
default: '20'
secrets:
DATABASE_URL:
required: true
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: $
- run: npm ci && npm test
env:
DATABASE_URL: $
Gunakan dari workflow lain:
jobs:
test:
uses: ./.github/workflows/reusable-test.yml
with:
node-version: '20'
secrets:
DATABASE_URL: $
Kesimpulan
GitHub Actions menghilangkan kebutuhan untuk setup CI/CD terpisah — semuanya ada di dalam repository yang sama dengan kode. Dengan pemahaman tentang jobs, steps, matrix, caching, dan secrets, kamu bisa membangun pipeline yang menjalankan test, build image, dan deploy ke production secara otomatis setiap kali ada perubahan kode.
Di artikel berikutnya, kita masuki topik-topik Git advanced — submodules, worktrees, dan teknik-teknik yang dibutuhkan untuk skenario yang lebih kompleks.
Kiki/🎮🍉⌨️🍩💻