name: 发布构建 on: push: tags: - 'v*' # 只响应以 v 开头的标签,如 v1.0.0 workflow_dispatch: # 支持手动触发 inputs: tag: description: '发布标签' required: true default: 'v1.0.0' permissions: contents: write issues: write pull-requests: write jobs: goreleaser: name: 构建和发布 runs-on: ubuntu-latest timeout-minutes: 60 steps: - name: 📥 检出代码 uses: actions/checkout@v4 with: fetch-depth: 0 # 获取完整的 git 历史,用于生成变更日志 token: ${{ secrets.GITHUB_TOKEN }} - name: 🔍 获取项目信息 id: project run: | echo "owner=${GITHUB_REPOSITORY_OWNER}" >> $GITHUB_OUTPUT echo "repo=${GITHUB_REPOSITORY#*/}" >> $GITHUB_OUTPUT echo "version=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT - name: 🐹 设置 Go 环境 uses: actions/setup-go@v5 with: go-version: '1.21' # 使用更新的 Go 版本 cache: true # 启用 Go 模块缓存 - name: 📦 下载依赖 run: | go mod download go mod verify - name: 🗜️ 安装 UPX 压缩工具 uses: crazy-max/ghaction-upx@v3 with: install-only: true - name: ℹ️ 显示构建环境信息 run: | echo "Go 版本: $(go version)" echo "UPX 版本: $(upx --version)" echo "Git 标签: ${{ steps.project.outputs.version }}" echo "仓库: ${{ steps.project.outputs.owner }}/${{ steps.project.outputs.repo }}" - name: 🧪 运行测试 run: | go test -v ./... - name: 🔍 验证 GoReleaser 配置 uses: goreleaser/goreleaser-action@v5 with: distribution: goreleaser version: latest args: check -f .github/conf/.goreleaser.yml - name: 🚀 构建和发布 uses: goreleaser/goreleaser-action@v5 with: distribution: goreleaser version: latest args: release --clean -f .github/conf/.goreleaser.yml workdir: . env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_OWNER: ${{ steps.project.outputs.owner }} GITHUB_REPO: ${{ steps.project.outputs.repo }} PROJECT_NAME: ${{ steps.project.outputs.repo }} - name: 📋 上传构建产物 uses: actions/upload-artifact@v4 if: always() with: name: 构建产物-${{ steps.project.outputs.version }} path: | dist/ !dist/*.txt retention-days: 30 - name: 📊 生成构建报告 if: always() run: | echo "## 🎉 构建完成报告" >> $GITHUB_STEP_SUMMARY echo "" >> $GITHUB_STEP_SUMMARY echo "- **版本**: ${{ steps.project.outputs.version }}" >> $GITHUB_STEP_SUMMARY echo "- **仓库**: ${{ steps.project.outputs.owner }}/${{ steps.project.outputs.repo }}" >> $GITHUB_STEP_SUMMARY echo "- **Go 版本**: $(go version | cut -d' ' -f3)" >> $GITHUB_STEP_SUMMARY echo "- **构建时间**: $(date)" >> $GITHUB_STEP_SUMMARY echo "" >> $GITHUB_STEP_SUMMARY if [ -d "dist" ]; then echo "### 📦 生成的文件:" >> $GITHUB_STEP_SUMMARY echo "\`\`\`" >> $GITHUB_STEP_SUMMARY ls -la dist/ >> $GITHUB_STEP_SUMMARY echo "\`\`\`" >> $GITHUB_STEP_SUMMARY fi # 可选:发布到 Docker Hub docker: name: 构建 Docker 镜像 runs-on: ubuntu-latest needs: goreleaser if: success() steps: - name: 📥 检出代码 uses: actions/checkout@v4 - name: 🔍 获取元数据 id: meta uses: docker/metadata-action@v5 with: images: | ${{ github.repository }} tags: | type=ref,event=tag type=raw,value=latest,enable={{is_default_branch}} - name: 🐳 设置 Docker Buildx uses: docker/setup-buildx-action@v3 - name: 🔐 登录 Docker Hub uses: docker/login-action@v3 with: username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} - name: 🚀 构建并推送 Docker 镜像 uses: docker/build-push-action@v5 with: context: . push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} # 可选:通知步骤 notify: name: 发送通知 runs-on: ubuntu-latest needs: [goreleaser] if: always() steps: - name: 📧 发送成功通知 if: needs.goreleaser.result == 'success' run: | echo "✅ 发布成功完成!" echo "版本: ${GITHUB_REF#refs/tags/}" echo "查看发布: https://github.com/${{ github.repository }}/releases" - name: ⚠️ 发送失败通知 if: needs.goreleaser.result == 'failure' run: | echo "❌ 发布失败!" echo "请检查构建日志: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"