Files
iot-test-platform/.gitea/workflows/ci-deploy.yml
lzh 26806bac3f
Some checks failed
JT808 CI/CD / build-and-deploy (push) Failing after 1s
fix: cicd-调整9
2025-12-12 16:20:09 +08:00

97 lines
3.4 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
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. 登录镜像仓库 (如果有配置)
- 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"