Skip to content

Commit e03b38c

Browse files
committed
ci: 添加 GitHub Actions 自动构建与 SSH 部署流程
- 新增 Dockerfile(GraalVM Native Image 多阶段构建) - 新增 Caddyfile(反向代理配置,由环境变量驱动) - 新增 .github/workflows/deploy.yml(构建推送 GHCR + SSH 部署) - 修复 docker-compose.yml:pull_policy 改为 always,镜像默认指向 ghcr.io,移除 caddy 多余的 build 段 - 补全 .env.example 中缺失的 Postgres、镜像名等变量
1 parent c28c21a commit e03b38c

5 files changed

Lines changed: 131 additions & 4 deletions

File tree

.env.example

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,16 @@ REDIS_PASSWORD=change-me
3737
# --- 网关与代理 (Caddy) ---
3838
CADDY_SITE_ADDRESS=:80
3939
CADDY_HTTP_PORT=80
40+
CADDY_HTTPS_PORT=443
4041
CADDY_UPSTREAM=backend:8080
42+
43+
# --- Docker 镜像 ---
44+
BACKEND_IMAGE_NAME=ghcr.io/involutionhell/involutionhell-backend:latest
45+
46+
# --- 数据库(本地 Docker postgres 服务)---
47+
POSTGRES_DB=involution_hell
48+
POSTGRES_USER=involution
49+
POSTGRES_PASSWORD=change_me
50+
SPRING_DATASOURCE_URL=jdbc:postgresql://postgres:5432/involution_hell
51+
SPRING_DATASOURCE_USERNAME=involution
52+
SPRING_DATASOURCE_PASSWORD=change_me

.github/workflows/deploy.yml

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
name: 构建并部署后端
2+
3+
on:
4+
push:
5+
branches: [main]
6+
workflow_dispatch:
7+
8+
env:
9+
REGISTRY: ghcr.io
10+
IMAGE_NAME: ghcr.io/involutionhell/involutionhell-backend
11+
12+
jobs:
13+
build-and-push:
14+
name: 编译 Native Image 并推送到 GHCR
15+
runs-on: ubuntu-latest
16+
permissions:
17+
contents: read
18+
packages: write
19+
20+
steps:
21+
- name: 检出代码
22+
uses: actions/checkout@v4
23+
24+
- name: 登录 GitHub Container Registry
25+
uses: docker/login-action@v3
26+
with:
27+
registry: ${{ env.REGISTRY }}
28+
username: ${{ github.actor }}
29+
password: ${{ secrets.GITHUB_TOKEN }}
30+
31+
- name: 设置 Docker Buildx
32+
uses: docker/setup-buildx-action@v3
33+
34+
- name: 构建并推送镜像
35+
uses: docker/build-push-action@v6
36+
with:
37+
context: .
38+
push: true
39+
platforms: linux/amd64
40+
tags: |
41+
${{ env.IMAGE_NAME }}:latest
42+
${{ env.IMAGE_NAME }}:${{ github.sha }}
43+
cache-from: type=gha
44+
cache-to: type=gha,mode=max
45+
46+
deploy:
47+
name: SSH 部署到服务器
48+
runs-on: ubuntu-latest
49+
needs: build-and-push
50+
environment: production
51+
52+
steps:
53+
- name: 检出代码(仅获取 docker-compose.yml 和 Caddyfile)
54+
uses: actions/checkout@v4
55+
56+
- name: 将 compose 文件同步到服务器
57+
uses: appleboy/scp-action@v0.1.7
58+
with:
59+
host: ${{ secrets.SERVER_HOST }}
60+
username: ${{ secrets.SERVER_USER }}
61+
key: ${{ secrets.SERVER_SSH_KEY }}
62+
source: "docker-compose.yml,Caddyfile,docker/"
63+
target: "/opt/involutionhell"
64+
65+
- name: 远程执行部署
66+
uses: appleboy/ssh-action@v1
67+
with:
68+
host: ${{ secrets.SERVER_HOST }}
69+
username: ${{ secrets.SERVER_USER }}
70+
key: ${{ secrets.SERVER_SSH_KEY }}
71+
script: |
72+
cd /opt/involutionhell
73+
74+
# 写入镜像名,确保拉取最新
75+
export BACKEND_IMAGE_NAME=${{ env.IMAGE_NAME }}:${{ github.sha }}
76+
77+
# 登录 GHCR 后拉取镜像并重启服务
78+
echo ${{ secrets.GITHUB_TOKEN }} | docker login ghcr.io -u ${{ github.actor }} --password-stdin
79+
docker compose pull backend
80+
docker compose up -d --remove-orphans

Caddyfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Caddy 反向代理配置
2+
# 通过环境变量控制监听地址和上游服务地址
3+
{$CADDY_SITE_ADDRESS::80} {
4+
# 将所有请求转发至后端 Spring Boot 服务
5+
reverse_proxy {$CADDY_UPSTREAM:backend:8080} {
6+
health_uri /actuator/health
7+
}
8+
}

Dockerfile

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# 多阶段构建:第一阶段用 GraalVM 编译 Native Image
2+
FROM ghcr.io/graalvm/native-image-community:25-muslib AS build
3+
4+
WORKDIR /app
5+
6+
# 先复制 Maven Wrapper 和 pom.xml,利用 Docker 层缓存加速依赖下载
7+
COPY mvnw mvnw.cmd pom.xml ./
8+
COPY .mvn .mvn
9+
10+
RUN chmod +x mvnw && ./mvnw dependency:go-offline -q
11+
12+
# 复制源码并编译 Native Image
13+
COPY src ./src
14+
RUN ./mvnw -DskipTests native:compile-no-fork -q
15+
16+
# 第二阶段:最小化运行镜像
17+
FROM ubuntu:24.04
18+
19+
# 安装运行时依赖(curl 用于 healthcheck)
20+
RUN apt-get update && apt-get install -y --no-install-recommends curl && rm -rf /var/lib/apt/lists/*
21+
22+
WORKDIR /app
23+
COPY --from=build /app/target/backend ./backend
24+
25+
RUN chmod +x ./backend
26+
27+
EXPOSE 8080
28+
29+
ENTRYPOINT ["./backend"]

docker-compose.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
services:
22
backend:
3-
image: ${BACKEND_IMAGE_NAME:-backend:native}
3+
image: ${BACKEND_IMAGE_NAME:-ghcr.io/involutionhell/involutionhell-backend:latest}
44
platform: linux/amd64
5-
pull_policy: never
5+
pull_policy: always
66
container_name: involution-hell-backend
77
restart: always
88
env_file:
@@ -86,8 +86,6 @@ services:
8686
- InvolutionHell-net
8787

8888
caddy:
89-
build:
90-
context: .
9189
image: caddy:2.10-alpine
9290
container_name: involution-hell-caddy
9391
restart: unless-stopped

0 commit comments

Comments
 (0)