-
Notifications
You must be signed in to change notification settings - Fork 0
155 lines (131 loc) · 5.67 KB
/
Copy pathdeploy-backend.yml
File metadata and controls
155 lines (131 loc) · 5.67 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
name: CD — Deploy Backend & Database Migration
on:
push:
branches: ["main", "production"]
paths:
- "backend/**"
- ".github/workflows/deploy-backend.yml"
- '!**/**/*.md'
workflow_dispatch: # Memungkinkan trigger deployment secara manual dari GitHub Actions UI
jobs:
build-and-test:
name: 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
build-and-push-ghcr:
name: Build & Push Backend Docker Image to GHCR
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
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
deploy-to-production:
name: Deploy to Server & Run Migrations
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: "${{ secrets.DEPLOY_PATH_BACKEND }}"
- 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 & Git Pull"
echo "=========================================================="
DEPLOY_DIR="${{ secrets.DEPLOY_PATH_BACKEND }}"
echo "Directori target deployment: $DEPLOY_DIR"
cd "$DEPLOY_DIR" || exit 1
# Jika folder server menggunakan git repo, jalankan pull; jika tidak (hanya hasil SCP), abaikan:
if [ -d ".git" ]; then
git fetch --all
git reset --hard origin/main
fi
echo "=========================================================="
echo " [2/5] Menulis file .env di dalam folder backend/"
echo "=========================================================="
cat <<'EOF_ENV' > .env
${{ secrets.ENV_PROD_BACKEND }}
EOF_ENV
mkdir -p backend
cp .env backend/.env
chmod 600 .env backend/.env
echo "=========================================================="
echo " [3/5] Mengunduh Docker Image terbaru dari GHCR"
echo "=========================================================="
# Login ke GHCR dan pull image (agar server tidak membebani CPU/RAM untuk build)
echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u "${{ github.actor }}" --password-stdin || true
docker pull ghcr.io/${{ github.repository }}-backend:latest
echo "=========================================================="
echo " [4/5] Menjalankan Database Migrations via Drizzle ORM"
echo "=========================================================="
# Memastikan proxy_network dan minigrafana-net tersedia di server
docker network inspect proxy_network >/dev/null 2>&1 || docker network create proxy_network
docker network inspect minigrafana-net >/dev/null 2>&1 || docker network create minigrafana-net
# Menjalankan migrasi Drizzle secara terisolasi ke database production
docker run --rm --network minigrafana-net --env-file backend/.env ghcr.io/${{ github.repository }}-backend:latest bun run db:migrate:prod
echo "=========================================================="
echo " [5/5] Merestart Container Aplikasi Backend"
echo "=========================================================="
cd backend && docker compose up -d --force-recreate
echo "✅ Deployment dan Migrasi Database selesai!"