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