name: 构建并部署 Spring Boot 应用 on: [push] jobs: build-and-deploy: runs-on: ubuntu-24.04 steps: # 1. 检出代码 - 使用 Gitea.com 的镜像 - name: 检出代码 uses: https://gitea.com/actions/checkout@v4 # 使用完整 URL:cite[3]:cite[9] with: fetch-depth: 1 # 2. 设置 Java 环境 - 使用 Gitea.com 的镜像 - name: 设置 JDK 17 uses: https://gitea.com/actions/setup-java@v4 # 使用完整 URL:cite[3]:cite[9] with: java-version: '17' distribution: 'temurin' # 4. 缓存 Maven 依赖项 - 使用 Gitea.com 的镜像 - name: 缓存 Maven 依赖 uses: https://gitea.com/actions/cache@v3 # 使用完整 URL:cite[3]:cite[9] with: path: ~/.m2/repository key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} restore-keys: | ${{ runner.os }}-maven- # 5. 使用 Maven 打包应用 - name: 使用 Maven 打包 run: mvn clean package -DskipTests # 6. 构建 Docker 镜像 - name: 构建 Docker 镜像 run: | docker build -t light-delivery-app:latest . docker images # 7. 部署到服务器 - name: 部署到服务器 env: SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} SERVER_IP: ${{ secrets.SERVER_IP }} run: | mkdir -p ~/.ssh echo "$SSH_PRIVATE_KEY" > ~/.ssh/deploy_key chmod 600 ~/.ssh/deploy_key echo -e "Host *\n\tStrictHostKeyChecking no\n\tUserKnownHostsFile /dev/null" > ~/.ssh/config ssh -i ~/.ssh/deploy_key root@$SERVER_IP ' echo "开始部署应用..." docker stop light-delivery-container || true docker rm light-delivery-container || true docker run -d \ --name light-delivery-container \ --restart unless-stopped \ -p 443:443 \ -p 80:80 \ -v /etc/ssl/certs:/etc/ssl/certs:ro \ -e KEY_STORE_PASSWORD="$(cat /etc/ssl/certs/keyStorePass.txt)" \ light-delivery-app:latest echo "部署完成,清理旧镜像..." docker image prune -f ' echo "✅ 应用部署成功!" # 8. 健康检查(可选) - name: 健康检查 run: | sleep 30 curl -f http://$SERVER_IP/api/health || echo "健康检查失败,但继续流程"