版本控制
Some checks failed
构建并部署 Spring Boot 应用 / build-and-deploy (push) Has been cancelled

This commit is contained in:
2025-09-27 03:32:51 +08:00
parent f754e87136
commit 5e07bf55d7
4 changed files with 185 additions and 1 deletions

92
.github/workflows/deploy.yml vendored Normal file
View File

@@ -0,0 +1,92 @@
name: Build and Deploy
on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
cache: maven
- name: Cache Maven packages
uses: actions/cache@v3
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
- name: Build with Maven
run: mvn clean package -DskipTests
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to Docker Registry
if: github.event_name != 'pull_request'
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: light-delivery-app
tags: |
type=ref,event=branch
type=ref,event=pr
type=sha,prefix={{branch}}-
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push Docker image with Jib
run: |
mvn jib:build \
-Djib.to.image=light-delivery-app:${{ github.sha }} \
-Djib.allowInsecureRegistries=true \
-Djib.httpTimeout=60000 \
-Djib.sendCredentialsOverHttp=false
env:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
- name: Deploy to server via SSH
uses: appleboy/ssh-action@v0.1.5
with:
host: 115.190.121.151
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_KEY }}
script: |
# 拉取新镜像
docker pull light-delivery-app:${{ github.sha }}
# 停止并删除旧容器
docker stop light-delivery-app || true
docker rm light-delivery-app || true
# 运行新容器
docker run -d \
--name light-delivery-app \
-p 8080:8080 \
-p 8443:8443 \
-e SPRING_PROFILES_ACTIVE=prod \
-e SPRING_DATASOURCE_URL=jdbc:mysql://115.190.121.151:3306/light_delivery?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true \
-e SPRING_DATASOURCE_USERNAME=double \
-e SPRING_DATASOURCE_PASSWORD=Hu@ng1998! \
-v /etc/ssl/certs:/etc/ssl/certs \
light-delivery-app:${{ github.sha }}
# 清理旧镜像
docker image prune -f

49
ci-cd-deploy.yml Normal file
View File

@@ -0,0 +1,49 @@
# CI/CD Pipeline Deployment Configuration
version: '3.8'
# 构建阶段配置
services:
# 构建服务 - 用于在 CI/CD 环境中构建镜像
builder:
image: maven:3.8-openjdk-17
working_dir: /app
volumes:
- .:/app
- ~/.m2:/root/.m2 # Maven 缓存
environment:
- MAVEN_OPTS=-Xmx1024m
command: >
bash -c "
mvn clean package -DskipTests &&
mvn jib:build -Djib.to.image=light-delivery-app:$${BUILD_NUMBER:-latest} -Djib.allowInsecureRegistries=true
"
# 应用服务 - 实际部署的服务
light-delivery-app:
image: light-delivery-app:${IMAGE_TAG:-latest}
container_name: light-delivery-app
ports:
- "8080:8080"
- "8443:8443"
environment:
- SPRING_PROFILES_ACTIVE=prod
- SPRING_DATASOURCE_URL=jdbc:mysql://115.190.121.151:3306/light_delivery?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
- SPRING_DATASOURCE_USERNAME=double
- SPRING_DATASOURCE_PASSWORD=Hu@ng1998!
- JAVA_OPTS=-Xmx512m
- SERVER_PORT=8080
volumes:
- /etc/ssl/certs:/etc/ssl/certs
restart: unless-stopped
networks:
- light-network
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/actuator/health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 60s
networks:
light-network:
driver: bridge

33
deploy.yml Normal file
View File

@@ -0,0 +1,33 @@
# Deployment configuration for CI/CD pipeline
version: '3.8'
services:
light-delivery-app:
# 使用云端 CI/CD 构建的镜像
image: light-delivery-app:${IMAGE_TAG:-latest}
container_name: light-delivery-app
ports:
- "8080:8080"
- "8443:8443"
environment:
- SPRING_PROFILES_ACTIVE=prod
- SPRING_DATASOURCE_URL=jdbc:mysql://115.190.121.151:3306/light_delivery?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
- SPRING_DATASOURCE_USERNAME=double
- SPRING_DATASOURCE_PASSWORD=Hu@ng1998!
- JAVA_OPTS=-Xmx512m
- SERVER_PORT=8080
volumes:
- /etc/ssl/certs:/etc/ssl/certs
restart: unless-stopped
networks:
- light-network
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/actuator/health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 60s
networks:
light-network:
driver: bridge

12
pom.xml
View File

@@ -170,7 +170,7 @@
<version>3.3.1</version>
<configuration>
<from>
<image>registry.docker-cn.com/openjdk:17-jdk-slim</image>
<image>openjdk:17-jdk-slim</image>
</from>
<to>
<image>light-delivery-app</image>
@@ -191,6 +191,16 @@
<!-- 添加主类配置 -->
<mainClass>com.light.delivery.LightApplication</mainClass>
</container>
<!-- 为云端CI/CD环境配置 -->
<allowInsecureRegistries>false</allowInsecureRegistries>
<sendCredentialsOverHttp>false</sendCredentialsOverHttp>
<!-- CI/CD环境优化配置 -->
<httpTimeout>60000</httpTimeout>
<retries>3</retries>
<!-- 镜像标签配置 -->
<outputPaths>
<tar>${project.build.directory}/jib-image.tar</tar>
</outputPaths>
</configuration>
</plugin>
</plugins>