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/'
|
||||
Loading…
Add table
Add a link
Reference in a new issue