ci
Some checks failed
Build and Deploy Spring Boot Application / deploy (push) Has been cancelled
Build and Deploy Spring Boot Application / build (push) Has been cancelled

This commit is contained in:
huertian 2025-01-08 20:19:54 +08:00
parent 44a25e65a5
commit 6eb7e12030

View File

@ -1,62 +1,73 @@
name: 自动部署 Spring Boot 项目到 CentOS 🚀 name: Build and Deploy Spring Boot Application
on: on:
push: push:
branches: branches: ["main"]
- main pull_request:
branches: ["main"]
jobs: jobs:
deploy: build:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
# 1⃣ 检出代码 - name: Check out the repository
- name: 🛠️ 检出代码
uses: actions/checkout@v3 uses: actions/checkout@v3
# 2⃣ 设置 Java 环境 - name: Set up JDK 17
- name: ☕ 设置 Java 环境
uses: actions/setup-java@v3 uses: actions/setup-java@v3
with: with:
java-version: "17" java-version: "17"
distribution: "temurin"
# 3⃣ 构建 Spring Boot 项目 - name: Cache Maven packages
- name: 📦 构建 Spring Boot 项目 uses: actions/cache@v3
run: | with:
./mvnw clean package -DskipTests path: ~/.m2/repository
ls -lh target/ key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-
# 4⃣ 获取 JAR 文件路径 - name: Install dependencies
- name: 获取 JAR 文件路径 run: mvn install -DskipTests
id: jar_path
run: echo "JAR_FILE=$(ls target/*.jar)" >> $GITHUB_ENV
# 5⃣ 确保目标目录存在 - name: Run tests
- name: 🗂️ 确保远程路径 run: mvn test
run: ssh root@8.137.89.177 "mkdir -p /huertian && chmod 755 /huertian"
# 6⃣ 上传和部署 - name: Package application
- name: 配置 SSH run: mvn package -DskipTests
run: |
echo "$DEPLOY_KEY" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan -H 8.137.89.177 >> ~/.ssh/known_hosts
# 上传文件 - name: Upload artifact
scp ${{ env.JAR_FILE }} root@8.137.89.177:/huertian/app.jar uses: actions/upload-artifact@v3
with:
name: spring-boot-app
path: target/*.jar
# 校验文件完整性 deploy:
ssh root@8.137.89.177 "sha256sum /huertian/app.jar" needs: build
runs-on: ubuntu-latest
# 重启服务 steps:
ssh root@8.137.89.177 << 'EOF' - name: Download artifact
cd /huertian uses: actions/download-artifact@v3
pkill -f 'java -jar' || true with:
nohup java -jar app.jar --spring.config.location=/huertian/application.yml >> /huertian/log/app_$(date +%F).log 2>&1 & name: spring-boot-app
exit path: target/
EOF
# 7⃣ 检查启动日志 - name: Copy file via scp
- name: 检查启动日志 uses: appleboy/scp-action@v0.1.1
run: ssh root@8.137.89.177 "tail -n 20 /huertian/log/app_$(date +%F).log" with:
host: ${{ secrets.REMOTE_HOST }}
username: ${{ secrets.REMOTE_USER }}
key: ${{ secrets.REMOTE_SSH_KEY }}
source: "target/*.jar"
target: "/path/to/deploy/directory/"
- name: Execute remote command via ssh
uses: appleboy/ssh-action@v0.1.8
with:
host: ${{ secrets.REMOTE_HOST }}
username: ${{ secrets.REMOTE_USER }}
key: ${{ secrets.REMOTE_SSH_KEY }}
script: |
cd /path/to/deploy/directory/
java -jar your-spring-boot-app.jar