Files
aiot-platform-ui/.gitea/workflows/deploy-web.yaml
lzh 874c06fa51
All checks were successful
Web UI CI/CD / build-and-deploy (push) Successful in 3m31s
fix: 优化构建速度,后端容器名
2025-12-18 14:03:54 +08:00

103 lines
3.6 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: Web UI CI/CD
on:
push:
branches:
- master
paths:
# 仅当以下文件变动时触发构建,避免改个 README 也重新发版
- 'apps/web-antd/**'
- 'packages/**'
- 'package.json'
- 'pnpm-lock.yaml'
- 'Dockerfile'
- '.gitea/workflows/deploy-web.yaml'
jobs:
build-and-deploy:
runs-on: xw-runner
# 使用包含 Docker 客户端的镜像,这样我们可以在容器内构建镜像 (Docker-in-Docker 模式)
container:
image: catthehacker/ubuntu:act-latest
# 这里的 options 很重要,有时候 Runner 需要特权才能操作宿主机的 Docker
# 如果遇到 permission denied尝试取消注释下一行
# options: --privileged --user root
steps:
# 1. 检出代码
- name: Checkout Code
run: |
# 清理当前目录,防止旧文件干扰
ls -A1 | xargs rm -rf
# 拼接 Clone URL (支持 http 和 https)
SERVER_DOMAIN=$(echo "${{ gitea.server_url }}" | sed -E 's|https?://||')
PROTO="http"
if [[ "${{ gitea.server_url }}" == https* ]]; then PROTO="https"; fi
# 使用 Gitea Token 进行认证
GIT_URL="${PROTO}://${{ gitea.actor }}:${{ gitea.token }}@${SERVER_DOMAIN}/${{ gitea.repository }}.git"
echo "Cloning..."
git clone --depth 1 "$GIT_URL" .
git log -1 --format='%h - %s'
# 2. 准备 Docker 网络
# 确保所有服务在同一个网络下,方便互相访问 (e.g. 前端反向代理到后端)
- name: Create Docker Network
run: |
docker network create aiot-net || true
# 3. 构建并部署
- name: Build & Deploy Web UI
env:
# 镜像名称
IMAGE_NAME: 'aiot-web-antd'
# 容器名称
CONTAINER_NAME: aiot-web-antd
# 宿主机端口
HOST_PORT: 9090
run: |
# --- 构建阶段 ---
SHORT_SHA=$(git log -1 --format='%h')
FULL_IMAGE_NAME="${IMAGE_NAME}:${SHORT_SHA}"
echo "Building Docker Image: $FULL_IMAGE_NAME..."
# 使用根目录的 Dockerfile 进行构建
# Dockerfile 内部使用了多阶段构建,不需要 Runner 安装 Node
# 启用 BuildKit 以支持缓存挂载(加速依赖安装)
DOCKER_BUILDKIT=1 docker build -t "$FULL_IMAGE_NAME" -f Dockerfile .
# 打上 latest 标签
docker tag "$FULL_IMAGE_NAME" "${IMAGE_NAME}:latest"
# --- 部署阶段 ---
echo "Deploying Container: $CONTAINER_NAME..."
# 停止并删除旧容器
docker stop $CONTAINER_NAME || true
docker rm $CONTAINER_NAME || true
# 检查端口占用,如果有其他容器占用,先停止它
PORT_CONTAINER=$(docker ps --filter "publish=${HOST_PORT}" --format "{{.Names}}" | head -1)
if [ ! -z "$PORT_CONTAINER" ]; then
echo "⚠️ Port ${HOST_PORT} is occupied by container: $PORT_CONTAINER"
echo "Stopping conflicting container..."
docker stop $PORT_CONTAINER || true
fi
# 启动新容器
# -d: 后台运行
# --restart always: 开机自启
# -p: 端口映射 宿主机:容器
docker run -d \
--name $CONTAINER_NAME \
--network aiot-net \
--restart always \
-p ${HOST_PORT}:80 \
"${IMAGE_NAME}:latest"
echo "✅ Deployment Successful! Access at port ${HOST_PORT}"