更新 .gitea/workflows/deploy.yml
This commit is contained in:
@@ -1,108 +1,51 @@
|
||||
name: 构建并部署 Spring Boot 应用
|
||||
on: [push]
|
||||
|
||||
jobs:
|
||||
build-and-deploy:
|
||||
runs-on: ubuntu-24.04
|
||||
steps:
|
||||
# 1. 检出代码 - 使用标准的 checkout action
|
||||
- name: 检出代码
|
||||
uses: actions/checkout@v4
|
||||
build-and-deploy:
|
||||
runs-on: ubuntu-24.04 runs-on: ubuntu-24.04
|
||||
steps: steps:
|
||||
# 1. 设置 Node.js(禁用缓存) # 使用本地离线 Action - setup-node
|
||||
- name: 为 Gitea Actions 设置 Node.js 环境 - name: 为 Gitea Actions 设置 Node.js 环境 (离线版)
|
||||
uses: actions/setup-node@v4 uses: /opt/gitea-actions/setup-node/v4.0.2/
|
||||
with: with:
|
||||
node-version: '20' node-version: '20'
|
||||
cache: 'none' # 关键修改:禁用缓存 cache: 'none'
|
||||
# 2. 检出代码 # 使用本地离线 Action - checkout
|
||||
- name: 检出代码 - name: 检出代码 (离线版)
|
||||
uses: actions/checkout@v4 uses: /opt/gitea-actions/checkout/v4.1.1/
|
||||
with:
|
||||
fetch-depth: 1
|
||||
|
||||
# 2. 设置 Java 环境 - 使用标准的 setup-java action
|
||||
- name: 设置 JDK 17
|
||||
uses: actions/setup-java@v4
|
||||
with:
|
||||
java-version: '17'
|
||||
distribution: 'temurin'
|
||||
|
||||
# 3. 设置 Node.js 环境(如果项目需要)
|
||||
- name: 设置 Node.js 环境
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: '20'
|
||||
cache: 'npm' # 启用 npm 缓存加速构建
|
||||
|
||||
# 4. 缓存 Maven 依赖项以加速构建
|
||||
- name: 缓存 Maven 依赖
|
||||
uses: actions/cache@v3
|
||||
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. 登录到 Docker Registry(如果需要推送到镜像仓库)
|
||||
# - name: 登录到 Docker Hub
|
||||
# uses: docker/login-action@v2
|
||||
# with:
|
||||
# username: ${{ secrets.DOCKER_USERNAME }}
|
||||
# password: ${{ secrets.DOCKER_PASSWORD }}
|
||||
|
||||
# 8. 推送 Docker 镜像(可选)
|
||||
# - name: 推送 Docker 镜像
|
||||
# run: |
|
||||
# docker tag light-delivery-app:latest yourusername/light-delivery-app:latest
|
||||
# docker push yourusername/light-delivery-app:latest
|
||||
|
||||
# 9. 部署到服务器
|
||||
- name: 部署到服务器
|
||||
env:
|
||||
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
|
||||
SERVER_IP: ${{ secrets.SERVER_IP }} # 建议将IP地址也存储在secrets中
|
||||
run: |
|
||||
# 设置SSH
|
||||
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
|
||||
|
||||
# 传输Docker镜像到服务器(如果需要)
|
||||
# docker save light-delivery-app:latest | ssh -i ~/.ssh/deploy_key root@$SERVER_IP 'docker load'
|
||||
|
||||
# 在服务器上执行部署命令
|
||||
ssh -i ~/.ssh/deploy_key root@$SERVER_IP '
|
||||
echo "开始部署应用..."
|
||||
|
||||
# 停止并删除现有容器
|
||||
docker stop light-delivery-container || true
|
||||
docker rm light-delivery-container || true
|
||||
|
||||
# 拉取最新镜像(如果使用了镜像仓库)
|
||||
# docker pull yourusername/light-delivery-app:latest
|
||||
|
||||
# 运行新容器
|
||||
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 "✅ 应用部署成功!"
|
||||
|
||||
# 10. 健康检查(可选)
|
||||
- name: 健康检查
|
||||
run: |
|
||||
sleep 30 # 等待应用启动
|
||||
curl -f http://${{ secrets.SERVER_IP }}/api/health || echo "健康检查失败,但继续流程"
|
||||
# 3. 设置 Java 环境 # 使用本地离线 Action - setup-java
|
||||
- name: 设置 JDK 17 - name: 设置 JDK 17 (离线版)
|
||||
uses: actions/setup-java@v4 uses: /opt/gitea-actions/setup-java/v4.2.1/
|
||||
with: with:
|
||||
java-version: '17' java-version: '17'
|
||||
distribution: 'temurin' distribution: 'temurin'
|
||||
# 4. 使用 Maven 打包 # 后续步骤保持不变...
|
||||
- name: 使用 Maven 打包 - name: 使用 Maven 打包
|
||||
run: mvn clean package -DskipTests run: mvn clean package -DskipTests
|
||||
# 5. 构建 Docker 镜像
|
||||
- name: 构建 Docker 镜像 - name: 构建 Docker 镜像
|
||||
run: | run: docker build -t light-delivery-app:latest .
|
||||
docker build -t light-delivery-app:latest .
|
||||
# 6. 部署到服务器
|
||||
- name: 部署到服务器 - name: 部署到服务器
|
||||
env: env:
|
||||
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
|
||||
run: | run: |
|
||||
# 配置SSH环境
|
||||
mkdir -p ~/.ssh mkdir -p ~/.ssh
|
||||
echo "$SSH_PRIVATE_KEY" > ~/.ssh/deploy_key echo "$SSH_PRIVATE_KEY" > ~/.ssh/deploy_key
|
||||
chmod 600 ~/.ssh/deploy_key chmod 600 ~/.ssh/deploy_key
|
||||
echo -e "Host *\n\tStrictHostKeyChecking no\n" > ~/.ssh/config echo -e "Host *\n\tStrictHostKeyChecking no\n" > ~/.ssh/config
|
||||
|
||||
# 部署到服务器
|
||||
ssh -i ~/.ssh/deploy_key root@115.190.121.151 ' ssh -i ~/.ssh/deploy_key root@115.190.121.151 '
|
||||
echo "🚀 开始部署应用..." docker stop light-delivery-container || true
|
||||
docker rm -f light-delivery-container || true docker rm light-delivery-container || true
|
||||
docker run -d \ docker run -d \
|
||||
--name light-delivery-container \ --name light-delivery-container \
|
||||
-p 443:443 \ -p 443:443 \
|
||||
@@ -54,5 +51,4 @@ jobs:
|
||||
-v /etc/ssl/certs:/etc/ssl/certs \ -v /etc/ssl/certs:/etc/ssl/certs \
|
||||
-e KEY_STORE_PASSWORD="$(cat /etc/ssl/certs/keyStorePass.txt)" \ -e KEY_STORE_PASSWORD="$(cat /etc/ssl/certs/keyStorePass.txt)" \
|
||||
light-delivery-app:latest light-delivery-app:latest
|
||||
echo "✅ 部署完成!"
|
||||
' '
|
||||
|
Reference in New Issue
Block a user