LLMX/.gitea/workflows/deploy.test.yml
gewuyou 7205123043
All checks were successful
CI/CD Pipeline / build-and-deploy (push) Successful in 1m43s
ci(deploy): 添加清理无标签镜像步骤
- 在部署流程结束后增加清理无标签镜像的步骤
- 使用 docker image prune -f 命令快速清理无标签镜像
2025-05-09 11:52:34 +08:00

138 lines
5.1 KiB
YAML
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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: CI/CD Pipeline
on:
push:
branches:
- test # 触发构建的分支
env:
# ========== 环境变量配置 ==========
DOCKER_REGISTRY_URL: ${{vars.DOCKER_REGISTRY_URL}} # 私有Docker镜像仓库地址
PROJECT_NAME: llmx # 项目名称
COMPOSE_FILE: docker/docker-compose.test.yml # Docker compose文件路径
SERVER_PASSWORD: ${{ secrets.SERVER_PASSWORD }} # 仓库密码
JCNC_GITEA_URL: ${{vars.SERVER_GITEA_URL}} # Gitea地址
RUNNER_TOOL_CACHE: /opt/tools-cache # 工具缓存目录
GRADLE_CACHE_KEY: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
SPRING_PROFILES_ACTIVE: test
jobs:
build-and-deploy:
runs-on: ubuntu-latest
container:
image: jcnc/act-runner:latest # 使用自定义Runner镜像
options: --user root # 以root用户运行需要docker权限
steps:
# ========== 1. 代码检出 ==========
- name: 🛒 Checkout source code
uses: ${{env.JCNC_GITEA_URL}}/actions/checkout@v4
with:
fetch-depth: 0 # 获取完整git历史某些插件需要
# ========== 2. Docker环境准备 ==========
- name: 🐳 Install Docker Environment
run: |
echo "=== 检查Docker安装状态 ==="
if ! command -v docker >/dev/null; then
echo "❌ Docker未安装开始安装..."
curl -fsSL https://get.docker.com | sh | tee docker-install.log
echo "✅ Docker安装完成"
echo "✅ Docker Compose安装完成"
else
echo " Docker已安装版本: $(docker -v)"
echo " Docker Compose已安装版本: $(docker compose version)"
fi
# ========== 3. Gradle环境准备 ==========
- name: 🔧 Prepare Gradle Environment
run: |
echo "赋予gradlew执行权限..."
chmod +x gradlew
echo "当前目录结构:"
ls -al
# ========== 4. 恢复缓存 ==========
- name: 📦 Use Cache
id: cache
uses: ${{env.JCNC_GITEA_URL}}/actions/cache/restore@v4
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
~/.cache
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
env:
ACTIONS_RUNNER_DEBUG: true # 启用缓存调试输出
- name: ⚙️ Setup Gradle
uses: ${{env.JCNC_GITEA_URL}}/gradle/actions/setup-gradle@v4
with:
gradle-version: wrapper # 使用项目自带的gradle-wrapper
# ========== 5. 构建阶段 ==========
- name: 🏗️ Build with Jib
run: |
echo "开始构建Docker镜像..."
./gradlew jib --stacktrace --build-cache --info -Dorg.gradle.caching=true -Dorg.gradle.jvmargs="-Xmx2g -Xms2g -XX:MaxMetaspaceSize=1g" | tee build.log
echo "=== 镜像构建结果 ==="
docker images | grep ${{ env.PROJECT_NAME }} || true
- name: 🛑 Stop Gradle Daemon
run: |
echo "停止Gradle守护进程..."
./gradlew --stop
echo "剩余Java进程:"
ps aux | grep java || true
# ========== 6. 保存缓存 ==========
- name: 📦 Save Cache
id: cache
uses: ${{env.JCNC_GITEA_URL}}/actions/cache/save@v4
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
~/.cache
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
env:
ACTIONS_RUNNER_DEBUG: true # 启用缓存调试输出
# ========== 7. 部署阶段 ==========
- name: 🔧 Prepare Deployment
run: |
echo "当前路径..."
pwd && ls -al
echo "准备部署环境..."
chmod +x ${COMPOSE_FILE}
echo "当前Docker状态:"
docker ps -a
- name: 🧹 Clean Old Containers
run: |
echo "当前路径..."
pwd && ls -al
echo "清理旧容器..."
docker compose -f ${COMPOSE_FILE} down --remove-orphans
echo "清理后Docker状态:"
docker ps -a
- name: 🚀 Deploy New Version
run: |
echo "当前路径..."
pwd && ls -al
echo "拉取最新镜像..."
docker compose -f ${COMPOSE_FILE} pull
echo "启动新服务..."
docker compose -f ${COMPOSE_FILE} up -d
echo "=== 服务状态检查 ==="
docker compose -f ${COMPOSE_FILE} ps
- name: 🧼 Cleanup Dangling Images
run: |
echo "开始清理无标签镜像..."
docker image prune -f