anotation-frontend/.github/workflows/cd.yml
hailt 5f362ec7d3 fix: normalize CD ssh key with literal backslash-n
Secret paste dang 1 dong voi backslash-n chu (thay vi newline that) lam key hong. Tu dung lai newline, giu nguyen dang chuan, validate bang ssh-keygen -lf.
2026-11-02 11:10:14 +07:00

105 lines
3.8 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 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
# Chuan hoa secret truoc khi ghi key:
# - Loc ky tu CR (paste tu Windows/chat hay dinh CRLF).
# - Neu secret bi paste dang 1 dong voi \n chu (thay vi xuong
# dong that) thi dung lai newline that.
KEY="$SSH_PRIVATE_KEY"
if [ "$(printf '%s' "$KEY" | wc -l)" -eq 0 ]; then
case "$KEY" in
*\\n*) KEY="$(printf '%b' "$KEY")" ;;
esac
fi
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
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/'