demo/.woodpecker.yaml

64 lines
2.3 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# .woodpecker.yaml
# 1. 🛑 顶层统一定义全局变量
vars:
- &NEXUS_REGISTRY "43.143.243.136:8082" # 👈 以后换私服,全篇只改这一个地方!
- &NEXUS_HOSTED "43.143.243.136:8083"
steps:
- name: Java 构建
image: *NEXUS_REGISTRY/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: *NEXUS_REGISTRY/woodpeckerci/plugin-docker-buildx:latest
volumes:
- /var/run/docker.sock:/var/run/docker.sock
settings:
repo: *NEXUS_HOSTED/abhors/demo
registry: *NEXUS_HOSTED
username:
from_secret: nexus_docker_username
password:
from_secret: nexus_docker_password
mirror: http://43.143.243.136:8082
when:
event: push
branch: master
- name: 远程终端部署
image: *NEXUS_REGISTRY/appleboy/drone-ssh:latest
# 💡 核心魔术:通过 environment 将 YAML 锚点变量转换为容器内真正的环境变量
environment:
REGISTRY_VAR: *NEXUS_REGISTRY
settings:
host:
from_secret: 136_ssh_host
username:
from_secret: 136_ssh_user
password:
from_secret: 136_ssh_password
port: 22
secrets: [ nexus_docker_username, nexus_docker_password ]
# 💡 完美抽离 1告诉插件顺便把刚才定义的 REGISTRY_VAR 变量也通过 SSH 跨网带到远程 136 终端去
envs: [ nexus_docker_username, nexus_docker_password, REGISTRY_VAR ]
script:
- echo "========================================="
- echo "🚀 开始部署节点136服务器"
- echo "========================================="
# 💡 完美抽离 2在远程脚本中直接使用 $REGISTRY_VAR 变量代替生硬的 IP 地址!
- docker login --username "$nexus_docker_username" --password "$nexus_docker_password" "$REGISTRY_VAR"
- docker stop demo-container || true
- docker rm demo-container || true
- docker pull "$REGISTRY_VAR"/abhors/demo:latest
- docker run -d --name demo-container -p 8080:8080 --restart always "$REGISTRY_VAR"/abhors/demo:latest
when:
event: push
branch: master