Automasi adalah Kunci
Manual deployment adalah resep untuk disaster. Saya sudah belajar hal ini dengan cara yang sulit di awal karir. Sekarang, di setiap project yang saya handle, CI/CD pipeline adalah salah satu hal pertama yang saya setup. Artikel ini akan guide kamu dari nol sampai punya pipeline yang production-ready.
Kita akan menggunakan GitHub Actions karena integrasinya yang seamless dan free tier yang cukup untuk kebanyakan project.
GitHub Actions Workflow
Berikut workflow yang saya gunakan untuk backend Go:
# .github/workflows/ci.yml
name: CI/CD Pipeline
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env:
POSTGRES_DB: testdb
POSTGRES_USER: testuser
POSTGRES_PASSWORD: testpass
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
redis:
image: redis:7
ports:
- 6379:6379
steps:
- uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: '1.22'
- name: Download dependencies
run: go mod download
- name: Run linter
uses: golangci/golangci-lint-action@v3
with:
version: latest
- name: Run tests with coverage
run: go test -v -race -coverprofile=coverage.out ./...
env:
DATABASE_URL: postgres://testuser:testpass@localhost:5432/testdb?sslmode=disable
REDIS_URL: localhost:6379
- name: Upload coverage
uses: codecov/codecov-action@v3
with:
file: ./coverage.outDocker Build & Push
Setelah tests pass, pipeline akan build dan push Docker image:
build-and-push:
needs: test
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: |
ghcr.io/${{ github.repository }}:latest
ghcr.io/${{ github.repository }}:${{ github.sha }}
cache-from: type=gha
cache-to: type=gha,mode=maxDeployment ke Kubernetes
Terakhir, pipeline akan deploy ke Kubernetes cluster. Saya menggunakan ArgoCD untuk GitOps approach:
deploy:
needs: build-and-push
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- name: Install kubectl
uses: azure/setup-kubectl@v3
- name: Update Kubernetes manifests
run: |
sed -i "s|IMAGE_TAG|${{ github.sha }}|g" k8s/deployment.yaml
- name: Apply to cluster
run: |
kubectl apply -f k8s/
kubectl rollout status deployment/myapp -n production
env:
KUBECONFIG: ${{ secrets.KUBECONFIG }}Security Best Practices
Beberapa hal keamanan yang tidak boleh dilupakan: gunakan GitHub secrets untuk semua sensitive data, implement OIDC untuk cloud provider authentication (jangan hardcode credentials), dan scan Docker image untuk vulnerability menggunakan Trivy atau Snyk. Pipeline yang aman sama pentingnya dengan code yang aman.