From 5e07bf55d7465b8758a3f27a7d84777f6a509ae9 Mon Sep 17 00:00:00 2001 From: Doubleyin <953994191@qq.com> Date: Sat, 27 Sep 2025 03:32:51 +0800 Subject: [PATCH] =?UTF-8?q?=E7=89=88=E6=9C=AC=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy.yml | 92 ++++++++++++++++++++++++++++++++++++ ci-cd-deploy.yml | 49 +++++++++++++++++++ deploy.yml | 33 +++++++++++++ pom.xml | 12 ++++- 4 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/deploy.yml create mode 100644 ci-cd-deploy.yml create mode 100644 deploy.yml diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..903d1fd --- /dev/null +++ b/.github/workflows/deploy.yml @@ -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 \ No newline at end of file diff --git a/ci-cd-deploy.yml b/ci-cd-deploy.yml new file mode 100644 index 0000000..554bb0f --- /dev/null +++ b/ci-cd-deploy.yml @@ -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 \ No newline at end of file diff --git a/deploy.yml b/deploy.yml new file mode 100644 index 0000000..78cade1 --- /dev/null +++ b/deploy.yml @@ -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 \ No newline at end of file diff --git a/pom.xml b/pom.xml index bb2cb17..b1b43d9 100644 --- a/pom.xml +++ b/pom.xml @@ -170,7 +170,7 @@ 3.3.1 - registry.docker-cn.com/openjdk:17-jdk-slim + openjdk:17-jdk-slim light-delivery-app @@ -191,6 +191,16 @@ com.light.delivery.LightApplication + + false + false + + 60000 + 3 + + + ${project.build.directory}/jib-image.tar +