Files
iot-test-platform/.gitea/workflows/ci-deploy.yml
lzh 19820f4ee1
Some checks failed
JT808 CI/CD / build-and-deploy (push) Has been cancelled
fix: cicd-调整5
2025-12-12 16:10:02 +08:00

70 lines
2.2 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: JT808 CI/CD
on:
push:
branches:
- main
jobs:
build-and-deploy:
runs-on: xw-runner
steps:
# 1. 检出代码
# 使用 Gitea 官方镜像 URL避免 Runner 无法访问 GitHub
- name: Checkout
uses: https://gitea.com/actions/checkout@v3
# 2. 登录镜像仓库 (如果有配置)
- name: Login to Docker Registry
if: ${{ secrets.REGISTRY_URL != '' }}
env:
REGISTRY_URL: ${{ secrets.REGISTRY_URL }}
REGISTRY_USERNAME: ${{ secrets.REGISTRY_USERNAME }}
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
run: |
echo "$REGISTRY_PASSWORD" | docker login "$REGISTRY_URL" -u "$REGISTRY_USERNAME" --password-stdin
# 3. 构建并推送镜像
# 利用 Multi-stage Dockerfile无需在宿主机安装 Maven
- name: Build and Push
id: build
env:
REGISTRY_URL: ${{ secrets.REGISTRY_URL }}
# 默认为 jt808-server如果 secret 未配置
IMAGE_NAME: ${{ secrets.IMAGE_NAME || 'jt808-server' }}
run: |
SHORT_SHA=$(git rev-parse --short HEAD)
# 组装完整镜像名: registry.example.com/project/jt808-server:a1b2c3d
FULL_IMAGE_NAME="${REGISTRY_URL}/${IMAGE_NAME}:${SHORT_SHA}"
echo "Building $FULL_IMAGE_NAME..."
docker build -t "$FULL_IMAGE_NAME" .
echo "Pushing $FULL_IMAGE_NAME..."
docker push "$FULL_IMAGE_NAME"
# 输出变量供后续步骤使用
echo "image=$FULL_IMAGE_NAME" >> $GITHUB_OUTPUT
# 4. 部署 (同机部署模式)
# 直接在 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"