name: Build and Publish Server on: push: branches: - main paths: - "server/**" - ".gitea/workflows/server-deploy.yml" jobs: build: # 标准标签:hk / bt-runner 均带 ubuntu-latest,任一空闲 runner 接单 runs-on: ubuntu-latest steps: # 精简 runner(Alpine)无 node,无法跑 action-checkout;直接用 git 浅克隆 - name: Checkout env: REPO_URL: ${{ gitea.server_url }}/${{ gitea.repository }} REF_NAME: ${{ gitea.ref_name }} run: | git init -q . git remote add origin "$REPO_URL" git fetch -q --depth 20 origin "$REF_NAME" git checkout -q FETCH_HEAD git log --oneline -3 - name: Ensure curl available run: | # 精简 runner(Alpine 等)可能未预装 curl,发布 release 需要它 if ! command -v curl >/dev/null 2>&1; then if command -v apk >/dev/null 2>&1; then apk add --no-cache curl elif command -v apt-get >/dev/null 2>&1; then apt-get update -qq && apt-get install -y -qq curl elif command -v dnf >/dev/null 2>&1; then dnf install -y -q curl else echo "无法安装 curl"; exit 1; fi fi curl --version | head -1 - name: Prepare Go toolchain run: | # 优先复用 runner 宿主机缓存的工具链(工作目录持久化) TOOLDIR="$RUNNER_WORKSPACE/.lunar-toolchain" mkdir -p "$TOOLDIR" if [ -x "$TOOLDIR/go/bin/go" ]; then echo "复用缓存 Go 工具链" else # 官方源与阿里云镜像双通道,先到先用 wget -q --tries=2 --timeout=60 -O "$TOOLDIR/go.tgz" \ "https://go.dev/dl/go1.22.10.linux-amd64.tar.gz" \ || wget -q --tries=2 --timeout=60 -O "$TOOLDIR/go.tgz" \ "https://mirrors.aliyun.com/golang/go1.22.10.linux-amd64.tar.gz" rm -rf "$TOOLDIR/go" tar -C "$TOOLDIR" -xzf "$TOOLDIR/go.tgz" rm -f "$TOOLDIR/go.tgz" fi "$TOOLDIR/go/bin/go" version echo "LUNAR_GO=$TOOLDIR/go/bin/go" >> "$GITEA_ENV" - name: Install dependencies working-directory: server run: | export HOME="$RUNNER_WORKSPACE" "$LUNAR_GO" mod download - name: Run tests working-directory: server run: | export HOME="$RUNNER_WORKSPACE" "$LUNAR_GO" test ./... - name: Build application working-directory: server env: GITEA_SHA: ${{ gitea.sha }} GITEA_RUN_NUMBER: ${{ gitea.run_number }} run: | export HOME="$RUNNER_WORKSPACE" mkdir -p bin # 生成版本号(基线 1.1 + 构建编号,与小程序版本段对齐) VERSION="1.1.${GITEA_RUN_NUMBER}" BUILD_TIME="$(date -u +%Y-%m-%dT%H:%M:%SZ)" COMMIT_SHORT="${GITEA_SHA:0:7}" echo "Building server version: $VERSION" # ldflags 注入版本信息;目标平台为 doc79(linux/amd64) CGO_ENABLED=0 GOOS=linux GOARCH=amd64 "$LUNAR_GO" build -ldflags "\ -X main.Version=$VERSION \ -X main.BuildTime=$BUILD_TIME \ -X main.CommitSha=$COMMIT_SHORT" \ -o bin/server cmd/main.go # 版本元数据随包分发,便于服务器侧追溯 cat > bin/version.env <