feat: push-based CD via bastion (build image, scp, compose up)
Runner builds the FE image, transfers image tarball + compose over SSH bastion, prod only docker loads and ups. No repo read access needed on prod.
This commit is contained in:
parent
5e9b2a566d
commit
43c1827fbd
3 changed files with 150 additions and 0 deletions
89
.github/workflows/cd.yml
vendored
Normal file
89
.github/workflows/cd.yml
vendored
Normal file
|
|
@ -0,0 +1,89 @@
|
||||||
|
name: Deploy FE to prod (push-based)
|
||||||
|
|
||||||
|
# Chỉ chạy khi workflow "Frontend CI" trên main đã xanh.
|
||||||
|
# Không cần quyền đọc repo trên prod: image + compose được build
|
||||||
|
# trên runner rồi đẩy thẳng qua bastion.
|
||||||
|
on:
|
||||||
|
workflow_run:
|
||||||
|
workflows: ["Frontend CI"]
|
||||||
|
types: [completed]
|
||||||
|
branches: [main]
|
||||||
|
|
||||||
|
concurrency:
|
||||||
|
group: prod-deploy
|
||||||
|
cancel-in-progress: false
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
deploy:
|
||||||
|
name: Build image, push and roll out via bastion
|
||||||
|
if: ${{ github.event.workflow_run.conclusion == 'success' }}
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
env:
|
||||||
|
# Muốn đổi giá trị build-time thì đặt Variables cùng tên ở repo.
|
||||||
|
# Để trống = dùng default trong Dockerfile.
|
||||||
|
VITE_API_URL: ${{ vars.VITE_API_URL }}
|
||||||
|
VITE_APP_NAME: ${{ vars.VITE_APP_NAME }}
|
||||||
|
VITE_USE_MOCK_DATA: ${{ vars.VITE_USE_MOCK_DATA }}
|
||||||
|
steps:
|
||||||
|
- name: Checkout (đúng commit CI đã test)
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
ref: ${{ github.event.workflow_run.head_sha }}
|
||||||
|
|
||||||
|
- name: Setup Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v3
|
||||||
|
|
||||||
|
- name: Build FE image
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
ARGS=""
|
||||||
|
if [ -n "${VITE_API_URL:-}" ]; then ARGS="$ARGS --build-arg VITE_API_URL=${VITE_API_URL}"; fi
|
||||||
|
if [ -n "${VITE_APP_NAME:-}" ]; then ARGS="$ARGS --build-arg VITE_APP_NAME=${VITE_APP_NAME}"; fi
|
||||||
|
if [ -n "${VITE_USE_MOCK_DATA:-}" ]; then ARGS="$ARGS --build-arg VITE_USE_MOCK_DATA=${VITE_USE_MOCK_DATA}"; fi
|
||||||
|
# shellcheck disable=SC2086
|
||||||
|
docker build $ARGS -t danangble-fe:latest .
|
||||||
|
docker save danangble-fe:latest | gzip -c > /tmp/danangble-fe.tar.gz
|
||||||
|
ls -lh /tmp/danangble-fe.tar.gz
|
||||||
|
|
||||||
|
- name: Setup SSH key and bastion config
|
||||||
|
env:
|
||||||
|
SSH_PRIVATE_KEY: ${{ secrets.PROD_SSH_KEY }}
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
install -m 700 -d ~/.ssh
|
||||||
|
printf '%s\n' "$SSH_PRIVATE_KEY" > ~/.ssh/id_ed25519
|
||||||
|
chmod 600 ~/.ssh/id_ed25519
|
||||||
|
cat > ~/.ssh/config <<'EOF'
|
||||||
|
Host bastion
|
||||||
|
HostName 103.195.236.32
|
||||||
|
User root
|
||||||
|
StrictHostKeyChecking accept-new
|
||||||
|
Host prod
|
||||||
|
HostName 100.109.54.207
|
||||||
|
User neurosus
|
||||||
|
ProxyJump bastion
|
||||||
|
StrictHostKeyChecking accept-new
|
||||||
|
EOF
|
||||||
|
chmod 600 ~/.ssh/config
|
||||||
|
|
||||||
|
- name: Transfer bundle to prod
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
scp -o StrictHostKeyChecking=accept-new /tmp/danangble-fe.tar.gz docker-compose.yml prod:danangble/
|
||||||
|
|
||||||
|
- name: Roll out (docker load + compose up)
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
ssh prod '
|
||||||
|
set -euo pipefail
|
||||||
|
cd ~/danangble
|
||||||
|
docker load -i danangble-fe.tar.gz
|
||||||
|
rm -f danangble-fe.tar.gz
|
||||||
|
docker compose up -d
|
||||||
|
docker image prune -f
|
||||||
|
docker compose ps
|
||||||
|
'
|
||||||
|
|
||||||
|
- name: Health check
|
||||||
|
run: |
|
||||||
|
ssh prod 'curl -fsS -m 10 -o /dev/null -w "fe http_code=%{http_code}\n" http://localhost:3000/'
|
||||||
40
Dockerfile
Normal file
40
Dockerfile
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
# ---- stage 1: build FE ----
|
||||||
|
FROM node:22-alpine AS build
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Biến môi trường Vite (đọc từ .env trên prod lúc compose build).
|
||||||
|
# Đổi API URL -> build lại là nhận (compose up --build tự làm).
|
||||||
|
ARG VITE_API_URL=http://localhost:8000/api/v1
|
||||||
|
ARG VITE_APP_NAME=danable
|
||||||
|
ARG VITE_USE_MOCK_DATA=true
|
||||||
|
ENV VITE_API_URL=$VITE_API_URL \
|
||||||
|
VITE_APP_NAME=$VITE_APP_NAME \
|
||||||
|
VITE_USE_MOCK_DATA=$VITE_USE_MOCK_DATA
|
||||||
|
|
||||||
|
COPY package.json package-lock.json ./
|
||||||
|
RUN npm ci
|
||||||
|
COPY index.html vite.config.ts tsconfig.json tailwind.config.js postcss.config.js ./
|
||||||
|
COPY public ./public
|
||||||
|
COPY src ./src
|
||||||
|
RUN npm run build
|
||||||
|
|
||||||
|
# ---- stage 2: serve tĩnh bằng nginx ----
|
||||||
|
FROM nginx:alpine
|
||||||
|
COPY --from=build /app/dist /usr/share/nginx/html
|
||||||
|
# Ghi đè default conf: SPA fallback + cache lâu cho /assets.
|
||||||
|
# ($$ trong Dockerfile -> $ trong file config.)
|
||||||
|
RUN printf '%s\n' \
|
||||||
|
'server {' \
|
||||||
|
' listen 80;' \
|
||||||
|
' server_name _;' \
|
||||||
|
' root /usr/share/nginx/html;' \
|
||||||
|
' index index.html;' \
|
||||||
|
' location / {' \
|
||||||
|
' try_files $uri $uri/ /index.html;' \
|
||||||
|
' }' \
|
||||||
|
' location /assets/ {' \
|
||||||
|
' expires 30d;' \
|
||||||
|
' add_header Cache-Control "public, immutable";' \
|
||||||
|
' }' \
|
||||||
|
'}' > /etc/nginx/conf.d/default.conf
|
||||||
|
EXPOSE 80
|
||||||
21
docker-compose.yml
Normal file
21
docker-compose.yml
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
# File compose DUY NHẤT trên prod: ~/danangble/docker-compose.yml
|
||||||
|
# CD push-based: CI build image, nén, đẩy image + file này lên prod,
|
||||||
|
# prod chỉ `docker load` rồi `up` — không build, không git pull.
|
||||||
|
# Có service mới (vd: backend) thì thêm vào file này, không tạo file compose khác.
|
||||||
|
name: danangble
|
||||||
|
|
||||||
|
services:
|
||||||
|
fe:
|
||||||
|
image: danangble-fe:latest
|
||||||
|
container_name: danangble-fe
|
||||||
|
restart: unless-stopped
|
||||||
|
ports:
|
||||||
|
- "3000:80"
|
||||||
|
|
||||||
|
# --- Backend (mai mốt thêm, mẫu sẵn) ---
|
||||||
|
# be:
|
||||||
|
# image: danangble-be:latest
|
||||||
|
# container_name: danangble-be
|
||||||
|
# restart: unless-stopped
|
||||||
|
# ports:
|
||||||
|
# - "8000:8000"
|
||||||
Loading…
Add table
Add a link
Reference in a new issue