-
Notifications
You must be signed in to change notification settings - Fork 0
163 lines (140 loc) · 5.91 KB
/
Copy pathdeploy-backend.yml
File metadata and controls
163 lines (140 loc) · 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
name: CD — Build, Push, Deploy Backend
on:
push:
branches: ["main", "production"]
paths:
- "backend/**"
- ".github/workflows/deploy-backend.yml"
- "!**/**/*.md"
workflow_dispatch:
# ✅ 1. WAJIB DEFINISIKAN ENV DI ROOT AGAR TIDAK NYASAR KE DOCKER HUB!
env:
REGISTRY: ghcr.io
# Menggunakan lowercase otomatis untuk menghindari error GHCR uppercase
IMAGE_NAME: ${{ github.repository }}-backend
jobs:
# ==========================================================
# JOB 1: CONTINUOUS INTEGRATION (TYPECHECK & TEST)
# ==========================================================
build-and-test:
name: 1. Build & Test (Bun)
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./backend
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Bun environment
uses: oven-sh/setup-bun@v2
with:
bun-version: latest
- name: Install dependencies
run: bun install --frozen-lockfile
- name: Typecheck & Compile (Build)
run: bun run build
- name: Run Unit Tests
run: bun test
# ==========================================================
# JOB 2: BUILD & PUSH IMAGE KE GHCR
# ==========================================================
build-and-push-ghcr:
name: 2. Build & Push Docker Image to GHCR
needs: build-and-test # ✅ KUNCI: Harus nunggu Job 1 sukses!
runs-on: ubuntu-latest
permissions:
contents: read
packages: write # ✅ KUNCI: Izin push ke GHCR
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GitHub Container Registry (GHCR)
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract Docker metadata (tags, labels)
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=sha,format=short
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push Backend Docker image to GHCR
uses: docker/build-push-action@v6
with:
context: ./backend
file: ./backend/Dockerfile
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
# ==========================================================
# JOB 3: DEPLOY KE SERVER VPS VIA SSH
# ==========================================================
deploy-to-server:
name: 3. Deploy to Server & Run Migrations
needs: build-and-push-ghcr
runs-on: ubuntu-latest
environment: staging
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Copy Backend Docker Compose file via SCP
uses: appleboy/scp-action@v0.1.7
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
port: ${{ secrets.SERVER_PORT || '22' }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
source: "backend/docker-compose.yml"
target: "${{ vars.DEPLOY_PATH_BACKEND || secrets.DEPLOY_PATH_BACKEND }}"
strip_components: 1
- name: Deploy & Execute DB Migrations via SSH
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
port: ${{ secrets.SERVER_PORT || '22' }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
command_timeout: 30m
script_stop: true
script: |
echo "=========================================================="
echo " [1/5] Masuk ke direktori aplikasi"
echo "=========================================================="
DEPLOY_DIR="${{ vars.DEPLOY_PATH_BACKEND || secrets.DEPLOY_PATH_BACKEND }}"
echo "Directori target deployment: $DEPLOY_DIR"
mkdir -p "$DEPLOY_DIR"
cd "$DEPLOY_DIR" || exit 1
echo "=========================================================="
echo " [2/5] Menulis file .env production"
echo "=========================================================="
# Menulis file .env di level direktori yang sama dengan docker-compose.yml
echo "${{ secrets.ENV_PROD_BACKEND }}" | base64 -d > .env
chmod 600 .env
echo "=========================================================="
echo " [3/5] Mengunduh Docker Image terbaru dari GHCR"
echo "=========================================================="
# Ubah nama repositori ke lowercase via bash syntax (GHCR menolak uppercase)
REPO_LOWER=$(echo "${{ github.repository }}-backend" | tr '[:upper:]' '[:lower:]')
IMAGE_TARGET="ghcr.io/${REPO_LOWER}:latest"
echo "Pulling image: $IMAGE_TARGET"
docker pull "$IMAGE_TARGET"
echo "=========================================================="
echo " [4/5] Menjalankan Database Migrations via Drizzle ORM"
echo "=========================================================="
docker compose run --rm backend bun run db:migrate:prod
echo "=========================================================="
echo " [5/5] Merestart Container Aplikasi Backend"
echo "=========================================================="
docker compose up -d --force-recreate --remove-orphans
docker image prune -f
echo "✅ Deployment dan Migrasi Database selesai!"