name: Go Build & Release on: push: branches: [main] workflow_dispatch: inputs: servicePort: description: '服务启动端口' required: true default: 8080 type: number # 最小化权限配置 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: # 仅签出当前仓库(核心修改:极简配置+本仓库内置token鉴权) - uses: actions/checkout@v4 with: fetch-depth: 1 token: ${{ github.token }} # 生成基于Commit的短版本号 - 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依赖(编译缓存+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 1.25环境(自动缓存依赖,双重保障) - name: 配置Go 1.25环境 uses: actions/setup-go@v5 with: go-version: '1.25' cache: true # 预下载所有Go依赖,避免编译时边下载边构建 - name: 预下载Go依赖 run: go mod download -x shell: bash # 编译Logic服务(极致优化参数+产物存在性校验) - name: 编译Logic服务 run: | mkdir -p build BIN_NAME="logic_${{ steps.set-version.outputs.build_version }}" # Go编译极致优化参数:静态编译+瘦身+并行构建 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 # 上传产物到七牛云(覆盖同名文件+统一远程目录) - name: 上传产物到七牛云 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 # 打印构建&分发核心信息(控制台+步骤摘要双展示) - 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 "七牛云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