96 lines
2.7 KiB
YAML
96 lines
2.7 KiB
YAML
# .gitea/workflows/deploy.yml
|
|
name: Deploy to K3s
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- master
|
|
|
|
jobs:
|
|
deploy:
|
|
runs-on: ubuntu-latest
|
|
|
|
container:
|
|
image: localhost:5000/tiny-ci-runner:latest
|
|
|
|
env:
|
|
IMAGE_TAG: ${{ github.sha }}
|
|
KUBECONFIG: /tmp/.kube/config
|
|
NODE_ENV: production
|
|
|
|
steps:
|
|
- name: Write kubeconfig
|
|
run: |
|
|
mkdir -p /tmp/.kube
|
|
cat << 'EOF' > /tmp/.kube/config
|
|
${{ secrets.KUBECONFIG_DATA }}
|
|
EOF
|
|
chmod 600 /tmp/.kube/config
|
|
|
|
- name: Verify Kubernetes access
|
|
run: |
|
|
kubectl cluster-info
|
|
kubectl get nodes
|
|
|
|
- name: Checkout code
|
|
run: |
|
|
git clone --depth=1 --branch master \
|
|
https://git.tonesc.cn/tone/tonePage.git \
|
|
/workspace/tone/tonePage
|
|
cd /workspace/tone/tonePage
|
|
git log -1 --oneline
|
|
|
|
- name: Build and push backend image
|
|
run: |
|
|
cd /workspace/tone/tonePage/apps/backend
|
|
docker build -t localhost:5000/backend:${IMAGE_TAG} .
|
|
docker push localhost:5000/backend:${IMAGE_TAG}
|
|
|
|
- name: Build and push frontend image
|
|
run: |
|
|
cd /workspace/tone/tonePage/apps/frontend
|
|
docker build \
|
|
--build-arg API_BASE="http://backend-service:3001" \
|
|
-t localhost:5000/frontend:${IMAGE_TAG} .
|
|
docker push localhost:5000/frontend:${IMAGE_TAG}
|
|
|
|
- name: Run database migration job
|
|
run: |
|
|
cd /workspace/tone/tonePage/apps/deploy
|
|
|
|
kubectl delete job backend-migration --ignore-not-found
|
|
|
|
sed "s|IMAGE_TAG|${IMAGE_TAG}|g" backend-migration-job.yaml \
|
|
| kubectl apply -f -
|
|
|
|
kubectl wait \
|
|
--for=condition=complete \
|
|
job/backend-migration \
|
|
--timeout=120s || \
|
|
kubectl logs -l job-name=backend-migration
|
|
|
|
- name: Deploy to K3s
|
|
run: |
|
|
cd /workspace/tone/tonePage/apps/deploy
|
|
|
|
# 基础资源
|
|
kubectl apply -f postgres-deployment.yaml
|
|
kubectl apply -f backend-deployment.yaml
|
|
kubectl apply -f frontend-deployment.yaml
|
|
|
|
# 更新镜像(触发滚动更新)
|
|
kubectl set image deployment/backend \
|
|
backend=localhost:5000/backend:${IMAGE_TAG}
|
|
|
|
kubectl set image deployment/frontend \
|
|
frontend=localhost:5000/frontend:${IMAGE_TAG}
|
|
|
|
# 等待滚动完成
|
|
kubectl rollout status deployment/backend --timeout=120s
|
|
kubectl rollout status deployment/frontend --timeout=120s
|
|
|
|
- name: Post-deploy sanity check
|
|
run: |
|
|
kubectl get pods
|
|
kubectl get svc
|