name: Go Build & Release on: push: branches: [main] workflow_dispatch: inputs: servicePort: description: '服务启动端口' required: true default: 8080 type: number # 核心修复1:最高权限兜底(解决私有仓库鉴权) permissions: contents: write actions: write env: # Go编译优化 CGO_ENABLED: 0 GO111MODULE: on GOSUMDB: off # 七牛云配置 QINIU_REMOTE_DIR: releases/ # 显式指定你的仓库地址(解决not found核心问题) REPO_URL: https://github.com/72wo/blazing.git jobs: build-and-upload-qiniu: runs-on: ubuntu-latest steps: # 核心修复2:手动克隆仓库(替代checkout,彻底解决鉴权/地址问题) - name: 手动克隆当前仓库(兜底方案) run: | # 用GITHUB_TOKEN拼接鉴权地址,避免匿名访问404 CLONE_URL="https://${{ github.actor }}:${{ github.token }}@github.com/72wo/blazing.git" git clone --depth 1 $CLONE_URL $GITHUB_WORKSPACE cd $GITHUB_WORKSPACE # 确认克隆成功,打印仓库信息 git config --global user.name "github-actions[bot]" git config --global user.email "github-actions[bot]@users.noreply.github.com" git rev-parse HEAD echo "✅ 仓库克隆成功,当前Commit:$(git rev-parse --short=8 HEAD)" shell: bash # 生成版本号(基于本地Git,兜底无异常) - name: 生成构建版本号 id: set-version run: | cd $GITHUB_WORKSPACE VERSION="v$(git rev-parse --short=8 HEAD)" echo "build_version=${VERSION}" >> $GITHUB_OUTPUT echo "构建版本号:${VERSION}" >> $GITHUB_STEP_SUMMARY shell: bash # 缓存Go依赖(双缓存加速) - 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环境 - name: 配置Go 1.25环境 uses: actions/setup-go@v5 with: go-version: '1.25' cache: true # 预下载依赖 - name: 预下载Go依赖 run: | cd $GITHUB_WORKSPACE go mod download -x shell: bash # 编译Logic服务(产物校验) - name: 编译Logic服务 run: | cd $GITHUB_WORKSPACE 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 # 上传七牛云(绝对路径指定,无目录问题) - 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: $GITHUB_WORKSPACE/build remote_dir: ${{ env.QINIU_REMOTE_DIR }} overwrite: true # 打印构建信息 - name: 打印构建完成信息 run: | cd $GITHUB_WORKSPACE 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 "======================================" # 步骤摘要 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.repository }}/commit/${{ github.sha }})" >> $GITHUB_STEP_SUMMARY shell: bash