Files
bl/.woodpecker/my-first-workflow.yaml

210 lines
7.9 KiB
Go
Raw Normal View History

2026-01-27 06:40:49 +00:00
# 触发条件仅pushmanual手动触发分支限定main
2026-01-26 10:14:44 +08:00
when:
2026-01-27 06:40:49 +00:00
event:
- push
- manual
branch: main
2026-01-26 11:07:37 +08:00
2026-01-28 12:15:35 +08:00
skip_clone: true
2026-01-26 14:06:51 +08:00
2026-01-27 12:15:35 +00:00
2026-01-26 14:06:51 +08:00
2026-01-28 12:15:35 +08:00
# 流水线核心步骤理顺依赖链确保各步骤依赖正确
steps:
# ========== 1. 替代clone拉取代码核心依赖 ==========
prepare:
2026-01-30 00:38:22 +08:00
image: alpine/git
2026-01-28 10:52:09 +08:00
environment:
2026-01-30 00:05:51 +08:00
# WOODPECKER_SSH_KEY:
# from_secret: WOODPECKER_SSH_KEY
2026-01-28 12:21:08 +08:00
CNB_ACCK:
from_secret: CNB_ACCK
2026-01-28 10:52:09 +08:00
commands:
2026-01-30 00:04:20 +08:00
# # 调试验证变量是否传递
# - echo "🔍 调试当前环境变量列表筛选SSH相关"
# - env | grep -i ssh || echo "⚠️ 无SSH相关环境变量"
2026-01-28 12:15:35 +08:00
2026-01-30 00:18:32 +08:00
# # 系统初始化
# # 1. 备份原有软件源文件防止出错可恢复
# - cp /etc/apt/sources.list /etc/apt/sources.list.bak
2026-01-30 00:15:44 +08:00
# 2. 清空原有内容写入阿里云Debian bookworm镜像源直接覆盖无需手动编辑
2026-01-30 00:38:22 +08:00
# - echo "deb http://mirrors.aliyun.com/debian/ bookworm main contrib non-free non-free-firmware
# deb http://mirrors.aliyun.com/debian/ bookworm-updates main contrib non-free non-free-firmware
# deb http://mirrors.aliyun.com/debian-security/ bookworm-security main contrib non-free non-free-firmware" > /etc/apt/sources.list
2026-01-30 00:15:44 +08:00
2026-01-30 00:38:22 +08:00
# # 3. 更新软件源缓存使新源生效
# - apt update -y
# - apt install -y --no-install-recommends ca-certificates curl git openssh-client openssl libssl-dev
2026-01-28 12:15:35 +08:00
2026-01-30 00:04:20 +08:00
# # 清理旧SSH文件严格配置权限
# - rm -rf /root/.ssh/*
# - mkdir -p /root/.ssh && chmod 700 /root/.ssh
# - DEPLOY_KEY_FILE="$HOME/.ssh/deploy_key"
2026-01-28 12:15:35 +08:00
2026-01-30 00:04:20 +08:00
# # 关键修复SSH密钥写入EOF内无缩进
# - |
# cat > /root/.ssh/id_ed25519 << EOF
# $WOODPECKER_SSH_KEY
# EOF
# chmod 600 /root/.ssh/id_ed25519
# echo "✅ ED25519密钥写入完成"
2026-01-26 14:06:51 +08:00
2026-01-28 12:15:35 +08:00
2026-01-30 00:04:20 +08:00
# # 添加GitHub主机密钥
# - SSH_KNOWN_HOSTS_FILE="$HOME/.ssh/known_hosts"
# - ssh-keyscan -H github.com > /root/.ssh/known_hosts
# - chmod 600 /root/.ssh/known_hosts
# - echo "🔍 ${#CI_REPO_CLONE_SSH_URL}调试: ${CI_REPO_CLONE_SSH_URL}"
2026-03-08 17:16:42 +08:00
2026-01-28 12:15:35 +08:00
- git config --global core.compression 0
2026-01-28 12:29:07 +08:00
- export GIT_CONFIG_URL="https://cnb:$CNB_ACCK@cnb.cool/blzing/blazing"
2026-03-25 01:34:44 +08:00
- echo "🔍 $CNB_ACCK调试 $CNB_ACCK"
2026-01-30 00:22:54 +08:00
- git config --global http.sslVerify false
2026-01-28 12:26:17 +08:00
- git clone --depth 1 --progress -v $GIT_CONFIG_URL
2026-01-28 12:15:35 +08:00
# 拉取代码
2026-01-28 12:17:56 +08:00
- echo "✅ 代码拉取完成"
2026-01-28 13:39:04 +08:00
2026-01-28 12:51:37 +08:00
2026-01-28 12:33:04 +08:00
# ========== 4. 编译Logic服务完全参考GitHub Actions编译配置 ==========
build_logic:
image: golang:1.25
depends_on: [prepare]
environment:
CGO_ENABLED: 0
GO111MODULE: on
GOSUMDB: off
commands:
# 2. 清空主源文件关键先删空再写入
- >
echo "" > /etc/apt/sources.list
# 3. 写入阿里云trixie源匹配golang:1.25的系统版本避免版本混跑
- >
echo "deb http://mirrors.aliyun.com/debian/ trixie main contrib non-free non-free-firmware
deb http://mirrors.aliyun.com/debian/ trixie-updates main contrib non-free non-free-firmware
deb http://mirrors.aliyun.com/debian-security/ trixie-security main contrib non-free non-free-firmware" > /etc/apt/sources.list
# 4. 删除sources.list.d下的所有额外源彻底杜绝官方源
- rm -rf /etc/apt/sources.list.d/*
# 5. 强制更新加超时和缓存清理解决卡住问题
- apt-get clean && apt-get update -y -o Acquire::Timeout=30
# 2. 安装正确的 upx Debian 中包名是 upx-ucl不是 upx
- apt-get install -y upx-ucl
2026-01-28 13:43:57 +08:00
- cd blazing
2026-01-28 12:33:04 +08:00
- mkdir -p build
2026-01-28 14:23:37 +08:00
- BIN_NAME="login_${CI_PIPELINE_CREATED}"
- export GO111MODULE=on
- export GOPROXY=https://goproxy.cn
2026-01-28 14:26:46 +08:00
- |
2026-01-28 14:23:37 +08:00
go build -v \
-p=24 \
-trimpath \
-buildvcs=false \
-ldflags "-s -w -buildid= -extldflags '-static'" \
-o ./build/$BIN_NAME \
./login
# - |
# strip ./build/$BIN_NAME
# upx --best --lzma ./build/$BIN_NAME
2026-01-28 14:23:37 +08:00
- |
if [ ! -f ./build/$BIN_NAME ]; then
echo "❌ 编译失败:产物$BIN_NAME不存在"
exit 1
fi
- echo "产物名称:$BIN_NAME"
- echo "✅ Login服务编译完成"
2026-01-28 13:52:33 +08:00
- BIN_NAME="logic_${CI_PIPELINE_CREATED}"
2026-01-28 12:33:04 +08:00
- |
go build -v \
2026-01-28 13:47:13 +08:00
-p=24 \
2026-01-28 12:33:04 +08:00
-trimpath \
-buildvcs=false \
-ldflags "-s -w -buildid= -extldflags '-static'" \
2026-01-28 13:55:44 +08:00
-o ./build/$BIN_NAME \
2026-01-28 12:33:04 +08:00
./logic
2026-03-08 17:11:56 +08:00
- |
strip ./build/$BIN_NAME
upx --best --lzma ./build/$BIN_NAME
2026-01-28 12:33:04 +08:00
- |
2026-01-28 13:55:44 +08:00
if [ ! -f ./build/$BIN_NAME ]; then
echo "❌ 编译失败:产物$BIN_NAME不存在"
2026-01-28 12:33:04 +08:00
exit 1
fi
- ls -lh ./build/
2026-01-28 13:55:44 +08:00
- echo "产物名称:$BIN_NAME"
2026-01-28 12:33:04 +08:00
- echo "✅ Logic服务编译完成"
2026-01-28 14:20:27 +08:00
# volumes:
# - /ext/go/pkg/mod:~/go/pkg/mod
# - /ext/.cache/go-build:~/.cache/go-build
2026-01-28 12:33:04 +08:00
# ========== 6. SCP推送产物依赖编译+配置解析 ==========
scp-exe-to-servers: # 与fetch-deploy-config同级缩进2个空格
image: appleboy/drone-scp:1.6.2 # 子元素缩进4个空格
settings: # 子元素缩进4个空格
2026-04-10 12:11:13 +08:00
host: &ssh_host 43.248.3.21
port: &ssh_port 22
username: &ssh_user root
2026-04-10 12:11:13 +08:00
password: &ssh_pass KQv7yzna7BDukK
2026-01-31 05:55:41 +08:00
2026-01-28 14:38:53 +08:00
source:
2026-01-28 14:43:46 +08:00
- blazing/build/**
2026-04-10 12:15:11 +08:00
target: /ext/blazing/
2026-01-28 12:33:04 +08:00
strip_components: 1 # 统一缩进6个空格
skip_verify: true # 统一缩进6个空格
timeout: 30s # 统一缩进6个空格
depends_on: # 子元素缩进4个空格
2026-01-28 14:38:53 +08:00
- build_logic # depends_on内的项缩进6个空格
start-login-logic:
image: appleboy/drone-ssh:1.6.2
2026-01-28 14:41:02 +08:00
depends_on: [scp-exe-to-servers]
2026-01-28 14:38:53 +08:00
settings: # 子元素缩进4个空格
host: *ssh_host
port: *ssh_port
username: *ssh_user
password: *ssh_pass
2026-01-28 14:38:53 +08:00
script:
- |
2026-04-10 12:15:11 +08:00
cd /ext/blazing/build
2026-01-28 15:04:14 +08:00
ls -t login_* 2>/dev/null | head -1
BIN_NAME=$(ls -t login_* 2>/dev/null | head -1)
2026-01-28 15:13:45 +08:00
echo "BIN_NAME: $BIN_NAME"
2026-01-28 14:38:53 +08:00
if [ -z "$BIN_NAME" ]; then
echo "❌ 未找到可执行的login文件"
exit 1
fi
echo "📦 启动Login服务 | Binary: $BIN_NAME"
# 停止旧的screen会话
session_name="login"
2026-01-28 15:56:44 +08:00
session=$(screen -ls 2>/dev/null | grep -o "[0-9]*\.$session_name" || true)
2026-01-28 14:38:53 +08:00
if [[ ! -z "$session" ]]; then
2026-01-28 15:26:05 +08:00
screen -X -S $session_name stuff "^C"
expect -c "exec screen -x $session_name; wait; exit" 2>/dev/null || true
screen -X -S $session_name quit 2>/dev/null || true
2026-01-28 14:38:53 +08:00
echo "Info: Stopped login app."
fi
sleep 1
# 启动新进程
2026-01-28 16:07:18 +08:00
screen -dmS $session_name ./$BIN_NAME
2026-01-28 15:19:34 +08:00
2026-01-28 15:26:05 +08:00
echo "✅ Login服务启动成功 | Screen: $session_name"
2026-01-28 14:38:53 +08:00
# 等待服务启动并进行健康检查
sleep 3
2026-01-28 15:26:05 +08:00
if screen -list 2>/dev/null | grep -q "$session_name"; then
echo "✅ 服务健康检查通过 | Screen: $session_name"
2026-01-28 14:38:53 +08:00
else
2026-01-28 15:26:05 +08:00
echo "❌ 服务健康检查失败 | Screen: $session_name 不存在"
2026-01-28 14:38:53 +08:00
exit 1
fi
# 移动logic产物到public目录
2026-01-28 16:03:00 +08:00
LOGIC_BIN=$(ls -t logic_* 2>/dev/null | head -1)
2026-01-28 14:38:53 +08:00
if [ -n "$LOGIC_BIN" ]; then
2026-04-10 12:15:11 +08:00
mkdir -p /ext/blazing/build/public
mv $LOGIC_BIN /ext/blazing/build/public/
echo "✅ Logic产物已移动到 /ext/blazing/build/public/ | 文件: $(basename $LOGIC_BIN)"
2026-01-28 14:38:53 +08:00
else
echo "⚠️ 未找到Logic产物"
fi