Files
iot-test-platform/.gitea/workflows/ci-deploy.yml
lzh d82c8b03d9
Some checks failed
JT808 CI/CD / build-and-deploy (push) Failing after 0s
fix: cicd-调整6
2025-12-12 16:12:38 +08:00

81 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: JT808 CI/CD
on:
push:
branches:
- main
jobs:
build-and-deploy:
runs-on: xw-runner
steps:
# 1. 手动检出代码 (零外部依赖,解决拉取 Action 慢的问题)
- name: Checkout (Native)
run: |
# 清理工作目录
ls -A1 | xargs rm -rf
# 使用 Gitea 提供的 Token 直接克隆
# 注意gitea.repository_clone_url 通常是 http://domain/user/repo.git
# 我们在 clone 命令中手动注入用户名和 token
CLONE_URL=$(echo "${{ gitea.repository_clone_url }}" | sed -e "s|://|://${{ gitea.actor }}:${{ gitea.token }}@|")
echo "Cloning from local Gitea..."
git clone --depth 1 "$CLONE_URL" .
git log -1 --format='%h - %s'
# 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"