Files
aiot-platform/.gitea/workflows/cicd.yaml
lzh 97fef34098
All checks were successful
aiot-platform CI/CD / build-and-deploy (push) Successful in 7m20s
fix: docker部署打开8091端口
2025-12-26 16:19:46 +08:00

129 lines
5.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: aiot-platform CI/CD
on:
push:
branches:
- master
jobs:
build-and-deploy:
runs-on: xw-runner
container:
image: catthehacker/ubuntu:act-latest
# Runner 可能已默认挂载 docker.sock如遇权限问题再尝试添加 options: --user root
steps:
# 1. 手动检出代码
- name: Checkout (Native)
run: |
# 清理工作目录
ls -A1 | xargs rm -rf
# 手动拼接 Git URL
SERVER_DOMAIN=$(echo "${{ gitea.server_url }}" | sed -E 's|https?://||')
PROTO="http"
if [[ "${{ gitea.server_url }}" == https* ]]; then
PROTO="https"
fi
GIT_URL="${PROTO}://${{ gitea.actor }}:${{ gitea.token }}@${SERVER_DOMAIN}/${{ gitea.repository }}.git"
echo "Cloning from ${PROTO}://${SERVER_DOMAIN}/${{ gitea.repository }}.git ..."
git clone --depth 1 "$GIT_URL" .
git log -1 --format='%h - %s'
# 2. 准备 Docker 网络 (使用 1Panel 默认网络)
# 假设 1Panel 创建的网络名为 1panel-network
# 如果不存在,尝试创建 aiot-net (兼容模式)
- name: Setup Network
run: |
if docker network inspect 1panel-network >/dev/null 2>&1; then
echo "NETWORK_NAME=1panel-network" >> $GITHUB_ENV
else
echo "1panel-network not found, creating aiot-net..."
docker network create aiot-net || true
echo "NETWORK_NAME=aiot-net" >> $GITHUB_ENV
fi
# 3. 构建并部署主服务 (Server)
- name: Build & Deploy Server
id: build-server
env:
IMAGE_NAME: 'aiot-server'
CONTAINER_NAME: aiot-server
# 使用 1Panel 管理的实际容器名
MYSQL_HOST: '1Panel-mysql-28oP'
REDIS_HOST: '1Panel-redis-9iYV'
TDENGINE_HOST: '1Panel-tdengine-XGgv'
# 支持通过 Secrets 覆盖其他配置(如密码)
MYSQL_PASSWORD: ${{ secrets.MYSQL_PASSWORD || '65p^VTPi9Qd+' }}
REDIS_PASSWORD: ${{ secrets.REDIS_PASSWORD || '9kHXcZ1ojFsD' }}
TDENGINE_PASSWORD: ${{ secrets.TDENGINE_PASSWORD || 'taosdata' }}
run: |
# --- 构建 ---
SHORT_SHA=$(git log -1 --format='%h')
FULL_IMAGE_NAME="${IMAGE_NAME}:${SHORT_SHA}"
echo "Building Server $FULL_IMAGE_NAME..."
# 使用默认 Dockerfile
docker build -t "$FULL_IMAGE_NAME" -f Dockerfile .
docker tag "$FULL_IMAGE_NAME" "${IMAGE_NAME}:latest"
# --- 部署 ---
echo "Deploying Server..."
echo "Config: MySQL=$MYSQL_HOST, Redis=$REDIS_HOST, TDE=$TDENGINE_HOST, Net=$NETWORK_NAME"
docker stop $CONTAINER_NAME || true
docker rm $CONTAINER_NAME || true
# 注入环境变量,强制切换到 Docker 内网连接
docker run -d \
--name $CONTAINER_NAME \
--network $NETWORK_NAME \
--restart always \
-p 48080:48080 \
-e MYSQL_HOST="$MYSQL_HOST" \
-e MYSQL_PASSWORD="$MYSQL_PASSWORD" \
-e REDIS_HOST="$REDIS_HOST" \
-e TDENGINE_HOST="$TDENGINE_HOST" \
-e TDENGINE_PASSWORD="$TDENGINE_PASSWORD" \
$([ -n "$REDIS_PASSWORD" ] && echo "-e REDIS_PASSWORD=$REDIS_PASSWORD" || echo "") \
"${IMAGE_NAME}:latest"
# 4. 构建并部署 IoT 网关 (Gateway)
- name: Build & Deploy Gateway
id: build-gateway
env:
IMAGE_NAME: 'aiot-gateway'
CONTAINER_NAME: aiot-gateway
# 使用 1Panel 管理的实际容器名
MYSQL_HOST: '1Panel-mysql-28oP'
REDIS_HOST: '1Panel-redis-9iYV'
run: |
# --- 构建 ---
SHORT_SHA=$(git log -1 --format='%h')
FULL_IMAGE_NAME="${IMAGE_NAME}:${SHORT_SHA}"
echo "Building Gateway $FULL_IMAGE_NAME..."
# 使用 Dockerfile.gateway (强制不使用缓存,确保配置更新生效)
docker build --no-cache -t "$FULL_IMAGE_NAME" -f Dockerfile.gateway .
docker tag "$FULL_IMAGE_NAME" "${IMAGE_NAME}:latest"
# --- 部署 ---
echo "Deploying Gateway..."
echo "Gateway Env: RPC=$VIEWSHANGHAI_IOT_GATEWAY_RPC_URL, Redis=$REDIS_HOST"
docker stop $CONTAINER_NAME || true
docker rm $CONTAINER_NAME || true
# 覆盖 RPC 地址,使其指向 aiot-server 容器
# 注入 Redis 配置 (网关需要连 Redis 做消息总线)
docker run -d \
--name $CONTAINER_NAME \
--network $NETWORK_NAME \
--restart always \
-p 1883:1883 \
-p 8091:8091 \
-p 8092:8092 \
-e VIEWSHANGHAI_IOT_GATEWAY_RPC_URL="http://aiot-server:48080" \
-e REDIS_HOST="$REDIS_HOST" \
$([ -n "$REDIS_PASSWORD" ] && echo "-e REDIS_PASSWORD=$REDIS_PASSWORD" || echo "") \
"${IMAGE_NAME}:latest"