Files
UserbaseFront/.gitea/workflows/ci.yml
T

94 lines
4.0 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
name: ci-userbase-front
# 前端标准化 CI:npm ci -> vite build --mode prod(-> dist/prod) -> docker build(nginx) -> 推 Nexus -> 部署 160
# 触发:push 到 main / 手动 Run workflow
# 依赖仓库 Actions Secrets:NEXUS_USER / NEXUS_PASSWORD(ci-docker 账号,仅 docker-hosted 推送权)
on:
push:
branches: [main]
workflow_dispatch:
inputs:
tag:
description: 'image tag (default = short sha)'
required: false
default: 'auto'
deploy:
description: 'deploy to 160 after build (auto / skip)'
required: false
default: 'auto'
jobs:
build-and-push:
runs-on: ubuntu-latest
# docker:24 自带 docker CLI,复用宿主 docker daemon(act_runner v0.6.1 已挂载 docker.sock)。
container:
image: docker:24
steps:
- name: Install git + node (alpine)
run: apk add --no-cache git nodejs npm
# 内网 Gitea 无法访问 github.com 拉取 actions/checkout,改为直接 clone 本仓库
- name: Checkout (manual, internal Gitea)
run: |
git clone --depth 1 --branch main http://192.168.10.160:3000/${GITHUB_REPOSITORY}.git /tmp/src
cp -a /tmp/src/. .
rm -rf /tmp/src
- name: Compute image tag
id: meta
run: |
if [ "${{ inputs.tag }}" = "auto" ] || [ -z "${{ inputs.tag }}" ]; then
echo "tag=$(echo ${GITHUB_SHA} | cut -c1-7)" >> $GITHUB_OUTPUT
else
echo "tag=${{ inputs.tag }}" >> $GITHUB_OUTPUT
fi
- name: Install deps
run: npm ci
- name: Build (vite --mode prod -> dist/prod)
run: npm run build:prod
- name: Login to Nexus docker registry
run: echo "${{ secrets.NEXUS_PASSWORD }}" | docker login nexus:8083 -u ${{ secrets.NEXUS_USER }} --password-stdin
# 构建上下文 = 仓库根(front),Dockerfile 读取 dist/prod 构建 nginx 静态镜像
- name: Build image
run: docker build -t nexus:8083/docker-hosted/userbase-front:${{ steps.meta.outputs.tag }} -f docker/Dockerfile .
- name: Push image
run: docker push nexus:8083/docker-hosted/userbase-front:${{ steps.meta.outputs.tag }}
- name: Report build
run: echo "pushed nexus:8083/docker-hosted/userbase-front:${{ steps.meta.outputs.tag }}"
# 部署阶段(仅在 push 或手动 dispatch deploy=auto 时执行),运行在 160 的 act_runner 上。
# 前端容器只连 infra-infra-net,由 Caddy(external) 经 reverse_proxy userbase-front:80 转发,无需映射宿主机端口。
- name: Deploy (rollout with rollback)
if: github.event_name == 'push' || inputs.deploy == 'auto'
run: |
set +e
IMG=nexus:8083/docker-hosted/userbase-front:${{ steps.meta.outputs.tag }}
echo "deploying $IMG"
echo "${{ secrets.NEXUS_PASSWORD }}" | docker login nexus:8083 -u ${{ secrets.NEXUS_USER }} --password-stdin
docker pull "$IMG"
RB=userbase-front:rb-$(date +%s)
docker tag userbase-front:latest "$RB" 2>/dev/null || true
docker stop userbase-front 2>/dev/null; docker rm -f userbase-front 2>/dev/null
docker run -d --name userbase-front --restart unless-stopped --network infra-infra-net "$IMG"
OK=0
for i in $(seq 1 20); do
sleep 5
ST=$(docker inspect -f '{{.State.Running}}' userbase-front 2>/dev/null || echo false)
if [ "$ST" = "true" ]; then OK=1; break; fi
done
if [ "$OK" = "0" ]; then
echo "!! UNHEALTHY -> rollback to userbase-front:latest"
docker stop userbase-front 2>/dev/null; docker rm -f userbase-front 2>/dev/null
docker run -d --name userbase-front --restart unless-stopped --network infra-infra-net userbase-front:latest
echo "rolled back"; exit 7
fi
echo "DEPLOY OK: $IMG"
docker tag "$IMG" userbase-front:latest
docker ps --format 'table {{.Names}}\t{{.Status}}\t{{.Image}}' | grep userbase-front || true