|
| 1 | +name: create-release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main # 监听 main 分支的 push 操作(编译和测试/代码检查) |
| 7 | + tags: |
| 8 | + - 'v*' # 监听以 'v' 开头的标签的 push 操作(发布 Release) |
| 9 | + |
| 10 | +jobs: |
| 11 | + lint: |
| 12 | + name: lint |
| 13 | + runs-on: ubuntu-latest |
| 14 | + steps: |
| 15 | + - uses: actions/checkout@v4 |
| 16 | + - uses: actions/setup-go@v5 |
| 17 | + with: |
| 18 | + go-version: stable |
| 19 | + cache: true |
| 20 | + - name: golangci-lint |
| 21 | + uses: golangci/golangci-lint-action@v8 |
| 22 | + with: |
| 23 | + version: latest |
| 24 | + args: --timeout=5m |
| 25 | + |
| 26 | + test: |
| 27 | + runs-on: ubuntu-latest |
| 28 | + strategy: |
| 29 | + matrix: |
| 30 | + go: [ "1.22.x", "1.23.x", "1.24.x", "stable" ] |
| 31 | + steps: |
| 32 | + - uses: actions/checkout@v4 |
| 33 | + |
| 34 | + - uses: actions/setup-go@v5 |
| 35 | + with: |
| 36 | + go-version: ${{ matrix.go }} |
| 37 | + cache: true |
| 38 | + |
| 39 | + - name: Run govulncheck |
| 40 | + uses: golang/govulncheck-action@v1 |
| 41 | + with: |
| 42 | + go-version-input: ${{ matrix.go }} |
| 43 | + go-package: ./... |
| 44 | + continue-on-error: true # 报错时允许工作流继续执行,因为项目依赖的底层包也会有错,很难做到百分百没问题,只打印检测结果就行 |
| 45 | + |
| 46 | + - name: Run test |
| 47 | + run: make test COVERAGE_DIR=/tmp/coverage |
| 48 | + |
| 49 | + - name: Upload test results |
| 50 | + uses: actions/upload-artifact@v4 |
| 51 | + if: always() |
| 52 | + with: |
| 53 | + name: test-results-${{ matrix.go }} |
| 54 | + path: /tmp/coverage/ |
| 55 | + retention-days: 30 |
| 56 | + |
| 57 | + - name: Send goveralls coverage |
| 58 | + uses: shogo82148/actions-goveralls@v1 |
| 59 | + with: |
| 60 | + path-to-profile: /tmp/coverage/combined.txt |
| 61 | + flag-name: Go-${{ matrix.go }} |
| 62 | + parallel: true |
| 63 | + if: ${{ github.event.repository.fork == false }} # 仅在非 fork 时上传覆盖率 |
| 64 | + |
| 65 | + check-coverage: |
| 66 | + name: Check coverage |
| 67 | + needs: [ test ] |
| 68 | + runs-on: ubuntu-latest |
| 69 | + steps: |
| 70 | + - uses: shogo82148/actions-goveralls@v1 |
| 71 | + with: |
| 72 | + parallel-finished: true |
| 73 | + if: ${{ github.event.repository.fork == false }} # 仅在非 fork 时检查覆盖率 |
| 74 | + |
| 75 | + # 代码质量分析 |
| 76 | + code-analysis: |
| 77 | + name: CodeQL Analysis |
| 78 | + runs-on: ubuntu-latest |
| 79 | + permissions: |
| 80 | + actions: read |
| 81 | + contents: read |
| 82 | + security-events: write |
| 83 | + steps: |
| 84 | + - name: Checkout repository |
| 85 | + uses: actions/checkout@v4 |
| 86 | + |
| 87 | + - name: Initialize CodeQL |
| 88 | + uses: github/codeql-action/init@v3 |
| 89 | + with: |
| 90 | + languages: go |
| 91 | + |
| 92 | + - name: Auto Build |
| 93 | + uses: github/codeql-action/autobuild@v3 |
| 94 | + |
| 95 | + - name: Perform CodeQL Analysis |
| 96 | + uses: github/codeql-action/analyze@v3 |
| 97 | + |
| 98 | + # 发布 Release |
| 99 | + release: |
| 100 | + name: Release a new version |
| 101 | + needs: [ lint, test, check-coverage, code-analysis ] |
| 102 | + runs-on: ubuntu-latest |
| 103 | + # 仅在推送标签时执行 - && - 仅在非 fork 时执行发布 |
| 104 | + if: ${{ github.event.repository.fork == false && success() && startsWith(github.ref, 'refs/tags/v') }} |
| 105 | + steps: |
| 106 | + # 1. 检出代码 |
| 107 | + - name: Checkout code |
| 108 | + uses: actions/checkout@v4 |
| 109 | + with: |
| 110 | + fetch-depth: 0 # 获取完整历史用于生成更好的 release notes |
| 111 | + |
| 112 | + # 2. 创建 Release 和上传源码包 |
| 113 | + - name: Create Release |
| 114 | + uses: softprops/action-gh-release@v2 |
| 115 | + with: |
| 116 | + generate_release_notes: true |
| 117 | + env: |
| 118 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments