From bd0c156ea07985facee1425faf353dc6e3877eed Mon Sep 17 00:00:00 2001 From: gxl Date: Mon, 8 Jun 2026 10:44:00 +0800 Subject: [PATCH] =?UTF-8?q?ci(woodpecker):=20=E9=85=8D=E7=BD=AE=E6=9E=84?= =?UTF-8?q?=E5=BB=BA=E5=92=8C=E9=83=A8=E7=BD=B2=E6=B5=81=E6=B0=B4=E7=BA=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加 build.yaml 文件实现 Java 项目 Maven 构建和 Docker 镜像推送 - 添加 deploy.yaml 文件实现远程 SSH 集群部署功能 - 配置 Nexus 私有镜像仓库的推送和拉取地址 - 设置 Maven 构建缓存卷提高构建效率 - 实现多节点集群部署支持 - 配置 SSH 密钥认证和 Docker 登录验证 - 添加容器停止、删除、拉取、运行的完整部署流程 --- {.woodpecker1 => .woodpecker}/README.md | 0 .../build-aliyun.yaml1 | 0 .woodpecker/build.yaml | 43 +++++++++++ .../deploy-aliyun.yaml1 | 0 .woodpecker/deploy.yaml | 71 +++++++++++++++++++ 5 files changed, 114 insertions(+) rename {.woodpecker1 => .woodpecker}/README.md (100%) rename .woodpecker1/build.yaml => .woodpecker/build-aliyun.yaml1 (100%) create mode 100644 .woodpecker/build.yaml rename .woodpecker1/deploy.yaml => .woodpecker/deploy-aliyun.yaml1 (100%) create mode 100644 .woodpecker/deploy.yaml diff --git a/.woodpecker1/README.md b/.woodpecker/README.md similarity index 100% rename from .woodpecker1/README.md rename to .woodpecker/README.md diff --git a/.woodpecker1/build.yaml b/.woodpecker/build-aliyun.yaml1 similarity index 100% rename from .woodpecker1/build.yaml rename to .woodpecker/build-aliyun.yaml1 diff --git a/.woodpecker/build.yaml b/.woodpecker/build.yaml new file mode 100644 index 0000000..fc571a0 --- /dev/null +++ b/.woodpecker/build.yaml @@ -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 \ No newline at end of file diff --git a/.woodpecker1/deploy.yaml b/.woodpecker/deploy-aliyun.yaml1 similarity index 100% rename from .woodpecker1/deploy.yaml rename to .woodpecker/deploy-aliyun.yaml1 diff --git a/.woodpecker/deploy.yaml b/.woodpecker/deploy.yaml new file mode 100644 index 0000000..bcce6e4 --- /dev/null +++ b/.woodpecker/deploy.yaml @@ -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 \ No newline at end of file