Skip to content

Commit dff7ddd

Browse files
committed
Fix docker compose for production
1 parent ffc6689 commit dff7ddd

2 files changed

Lines changed: 89 additions & 9 deletions

File tree

.github/workflows/deploy.yml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
name: Deploy PCB Backend
2+
3+
on:
4+
push:
5+
branches: [ "main" ] # Trigger saat push ke main
6+
7+
env:
8+
# GANTI DENGAN USERNAME GITHUB KAMU (HURUF KECIL):
9+
IMAGE_NAME: bagus-devlab/pcb-backend-api
10+
REGISTRY: ghcr.io
11+
12+
jobs:
13+
build-and-push:
14+
runs-on: ubuntu-latest
15+
permissions:
16+
contents: read
17+
packages: write
18+
19+
steps:
20+
- name: Checkout Code
21+
uses: actions/checkout@v4
22+
23+
- name: Login to GHCR
24+
uses: docker/login-action@v3
25+
with:
26+
registry: ${{ env.REGISTRY }}
27+
username: ${{ github.actor }}
28+
password: ${{ secrets.GITHUB_TOKEN }}
29+
30+
- name: Build and Push Docker Image
31+
uses: docker/build-push-action@v5
32+
with:
33+
context: ./API-PCB
34+
push: true
35+
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
36+
37+
deploy:
38+
needs: build-and-push
39+
runs-on: ubuntu-latest
40+
41+
steps:
42+
- name: Deploy via SSH
43+
uses: appleboy/ssh-action@v1.0.3
44+
with:
45+
host: ${{ secrets.VPS_HOST }}
46+
username: ${{ secrets.VPS_USER }}
47+
key: ${{ secrets.VPS_KEY }}
48+
port: 22
49+
script: |
50+
# Masuk folder
51+
cd /var/www/pcb-backend/API-PCB
52+
53+
# Login Docker di dalam VPS (Pakai token otomatis)
54+
echo ${{ secrets.GITHUB_TOKEN }} | docker login ghcr.io -u ${{ github.actor }} --password-stdin
55+
56+
# Tarik kodingan terbaru (untuk update docker-compose.yml)
57+
git pull origin main
58+
59+
# Tarik Image Docker terbaru dari GHCR
60+
docker pull ghcr.io/${{ env.IMAGE_NAME }}:latest
61+
62+
# Restart Container
63+
docker-compose down
64+
docker-compose up -d
65+
66+
# Hapus image lama biar hemat disk
67+
docker image prune -f

docker-compose.yml

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ services:
1616
- "5432:5432"
1717
# Healthcheck: Dokter yang ngecek DB sudah siap belum
1818
healthcheck:
19-
# PENTING: Pastikan 'user_pcb' & 'iot_db' sesuai dengan isi file .env kamu!
20-
test: ["CMD-SHELL", "pg_isready -U iot_admin -d iot_db"]
19+
# KOREKSI: Pakai variabel environment biar sinkron sama .env
20+
test: ["CMD-SHELL", "pg_isready -U ${DB_USER} -d ${DB_NAME}"]
2121
interval: 5s
2222
timeout: 5s
2323
retries: 5
@@ -31,22 +31,35 @@ services:
3131

3232
# --- SERVICE BACKEND (FastAPI) ---
3333
web:
34-
build: .
34+
# KOREKSI 1: Matikan build manual, kita pakai Image dari GitHub
35+
# build: .
36+
37+
# KOREKSI 2: Ganti username_github jadi username asli
38+
image: ghcr.io/bagus-devlab/pcb-backend-api:latest
39+
3540
container_name: iot_fastapi
3641
restart: always
37-
command: uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
42+
43+
# KOREKSI 3: Hilangkan --reload untuk production (biar enteng)
44+
command: uvicorn app.main:app --host 0.0.0.0 --port 8000
45+
3846
environment:
3947
- REDIS_URL=redis://redis:6379/0
40-
volumes:
41-
- ./app:/code/app
48+
49+
# KOREKSI 4: Matikan mounting volume code, biar dia pake code yg ada di dalam Image
50+
# volumes:
51+
# - ./app:/code/app
52+
4253
ports:
43-
- "8000:8000"
54+
# KOREKSI 5: Host Port ganti 8006 (karena 8000 dipake Portainer)
55+
- "8006:8000"
56+
4457
env_file:
4558
- .env
46-
# --- PERBAIKAN UTAMA DISINI ---
59+
4760
depends_on:
4861
db:
49-
condition: service_healthy # <--- Backend NUNGGU sampai DB bilang "Sehat"
62+
condition: service_healthy
5063
redis:
5164
condition: service_started
5265

0 commit comments

Comments
 (0)