From 0b8b03adf449a33668faaa723e7acccda7bb143f Mon Sep 17 00:00:00 2001 From: lzh Date: Fri, 12 Dec 2025 15:24:41 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20cicd-=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitea/workflows/ci-deploy.yml | 44 +++++++++++----------------------- docs/ci-cd.md | 15 ++++++------ 2 files changed, 21 insertions(+), 38 deletions(-) diff --git a/.gitea/workflows/ci-deploy.yml b/.gitea/workflows/ci-deploy.yml index c93098d..76d1346 100644 --- a/.gitea/workflows/ci-deploy.yml +++ b/.gitea/workflows/ci-deploy.yml @@ -8,49 +8,33 @@ on: jobs: build-and-deploy: runs-on: xw-runner - env: - REGISTRY_URL: ${{ secrets.REGISTRY_URL }} - IMAGE_NAME: ${{ secrets.IMAGE_NAME }} steps: - name: Checkout code - uses: https://github.com/actions/checkout@v4 - - - name: Set up JDK 17 - uses: https://github.com/actions/setup-java@v4 - with: - distribution: temurin - java-version: 17 - - - name: Build Jar - run: mvn -B clean package -DskipTests + uses: https://gitea.com/actions/checkout@v4 - name: Determine image tag id: vars run: | + REGISTRY_URL='${{ secrets.REGISTRY_URL }}' + IMAGE_NAME='${{ secrets.IMAGE_NAME }}' SHA=$(git rev-parse --short HEAD) echo "IMAGE_TAG=${REGISTRY_URL}/${IMAGE_NAME}:${SHA}" >> "$GITHUB_OUTPUT" - - name: Set up Docker Buildx - uses: https://github.com/docker/setup-buildx-action@v3 - - - name: Log in to registry - if: ${{ secrets.REGISTRY_USERNAME && secrets.REGISTRY_PASSWORD }} - uses: https://github.com/docker/login-action@v3 - with: - registry: ${{ secrets.REGISTRY_URL }} - username: ${{ secrets.REGISTRY_USERNAME }} - password: ${{ secrets.REGISTRY_PASSWORD }} + - name: Build Jar (in Maven container) + run: | + docker run --rm \ + -v "$PWD":/workspace \ + -w /workspace \ + maven:3.9.9-eclipse-temurin-17 \ + mvn -B clean package -DskipTests - name: Build and push Docker image - uses: https://github.com/docker/build-push-action@v5 - with: - context: . - push: true - file: Dockerfile - tags: ${{ steps.vars.outputs.IMAGE_TAG }} + run: | + docker build --pull -t "${{ steps.vars.outputs.IMAGE_TAG }}" . + docker push "${{ steps.vars.outputs.IMAGE_TAG }}" - name: Deploy to target host - uses: https://github.com/appleboy/ssh-action@v0.1.5 + uses: https://gitea.com/appleboy/ssh-action@v0.1.5 with: host: ${{ secrets.DEPLOY_HOST }} port: ${{ secrets.DEPLOY_PORT }} diff --git a/docs/ci-cd.md b/docs/ci-cd.md index 931229d..75fca48 100644 --- a/docs/ci-cd.md +++ b/docs/ci-cd.md @@ -8,7 +8,7 @@ ## 前置准备 1. **Runner 环境**:在可访问源码的 CI 节点安装 `gitea-runner`,为本仓库注册一个带标签 `self-hosted` 的 Runner。Runner 需要具备: - - Java 17 + Maven(用于构建)。 + - Docker(用于 Maven 容器构建 Jar、以及构建/推送应用镜像)。本 workflow 通过 `maven:3.9.9-eclipse-temurin-17` 容器执行 Maven,因此 **Runner 主机无需预装 Java/Maven**。 - Docker CLI + `docker buildx` (用于构建、推送镜像)。 - Git 和网络访问(拉取仓库、推送镜像、SSH 连接目标服务器)。 2. **Secrets 配置**:在 Gitea 仓库的 “Settings → Secrets” 下新增(若希望整个组织复用,可在组织层级配置,仓库内可以覆盖同名 Key): @@ -32,16 +32,15 @@ on: > Gitea Actions 使用 `uses: https://github.com/...` 语法来引用 GitHub 上已有的 Action,因此 workflow 中的 Action 即使不是 Gitea 官方的也能被 Runner 下载与执行。 +> 如果你希望 **完全不依赖 GitHub**,可以把 `uses` 指向 `https://gitea.com/actions/...` 的镜像仓库(如 `checkout`),并尽量用 `run:` + `docker` 命令完成构建与推送。 + - **构建阶段**: - - `https://github.com/actions/checkout@v4` 拉取代码。 - - `https://github.com/actions/setup-java@v4` 配置 Temurin JDK 17。 - - 使用 `mvn -B clean package -DskipTests` 生成可执行 Jar。 + - `https://gitea.com/actions/checkout@v4` 拉取代码。 + - 使用 `maven:3.9.9-eclipse-temurin-17` 容器在 CI 中执行 `mvn -B clean package -DskipTests` 生成可执行 Jar。 - **镜像阶段**: - - `https://github.com/docker/setup-buildx-action@v3` 准备多平台构建环境。 - - `https://github.com/docker/login-action@v3` 登陆私有仓库(如配置了 `REGISTRY_USERNAME/PASSWORD`)。 - - `https://github.com/docker/build-push-action@v5` 构建 Docker 镜像并推送:标签格式 `${REGISTRY_URL}/${IMAGE_NAME}:${short-commit}`,通过 `git rev-parse --short HEAD` 动态生成。 + - 直接使用 `docker build` / `docker push` 构建并推送镜像:标签格式 `${REGISTRY_URL}/${IMAGE_NAME}:${short-commit}`,通过 `git rev-parse --short HEAD` 动态生成。 - **部署阶段**: - - `https://github.com/appleboy/ssh-action@v0.1.5` 登录目标主机,执行 `docker pull`、`docker stop`、`docker rm`、`docker run` 的操作,始终以 `jt808-server` 名称运行容器并暴露 `8080`/`20048`。 + - `https://gitea.com/appleboy/ssh-action@v0.1.5` 登录目标主机,执行 `docker pull`、`docker stop`、`docker rm`、`docker run` 的操作,始终以 `jt808-server` 名称运行容器并暴露 `8080`/`20048`。 ## 目标服务器要求