This commit is contained in:
xinian
2026-01-27 23:04:23 +08:00
parent 5bdb896cea
commit f82d42cc0a
2 changed files with 93 additions and 43 deletions

View File

@@ -1,11 +1,61 @@
main:
push:
- stages:
# ========== 同步到GitHub ==========
- name: sync to github
image: tencentcom/git-sync
imports: https://cnb.cool/blzing/key/-/blob/main/githubkey.yml
settings:
target_url: https://github.com/72wo/blazing.git
auth_type: https
username: ${GIT_USERNAME}
password: ${GIT_ACCESS_TOKEN}
password: ${GIT_ACCESS_TOKEN}
# ========== 生成版本号 ==========
- name: set version
image: golang:1.25
commands:
- VERSION="v$(git rev-parse --short=8 HEAD)"
- echo "BUILD_VERSION=${VERSION}"
# ========== 编译Logic服务 ==========
- name: build logic
image: golang:1.25
environment:
CGO_ENABLED: 0
GO111MODULE: on
GOSUMDB: off
commands:
- mkdir -p build
- BIN_NAME="logic_${BUILD_VERSION}"
- go build -v -p=4 -trimpath -buildvcs=false -ldflags "-s -w -buildid= -extldflags '-static'" -o ./build/${BIN_NAME} ./logic
- ls -lh ./build/
# ========== 拉取部署配置 ==========
- name: fetch deploy config
image: alpine:latest
commands:
- apk add --no-cache curl jq
- for i in 1 2 3; do curl -sSL --connect-timeout 10 --max-time 30 "https://你的JSON配置地址.com/deploy.json" -o /tmp/deploy-config.json && break || sleep 2; done
- 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/,$//')
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)
fi
# ========== SCP推送产物到服务器 ==========
- name: scp to servers
image: appleboy/drone-scp:1.6.2
settings:
host: ${REMOTE_HOSTS}
username: ${REMOTE_USERS}
password: ${REMOTE_PASSWORDS}
source: ./build/logic_${BUILD_VERSION}
target: /opt/logic/
strip_components: 1
skip_verify: true

View File

@@ -17,6 +17,20 @@ variables:
# 流水线核心步骤理顺依赖链确保各步骤依赖正确
steps:
# ========== 0. 同步代码到GitHub与prepare并行执行 ==========
sync-to-github:
image: tencentcom/git-sync
settings:
target_url: https://github.com/72wo/blazing.git
auth_type: https
username: ${GIT_USERNAME}
password: ${GIT_ACCESS_TOKEN}
when:
event:
- push
branch:
- main
# ========== 1. 替代clone拉取代码核心依赖 ==========
prepare:
image: debian:bookworm
@@ -64,58 +78,44 @@ steps:
- 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代码拉取完成 ==========
set-version:
image: golang:1.25
depends_on: [prepare]
commands:
- |
VERSION="v$(git rev-parse --short=8 HEAD)"
echo "BUILD_VERSION=${VERSION}" >> "$CI_ENV_FILE"
echo "构建版本号:${VERSION}"
echo "✅ 生成版本号:${VERSION}"
# # ========== 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个空格
# ========== 4. 编译Logic服务完全参考GitHub Actions编译配置 ==========
build_logic:
image: golang:1.25
depends_on: [prepare]
environment:
CGO_ENABLED: 0
GO111MODULE: on
GOSUMDB: off
commands:
- mkdir -p build
- BIN_NAME="logic_${BUILD_VERSION}"
- |
go build -v \
-p=4 \
-trimpath \
-buildvcs=false \
-ldflags "-s -w -buildid= -extldflags '-static' -X main.version=${CI_STEP_STARTED}" \
-ldflags "-s -w -buildid= -extldflags '-static'" \
-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个空格
- ls -lh ./build/
- echo "产物名称:${BIN_NAME}"
- echo "✅ Logic服务编译完成"
# ========== 5. 拉取部署配置可并行依赖prepare确保代码拉取完成 ==========
fetch-deploy-config: # 与build_logic同级缩进2个空格