Files
bl/.github/workflows/logic_CI.yml
2026-01-22 10:02:58 +08:00

136 lines
5.3 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.

name: Go Build & Release
on:
push:
branches: [main]
workflow_dispatch:
inputs:
servicePort:
description: '服务启动端口'
required: true
default: 8080
type: number
# 统一最小化权限仅保留必要的actions/read删除contents/write
permissions:
actions: read
env:
# Go编译全局优化变量
CGO_ENABLED: 0
GO111MODULE: on
GOSUMDB: off
# 七牛云远程目录统一管理可按需修改
QINIU_REMOTE_DIR: releases/
jobs:
build-and-upload-qiniu:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 1 # 仅拉取最新提交加速克隆
# 生成版本号短Commit Hash合并原prepare-version任务
- name: 生成构建版本号
id: set-version
run: |
VERSION="v$(git rev-parse --short=8 HEAD)"
echo "build_version=${VERSION}" >> $GITHUB_OUTPUT
echo "构建版本号:${VERSION}" >> $GITHUB_STEP_SUMMARY
shell: bash
# 缓存Go依赖v4+双缓存编译缓存+mod缓存极致加速
- name: 缓存Go依赖
uses: actions/cache@v4
with:
path: |
~/go/pkg/mod
~/.cache/go-build
key: ${{ runner.os }}-go-${{ hashFiles('**/go.mod', '**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
fail-on-cache-miss: false
# 配置Go环境v5+自动缓存双重保障
- name: 配置Go 1.25环境
uses: actions/setup-go@v5
with:
go-version: '1.25'
cache: true
# 预下载依赖避免编译时边下载边构建
- name: 预下载Go依赖
run: go mod download -x
shell: bash
# 编译Go服务极致优化参数保留所有原优化
- name: 编译Logic服务
run: |
mkdir -p build
BIN_NAME="logic_${{ steps.set-version.outputs.build_version }}"
# 编译参数并行+瘦身+静态编译
go build -v \
-p=4 \
-trimpath \
-buildvcs=false \
-ldflags "-s -w -buildid= -extldflags '-static'" \
-o ./build/${BIN_NAME} \
./logic
# 校验编译产物是否存在
if [ ! -f ./build/${BIN_NAME} ]; then
echo "编译失败:产物${BIN_NAME}不存在"
exit 1
fi
# 打印产物信息
ls -lh ./build/
echo "产物名称:${BIN_NAME}" >> $GITHUB_STEP_SUMMARY
shell: bash
# 上传到七牛云核心步骤增加校验+日志+CDN地址打印
- name: 上传产物到七牛云
id: qiniu-upload
uses: cumt-robin/upload-to-qiniu-action@v1
with:
access_key: ${{ secrets.QINIU_AK }}
secret_key: ${{ secrets.QINIU_SK }}
bucket: ${{ secrets.QINIU_BUCKET_NAME }}
region: z2
local_dir: build
remote_dir: ${{ env.QINIU_REMOTE_DIR }}
overwrite: true # 覆盖同名文件避免版本冲突
# 上传成功后打印CDN访问地址+构建信息写入步骤摘要
- name: 打印构建&分发信息
run: |
BIN_NAME="logic_${{ steps.set-version.outputs.build_version }}"
CDN_URL="https://${{ secrets.QINIU_CDN_DOMAIN }}/${{ env.QINIU_REMOTE_DIR }}${BIN_NAME}"
# 打印到控制台
echo "======================================"
echo "✅ 构建&七牛云上传完成!"
echo "版本号:${{ steps.set-version.outputs.build_version }}"
echo "触发方式:${{ github.event_name == 'workflow_dispatch' && '手动触发' || '代码推送' }}"
echo "服务端口:${{ github.event.inputs.servicePort || 8080 }}"
echo "产物名称:${BIN_NAME}"
echo "七牛云CDN地址${CDN_URL}"
echo "启动命令:./${BIN_NAME} -port=${{ github.event.inputs.servicePort || 8080 }}"
echo "对应Commit${{ github.sha }}"
echo "======================================"
# 写入GitHub步骤摘要页面顶部可见更直观
echo "## ✅ 构建&分发完成" >> $GITHUB_STEP_SUMMARY
echo "- 版本号:${{ steps.set-version.outputs.build_version }}" >> $GITHUB_STEP_SUMMARY
echo "- 触发方式:${{ github.event_name == 'workflow_dispatch' && '手动触发' || '代码推送' }}" >> $GITHUB_STEP_SUMMARY
echo "- 服务端口:${{ github.event.inputs.servicePort || 8080 }}" >> $GITHUB_STEP_SUMMARY
echo "- 七牛云CDN地址[${CDN_URL}](${CDN_URL})" >> $GITHUB_STEP_SUMMARY
echo "- 启动命令:`./${BIN_NAME} -port=${{ github.event.inputs.servicePort || 8080 }}`" >> $GITHUB_STEP_SUMMARY
echo "- 对应Commit[${{ github.sha }}](https://github.com/${{ github.repository }}/commit/${{ github.sha }})" >> $GITHUB_STEP_SUMMARY
shell: bash
# 可选保留构建产物为Artifact按需开启保留1天方便临时下载
# - name: 上传产物为Artifact
# uses: actions/upload-artifact@v4
# with:
# name: logic_${{ steps.set-version.outputs.build_version }}
# path: ./build/
# if-no-files-found: error
# retention-days: 1