This commit is contained in:
92
.github/workflows/deploy.yml
vendored
Normal file
92
.github/workflows/deploy.yml
vendored
Normal 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
|
Reference in New Issue
Block a user