mirror of
https://github.com/danagble/anotation-frontend.git
synced 2026-11-16 03:00:22 +00:00
116 lines
4.7 KiB
YAML
116 lines
4.7 KiB
YAML
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 danagble-fe:latest .
|
|
|
|
- name: Setup SSH key and bastion config
|
|
env:
|
|
SSH_PRIVATE_KEY: ${{ secrets.PROD_SSH_KEY }}
|
|
run: |
|
|
set -euo pipefail
|
|
install -m 700 -d ~/.ssh
|
|
# Chuan hoa secret thanh PEM chuan: chiu duoc moi kieu paste hong
|
|
# kieu xuong dong (mot dong voi \n chu, CRLF, space, mat separator).
|
|
# Base64 khong bao gio chua backslash nen backslash chac chan la rac.
|
|
# Dau vao dung '%s' de giu nguyen secret; file dau ra dung '%s\n'
|
|
# vi OpenSSH bat buoc trailing newline (thieu la bao not a key file).
|
|
KEY="$(printf '%s' "$SSH_PRIVATE_KEY" | python3 -c '
|
|
import re, sys
|
|
s = sys.stdin.read()
|
|
s = re.sub(r"\\+n", "\n", s)
|
|
s = s.replace("-----BEGIN OPENSSH PRIVATE KEY-----", "")
|
|
s = s.replace("-----END OPENSSH PRIVATE KEY-----", "")
|
|
body = "".join(s.split())
|
|
sys.stdout.write("-----BEGIN OPENSSH PRIVATE KEY-----\n")
|
|
sys.stdout.write("\n".join(body[i:i + 64] for i in range(0, len(body), 64)))
|
|
sys.stdout.write("\n-----END OPENSSH PRIVATE KEY-----\n")
|
|
')"
|
|
printf 'secret bytes=%s lines=%s\n' "$(printf '%s' "$SSH_PRIVATE_KEY" | wc -c)" "$(printf '%s' "$SSH_PRIVATE_KEY" | wc -l)"
|
|
printf '%s\n' "$KEY" | tr -d '\r' > ~/.ssh/id_ed25519
|
|
chmod 600 ~/.ssh/id_ed25519
|
|
# Fail som neu key hong: in armor + fingerprint (khong lo secret) de doi chieu.
|
|
head -n 1 ~/.ssh/id_ed25519
|
|
tail -n 1 ~/.ssh/id_ed25519
|
|
ssh-keygen -lf ~/.ssh/id_ed25519
|
|
cat > ~/.ssh/config <<'EOF'
|
|
Host bastion
|
|
HostName 103.195.236.32
|
|
User root
|
|
IdentityFile ~/.ssh/id_ed25519
|
|
StrictHostKeyChecking accept-new
|
|
Host prod
|
|
HostName 100.109.54.207
|
|
User neurosus
|
|
IdentityFile ~/.ssh/id_ed25519
|
|
ProxyJump bastion
|
|
StrictHostKeyChecking accept-new
|
|
EOF
|
|
chmod 600 ~/.ssh/config
|
|
|
|
- name: Transfer bundle to prod
|
|
# scp/sftp bi chan tren duong bastion (Connection closed) nen day
|
|
# moi thu qua exec channel bang stdin pipe: compose qua tar, image
|
|
# thang vao `docker load`, khong can file trung gian.
|
|
run: |
|
|
set -euo pipefail
|
|
echo 'Uploading docker-compose.yml...'
|
|
tar -cf - docker-compose.yml | ssh prod 'mkdir -p ~/danangble && tar -xf - -C ~/danangble'
|
|
echo 'Uploading image (~100MB stream, khong co output vai phut la binh thuong)...'
|
|
docker save danagble-fe:latest | ssh prod 'docker load'
|
|
echo 'Upload done.'
|
|
|
|
- name: Roll out (docker load + compose up)
|
|
run: |
|
|
set -euo pipefail
|
|
ssh prod '
|
|
set -euo pipefail
|
|
cd ~/danangble
|
|
docker images danagble-fe
|
|
docker compose up -d --remove-orphans
|
|
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:3003/'
|