Files
bl/.woodpecker/my-first-workflow.yaml
2026-01-28 10:58:58 +08:00

164 lines
6.2 KiB
Go
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.

# 触发条件仅pushmanual手动触发分支限定main
when:
event:
- push
- manual
branch: main
skip_clone: true # 关闭默认clone手动拉取代码
# 流水线核心步骤整合新的编译+部署流程
steps:
# ========== 1. 替代clone拉取代码对应原sync to github ==========
- name: prepare
image: debian:bookworm
commands:
# 拉取代码替换为你的仓库地址变量
- export GIT_CONFIG_URL="https://cnb:${CNB_ACCK}@cnb.cool/blzing/blazing"
# 系统初始化
- apt update -y
- apt install -y --no-install-recommends ca-certificates curl git openssh-client openssl libssl-dev
# 拉取代码到项目目录
- git clone --depth 1 --progress -v ${GIT_CONFIG_URL} blazing-project
- cd blazing-project
- echo "✅ 代码拉取完成,当前目录:$(pwd)"
# ========== 2. 生成版本号前置步骤 ==========
- name: set version
image: golang:1.23
depends_on: [prepare] # 替换原sync to github为prepare
commands:
- cd blazing-project # 进入项目目录
- VERSION="v$(git rev-parse --short=8 HEAD 2>/dev/null || echo "unknown")"
- mkdir -p .build-info
- echo "BUILD_VERSION=${VERSION}" >> .build-info/.env
- echo "构建版本号:${VERSION}"
# ========== 3. 缓存Go依赖 ==========
- name: cache go modules
image: meltwater/drone-cache:latest
depends_on: [prepare] # 替换原sync to github为prepare
settings:
restore: true
mount:
- /go/pkg/mod
- /root/.cache/go-build
cache_key: '{{ .Repo.Name }}-{{ .Commit.Branch }}-{{ checksum "blazing-project/go.mod" }}-{{ checksum "blazing-project/login/go.mod" }}' # 补充项目目录
# ========== 4. 编译Logic和Login服务 ==========
- name: build
image: golang:1.25
environment:
CGO_ENABLED: 0
GO111MODULE: on
GOSUMDB: off
depends_on:
- cache go modules
- set version
commands:
- cd blazing-project # 进入项目目录
- . .build-info/.env # 加载版本号
- mkdir -p build
- |
# 编译Logic服务
BIN_NAME="logic_${BUILD_VERSION}"
go mod download -x
go build -v -trimpath -buildvcs=false -ldflags "-s -w -buildid= -extldflags '-static'" -o ./build/${BIN_NAME} ./logic
chmod +x ./build/${BIN_NAME}
ls -lh ./build/${BIN_NAME}
# 编译Login服务
cd login
BIN_NAME="login_${BUILD_VERSION}"
go mod download -x
go build -v -trimpath -buildvcs=false -ldflags "-s -w -buildid= -extldflags '-static'" -o ./build/${BIN_NAME} .
chmod +x ./build/${BIN_NAME}
ls -lh ./build/${BIN_NAME}
mv ./build/${BIN_NAME} ../build/
cd ..
ls -lh ./build/
# 写入BIN_NAME到环境文件供后续步骤使用
- echo "LOGIC_BIN=logic_${BUILD_VERSION}" >> "${CI_ENV_FILE}"
- echo "LOGIN_BIN=login_${BUILD_VERSION}" >> "${CI_ENV_FILE}"
# ========== 5. 重建缓存 ==========
- name: rebuild cache
image: meltwater/drone-cache:latest
depends_on: [build]
settings:
rebuild: true
mount:
- /go/pkg/mod
- /root/.cache/go-build
cache_key: '{{ .Repo.Name }}-{{ .Commit.Branch }}-{{ checksum "blazing-project/go.mod" }}-{{ checksum "blazing-project/login/go.mod" }}' # 补充项目目录
# ========== 6. SCP推送Login和Logic到指定服务器 ==========
- name: deploy to login server
image: appleboy/drone-scp:1.6.2
imports: https://cnb.cool/blzing/key/-/blob/main/githubkey.yml
depends_on: [rebuild cache]
settings:
# 核心修复settings块用Go模板语法 {{}} 引用变量
host: {{ LOGIN_SERVER_HOST }}
username: {{ LOGIN_SERVER_USER }}
password: {{ LOGIN_SERVER_PASSWORD }}
port: {{ LOGIN_SERVER_PORT }}
source:
- ./blazing-project/build/login_*
- ./blazing-project/build/logic_*
target: /opt/login/
strip_components: 2 # 调整层级blazing-project/build/xxx 取xxx
skip_verify: true
timeout: 30s
# ========== 7. SSH启动Login服务并上传Logic到Public ==========
- name: start login and move logic
image: appleboy/drone-ssh:1.6.2
imports: https://cnb.cool/blzing/key/-/blob/main/githubkey.yml
depends_on: [deploy to login server]
settings:
# 核心修复settings块用Go模板语法 {{}} 引用变量
host: {{ LOGIN_SERVER_HOST }}
username: {{ LOGIN_SERVER_USER }}
password: {{ LOGIN_SERVER_PASSWORD }}
port: {{ LOGIN_SERVER_PORT }}
script:
- |
cd /opt/login
BIN_NAME=$(ls -t login_v* 2>/dev/null | head -1)
echo "BIN_NAME: $BIN_NAME"
if [ -z "$BIN_NAME" ]; then
echo "❌ 未找到可执行的login文件"
exit 1
fi
echo "📦 启动Login服务 | Binary: $BIN_NAME"
# 停止旧的screen会话
session_name="login"
session=$(screen -ls 2>/dev/null | grep -o "[0-9]*\.${session_name}" || true)
if [[ ! -z "$session" ]]; then
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
echo "Info: Stopped login app."
fi
sleep 1
# 启动新进程
screen -dmS ${session_name} ./${BIN_NAME}
echo "✅ Login服务启动成功 | Screen: ${session_name}"
# 等待服务启动并进行健康检查
sleep 3
if screen -list 2>/dev/null | grep -q "${session_name}"; then
echo "✅ 服务健康检查通过 | Screen: ${session_name}"
else
echo "❌ 服务健康检查失败 | Screen: ${session_name} 不存在"
exit 1
fi
# 移动logic产物到public目录
LOGIC_BIN=$(ls -t logic_v* 2>/dev/null | head -1)
if [ -n "$LOGIC_BIN" ]; then
mkdir -p /opt/login/public
mv $LOGIC_BIN /opt/login/public/
echo "✅ Logic产物已移动到 /opt/login/public/ | 文件: $(basename $LOGIC_BIN)"
else
echo "⚠ 未找到Logic产物"
fi