Files
iot-test-platform/.gitea/workflows/ci-deploy.yml
lzh 461f139731
All checks were successful
iot-test-platform CI/CD / build-and-deploy (push) Successful in 15s
feat:测试平台2.0更新
2025-12-15 10:54:47 +08:00

82 lines
2.7 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: iot-test-platform CI/CD
on:
push:
branches:
- main
jobs:
build-and-deploy:
runs-on: xw-runner
container:
image: catthehacker/ubuntu:act-latest
# Runner 可能已默认挂载 docker.sock如遇权限问题再尝试添加 options: --user root
steps:
# 1. 手动检出代码 (零外部依赖,解决拉取 Action 慢的问题)
- name: Checkout (Native)
run: |
# 清理工作目录
ls -A1 | xargs rm -rf
# 调试信息: 打印一下基本的变量 (注意不要打印 token)
echo "Server: ${{ gitea.server_url }}"
echo "Repo: ${{ gitea.repository }}"
echo "Actor: ${{ gitea.actor }}"
# 既然 clone_url 可能是空的,我们手动拼接
# 1. 去掉 server_url 的协议头 (http:// 或 https://)
SERVER_DOMAIN=$(echo "${{ gitea.server_url }}" | sed -E 's|https?://||')
# 2. 判断使用 http 还是 https
PROTO="http"
if [[ "${{ gitea.server_url }}" == https* ]]; then
PROTO="https"
fi
# 3. 拼接完整 URL: proto://user:token@domain/repo.git
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. 构建镜像 (单机模式,无需 Push)
- name: Build Docker Image
id: build
env:
IMAGE_NAME: 'iot-test-platform'
run: |
SHORT_SHA=$(git log -1 --format='%h')
FULL_IMAGE_NAME="${IMAGE_NAME}:${SHORT_SHA}"
echo "Building $FULL_IMAGE_NAME..."
docker build -t "$FULL_IMAGE_NAME" .
# 标记为 latest 方便后续引用
docker tag "$FULL_IMAGE_NAME" "${IMAGE_NAME}:latest"
echo "image=${IMAGE_NAME}:latest" >> $GITHUB_OUTPUT
# 3. 部署 (同机部署模式)
# 直接在 Runner 所在机器重启容器
- name: Deploy
env:
CONTAINER_NAME: jt808-server
run: |
IMAGE="${{ steps.build.outputs.image }}"
echo "Deploying $IMAGE..."
# 停止并删除旧容器
docker stop $CONTAINER_NAME || true
docker rm $CONTAINER_NAME || true
# 启动新容器
docker run -d \
--name $CONTAINER_NAME \
--restart always \
-p 8080:8080 \
-p 20048:20048 \
"$IMAGE"