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

149 lines
6.8 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
# 流水线核心步骤理顺依赖链确保各步骤依赖正确
steps:
- name: prepare
image: debian:bookworm
environment:
# ========== 1. 替代clone拉取代码核心依赖 ==========
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 https://用户名:密码@仓库地址
- echo "🔍 ${#GIT_CONFIG_URL}调试: ${GIT_CONFIG_URL}"
- git clone --depth 1 --progress -v ${GIT_CONFIG_URL}
# 拉取代码
- echo "✅ 代码拉取完成"
# # ========== 2. 初始化Go环境依赖prepare代码拉取完成 ==========
# prepare-go: # 与prepare同级缩进2个空格
# image: golang:1.25 # 子元素缩进4个空格
# depends_on: [prepare] # 子元素缩进4个空格
# commands: # 子元素缩进4个空格
# - go version # commands内的项缩进6个空格
# - go mod download -x || { echo "❌ 下载Go依赖失败"; exit 1; } # 统一缩进6个空格
# - go mod verify || { echo "❌ 验证Go依赖失败"; exit 1; } # 统一缩进6个空格
# - echo "✅ Go环境初始化完成" # 统一缩进6个空格
# # ========== 3. 生成版本号依赖prepare-goGo环境就绪 ==========
# set-version: # 与prepare-go同级缩进2个空格
# image: golang:1.25 # 子元素缩进4个空格
# depends_on: [prepare-go] # 子元素缩进4个空格
# commands: # 子元素缩进4个空格
# - | # commands内的项缩进6个空格
# if [ -n "${CI_COMMIT_TAG}" ]; then
# VERSION="${CI_COMMIT_TAG}"
# else
# VERSION="v$(git rev-parse --short=8 HEAD 2>/dev/null || echo "unknown")"
# fi
# mkdir -p "$(dirname "$CI_ENV_FILE")"
# echo "BUILD_VERSION=${VERSION}" >> "$CI_ENV_FILE"
# echo "✅ 生成版本号:${VERSION}"
# ========== 4. 编译Go服务核心依赖prepare+prepare-go+set-version ==========
build_logic: # 与set-version同级缩进2个空格
image: golang:1.25 # 子元素缩进4个空格
depends_on: [prepare] # 子元素缩进4个空格
environment: # 子元素缩进4个空格
CGO_ENABLED: 0 # environment内的项缩进6个空格
GO111MODULE: on # 统一缩进6个空格
GOSUMDB: off # 统一缩进6个空格
commands: # 子元素缩进4个空格
- mkdir -p build # commands内的项缩进6个空格
- BIN_NAME="logic_${CI_STEP_STARTED}" # 缩进6个空格
- | # 缩进6个空格
go build -v \
-p=4 \
-trimpath \
-buildvcs=false \
-ldflags "-s -w -buildid= -extldflags '-static' -X main.version=${CI_STEP_STARTED}" \
-o ./build/${BIN_NAME} \
./logic
- | # 缩进6个空格
if [ ! -f ./build/${BIN_NAME} ]; then
echo "❌ 编译失败:产物${BIN_NAME}不存在"
exit 1
fi
- ls -lh ./build/ # 缩进6个空格
- ./build/${BIN_NAME} -v || true # 缩进6个空格
- echo "✅ Go服务编译完成" # 缩进6个空格
# ========== 5. 拉取部署配置可并行依赖prepare确保代码拉取完成 ==========
fetch-deploy-config: # 与build_logic同级缩进2个空格
image: alpine:latest # 子元素缩进4个空格
depends_on: [prepare] # 子元素缩进4个空格
commands: # 子元素缩进4个空格
- apk add --no-cache curl jq # commands内的项缩进6个空格
- echo "🔧 拉取部署配置:${JSON_CONFIG_URL}" # 缩进6个空格
- | # 缩进6个空格
for i in 1 2 3; do
curl -sSL --connect-timeout 10 --max-time 30 "${JSON_CONFIG_URL}" -o /tmp/deploy-config.json && break
echo "⚠️ 第${i}次拉取失败,重试中..."
sleep 2
done
if [ ! -f /tmp/deploy-config.json ]; then
echo "❌ 拉取JSON配置失败"
exit 1
fi
- | # 缩进6个空格
JSON_TYPE=$(jq -r 'type' /tmp/deploy-config.json 2>/dev/null || echo "invalid")
if [ "$JSON_TYPE" = "array" ]; then
REMOTE_HOSTS=$(jq -r '.[].loginaddr' /tmp/deploy-config.json | tr '\n' ',' | sed 's/,$//')
REMOTE_USERS=$(jq -r '.[].user' /tmp/deploy-config.json | tr '\n' ',' | sed 's/,$//')
REMOTE_PASSWORDS=$(jq -r '.[].password' /tmp/deploy-config.json | tr '\n' ',' | sed 's/,$//')
REMOTE_ONLINE_IDS=$(jq -r '.[].online_id' /tmp/deploy-config.json | tr '\n' ',' | sed 's/,$//')
elif [ "$JSON_TYPE" = "object" ]; then
REMOTE_HOSTS=$(jq -r '.loginaddr' /tmp/deploy-config.json)
REMOTE_USERS=$(jq -r '.user' /tmp/deploy-config.json)
REMOTE_PASSWORDS=$(jq -r '.password' /tmp/deploy-config.json)
REMOTE_ONLINE_IDS=$(jq -r '.online_id' /tmp/deploy-config.json)
else
echo "❌ JSON配置格式错误非数组/对象)"
cat /tmp/deploy-config.json
exit 1
fi
- | # 缩进6个空格
if [ -z "$REMOTE_HOSTS" ] || [ -z "$REMOTE_USERS" ]; then
echo "❌ 解析配置失败:服务器/用户名为空"
cat /tmp/deploy-config.json
exit 1
fi
- echo "✅ 配置解析完成 | 服务器:${REMOTE_HOSTS} | OnlineID${REMOTE_ONLINE_IDS}" # 缩进6个空格
- echo "REMOTE_HOSTS=${REMOTE_HOSTS}" >> "$CI_ENV_FILE" # 缩进6个空格
- echo "REMOTE_USERS=${REMOTE_USERS}" >> "$CI_ENV_FILE" # 缩进6个空格
- echo "REMOTE_PASSWORDS=${REMOTE_PASSWORDS}" >> "$CI_ENV_FILE" # 缩进6个空格
- echo "REMOTE_ONLINE_IDS=${REMOTE_ONLINE_IDS}" >> "$CI_ENV_FILE" # 缩进6个空格
# ========== 6. SCP推送产物依赖编译+配置解析 ==========
scp-exe-to-servers: # 与fetch-deploy-config同级缩进2个空格
image: appleboy/drone-scp:1.6.2 # 子元素缩进4个空格
settings: # 子元素缩进4个空格
host: ${REMOTE_HOSTS} # settings内的项缩进6个空格
username: ${REMOTE_USERS} # 统一缩进6个空格
password: ${REMOTE_PASSWORDS} # 统一缩进6个空格
source: ./build/${BIN_NAME} # 统一缩进6个空格
target: ${REMOTE_EXE_DIR}/ # 统一缩进6个空格
strip_components: 1 # 统一缩进6个空格
skip_verify: true # 统一缩进6个空格
timeout: 30s # 统一缩进6个空格
depends_on: # 子元素缩进4个空格
- build_logic # depends_on内的项缩进6个空格
- fetch-deploy-config # 统一缩进6个空格