ci(woodpecker): 配置构建和部署流水线

- 添加 build.yaml 文件实现 Java 项目 Maven 构建和 Docker 镜像推送
- 添加 deploy.yaml 文件实现远程 SSH 集群部署功能
- 配置 Nexus 私有镜像仓库的推送和拉取地址
- 设置 Maven 构建缓存卷提高构建效率
- 实现多节点集群部署支持
- 配置 SSH 密钥认证和 Docker 登录验证
- 添加容器停止、删除、拉取、运行的完整部署流程
This commit is contained in:
gxl 2026-06-08 10:44:00 +08:00
parent 35a0d6ad5f
commit bd0c156ea0
5 changed files with 114 additions and 0 deletions

43
.woodpecker/build.yaml Normal file
View File

@ -0,0 +1,43 @@
# 遵循文档:不写 kind不写 name直接以 steps 开头
# 要求:
# 1.镜像仓库 nexus, 拉取域名 https://registry.lovestory.cyou (必须要https 并且开启'Allow anonymous'否则拉不下来 ) 推送域名 push.lovestory.cyou
# 2.镜像仓库用户名密码配置在woodpecker, nexus_docker_username nexus_docker_password
# 第 97 次部署
variables:
# --- 仓库与私服配置 ---
- &APP_REGISTRY "push.lovestory.cyou" # 推送镜像的私服域名
- &APP_PULL_REGISTRY "registry.lovestory.cyou" # 拉取基础镜像的私服域名
- &PROJECT_NAME "abhors/demo" # 项目镜像路径与名称
- &PROJECT_PORT "8080" # 运行及映射端口
- &FULL_PUSH_REPO "push.lovestory.cyou/abhors/demo" # 拼接后的完整推送地址
- &FULL_PULL_REPO "push.lovestory.cyou/abhors/demo:latest" # 远程部署拉取的目标地址
- &REPO_MIRROR "https://registry.lovestory.cyou"
steps:
- name: Java 构建
image: library/maven:3.9.6-eclipse-temurin-17
pull: true
commands:
- mvn clean package -DskipTests
volumes:
- /tmp/maven-cache:/root/.m2
when:
event: [push, pull_request]
branch: master
- name: 构建与推送
image: woodpeckerci/plugin-docker-buildx:latest-insecure
privileged: true
volumes:
- /var/run/docker.sock:/var/run/docker.sock
settings:
repo: *FULL_PUSH_REPO
registry: *APP_REGISTRY
username:
from_secret: nexus_docker_username
password:
from_secret: nexus_docker_password
mirror: *REPO_MIRROR
when:
event: push
branch: master

71
.woodpecker/deploy.yaml Normal file
View File

@ -0,0 +1,71 @@
# 第 97 次部署
variables:
# --- 仓库与私服配置 ---
- &APP_REGISTRY "push.lovestory.cyou" # 推送镜像的私服域名
- &APP_PULL_REGISTRY "registry.lovestory.cyou" # 拉取基础镜像的私服域名
- &PROJECT_NAME "abhors/demo" # 项目镜像路径与名称
- &PROJECT_PORT "8080" # 运行及映射端口
- &FULL_PUSH_REPO "push.lovestory.cyou/abhors/demo" # 拼接后的完整推送地址
- &FULL_PULL_REPO "push.lovestory.cyou/abhors/demo:latest" # 远程部署拉取的目标地址
# --- 部署目标机配置 ---
- &CONTAINER_NAME "demo-container"
- &DEPLOY_HOST "43.143.243.136"
- &DEPLOY_PORT "22"
- &DEPLOY_USER_SECRET "136_ssh_user"
- &DEPLOY_PASS_SECRET "136_ssh_password"
# 遵循文档:顶层直接写 steps 和 matrix不写 kind 和 name
matrix:
include:
- NODE_NAME: "集群节点A"
SECRET_NODE_IP: "A_host"
SECRET_USER_NAME: "A_user"
SECRET_PASS_NAME: "A_password"
# - NODE_NAME: "集群节点B"
# SECRET_NODE_IP: "B_host"
# SECRET_USER_NAME: "B_user"
# SECRET_PASS_NAME: "B_password"
steps:
- name: 远程终端集群部署
image: appleboy/drone-ssh:latest
settings:
host:
from_secret: ${SECRET_NODE_IP}
username:
from_secret: ${SECRET_USER_NAME}
password:
from_secret: ${SECRET_PASS_NAME}
secrets: [ nexus_docker_username, nexus_docker_password ] # 这俩属于私密Secret直接进通道
# 💡 遵循官方文档指引 Step 2在 envs 里声明上面这些变量的名字(不区分大小写,插件会自动大写)
# 这样插件就会把这些变量强行同步到远程主机的 SSH 终端进程里
envs:
- REMOTE_HOST
- CONTAINER_NAME
- FULL_PULL_REPO
- APP_REGISTRY
- PROJECT_PORT
- nexus_docker_username
- nexus_docker_password
# 💡 遵循官方文档指引 Step 3在脚本里统一改用标准的 $大写 符号来吃环境变量
script:
- echo "========================================="
- echo "🚀 开始部署节点:$REMOTE_HOST" # 👈 严格按文档走100% 能打印出 IP
- echo "========================================="
# 所有的参数均在远程机器通过系统大写变量精准传参
- docker login --username "$nexus_docker_username" --password "$nexus_docker_password" "$APP_REGISTRY"
- docker stop "$CONTAINER_NAME" || true
- docker rm "$CONTAINER_NAME" || true
- docker pull "$FULL_PULL_REPO"
- docker run -d --name "$CONTAINER_NAME" -p "$PROJECT_PORT":"$PROJECT_PORT" --restart always "$FULL_PULL_REPO"
when:
event: [push, pull_request, manual]
branch: master
depends_on:
- build