anotation-frontend/.github/workflows/cd.yml
hailt 8ce9a600dd fix: sanitize CD ssh key (strip CR) and fail fast on bad key
Paste tu Windows/chat hay dinh CRLF lam hong key tren runner (libcrypto). Loc CR, validate bang ssh-keygen -lf, pin IdentityFile.
2026-11-02 11:05:42 +07:00

96 lines
3.4 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
# Loc ky tu CR (secret paste tu Windows/chat hay dinh CRLF lam hong key).
printf '%s\n' "$SSH_PRIVATE_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/'