|
1 | | -# This workflow will build a golang project |
2 | | -# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go |
| 1 | +# GitHub Actions Workflow for oho CLI |
| 2 | +# Supports: Auto build/test, Release on tag, Multi-platform, Multi-arch |
3 | 3 |
|
4 | | -name: Go |
| 4 | +name: Go CI/CD |
5 | 5 |
|
6 | 6 | on: |
7 | 7 | push: |
8 | | - branches: [ "master" ] |
| 8 | + branches: [master] |
9 | 9 | pull_request: |
10 | | - branches: [ "master" ] |
| 10 | + branches: [master] |
| 11 | + workflow_dispatch: |
| 12 | + tag: |
| 13 | + patterns: |
| 14 | + - 'v*' |
| 15 | + types: [created] |
| 16 | + |
| 17 | +env: |
| 18 | + GO_VERSION: '1.21' |
| 19 | + APP_NAME: oho |
11 | 20 |
|
12 | 21 | jobs: |
| 22 | + # =========================================== |
| 23 | + # Job 1: 代码质量检查 |
| 24 | + # =========================================== |
| 25 | + lint: |
| 26 | + name: Lint |
| 27 | + runs-on: ubuntu-latest |
| 28 | + steps: |
| 29 | + - name: Checkout code |
| 30 | + uses: actions/checkout@v4 |
| 31 | + |
| 32 | + - name: Set up Go |
| 33 | + uses: actions/setup-go@v5 |
| 34 | + with: |
| 35 | + go-version: ${{ env.GO_VERSION }} |
13 | 36 |
|
| 37 | + - name: Download dependencies |
| 38 | + run: go mod download |
| 39 | + |
| 40 | + - name: Run golangci-lint |
| 41 | + uses: golangci/golangci-lint-action@v6 |
| 42 | + with: |
| 43 | + version: latest |
| 44 | + args: --timeout=5m |
| 45 | + |
| 46 | + # =========================================== |
| 47 | + # Job 2: 测试 |
| 48 | + # =========================================== |
| 49 | + test: |
| 50 | + name: Test |
| 51 | + runs-on: ubuntu-latest |
| 52 | + steps: |
| 53 | + - name: Checkout code |
| 54 | + uses: actions/checkout@v4 |
| 55 | + |
| 56 | + - name: Set up Go |
| 57 | + uses: actions/setup-go@v5 |
| 58 | + with: |
| 59 | + go-version: ${{ env.GO_VERSION }} |
| 60 | + |
| 61 | + - name: Download dependencies |
| 62 | + run: go mod download |
| 63 | + |
| 64 | + - name: Run tests |
| 65 | + run: | |
| 66 | + go test -v -race -coverprofile=coverage.out ./... |
| 67 | + |
| 68 | + - name: Upload coverage to Codecov |
| 69 | + if: github.event_name == 'pull_request' |
| 70 | + uses: codecov/codecov-action@v4 |
| 71 | + with: |
| 72 | + files: ./coverage.out |
| 73 | + flags: unittests |
| 74 | + name: codecov-umbrella |
| 75 | + |
| 76 | + # =========================================== |
| 77 | + # Job 3: 构建 (PR 和 master 推送时) |
| 78 | + # =========================================== |
14 | 79 | build: |
| 80 | + name: Build |
15 | 81 | runs-on: ubuntu-latest |
| 82 | + needs: [lint, test] |
| 83 | + strategy: |
| 84 | + matrix: |
| 85 | + goos: [linux] |
| 86 | + goarch: [amd64, arm64] |
16 | 87 | steps: |
17 | | - - uses: actions/checkout@v4 |
| 88 | + - name: Checkout code |
| 89 | + uses: actions/checkout@v4 |
| 90 | + |
| 91 | + - name: Set up Go |
| 92 | + uses: actions/setup-go@v5 |
| 93 | + with: |
| 94 | + go-version: ${{ env.GO_VERSION }} |
18 | 95 |
|
19 | | - - name: Set up Go |
20 | | - uses: actions/setup-go@v4 |
21 | | - with: |
22 | | - go-version: '1.20' |
| 96 | + - name: Download dependencies |
| 97 | + run: go mod download |
23 | 98 |
|
24 | | - - name: Build |
25 | | - run: go build -v ./... |
| 99 | + - name: Build |
| 100 | + env: |
| 101 | + GOOS: ${{ matrix.goos }} |
| 102 | + GOARCH: ${{ matrix.goarch }} |
| 103 | + run: | |
| 104 | + go build -ldflags="-s -w" -o bin/${APP_NAME}-${GOOS}-${GOARCH} ./cmd |
26 | 105 |
|
27 | | - - name: Test |
28 | | - run: go test -v ./... |
29 | | - |
30 | | - |
| 106 | + - name: Upload artifact |
| 107 | + uses: actions/upload-artifact@v4 |
| 108 | + with: |
| 109 | + name: ${{ env.APP_NAME }}-${{ matrix.goos }}-${{ matrix.goarch }} |
| 110 | + path: bin/${{ env.APP_NAME }}-${{ matrix.goos }}-${{ matrix.goarch }} |
| 111 | + retention-days: 7 |
| 112 | + |
| 113 | + # =========================================== |
| 114 | + # Job 4: Release 发布 (仅在 tag 推送时) |
| 115 | + # =========================================== |
| 116 | + release: |
| 117 | + name: Release |
| 118 | + runs-on: ubuntu-latest |
| 119 | + needs: [lint, test] |
| 120 | + if: startsWith(github.ref, 'refs/tags/v') |
| 121 | + permissions: |
| 122 | + contents: write |
| 123 | + strategy: |
| 124 | + fail-fast: false |
| 125 | + matrix: |
| 126 | + include: |
| 127 | + # Linux |
| 128 | + - os: ubuntu-latest |
| 129 | + goos: linux |
| 130 | + goarch: amd64 |
| 131 | + ext: '' |
| 132 | + - os: ubuntu-latest |
| 133 | + goos: linux |
| 134 | + goarch: arm64 |
| 135 | + ext: '' |
| 136 | + # Windows |
| 137 | + - os: windows-latest |
| 138 | + goos: windows |
| 139 | + goarch: amd64 |
| 140 | + ext: .exe |
| 141 | + # macOS |
| 142 | + - os: macos-latest |
| 143 | + goos: darwin |
| 144 | + goarch: amd64 |
| 145 | + ext: '' |
| 146 | + - os: macos-latest |
| 147 | + goos: darwin |
| 148 | + goarch: arm64 |
| 149 | + ext: '' |
| 150 | + steps: |
| 151 | + - name: Checkout code |
| 152 | + uses: actions/checkout@v4 |
| 153 | + |
| 154 | + - name: Set up Go |
| 155 | + uses: actions/setup-go@v5 |
| 156 | + with: |
| 157 | + go-version: ${{ env.GO_VERSION }} |
| 158 | + |
| 159 | + - name: Download dependencies |
| 160 | + run: go mod download |
| 161 | + |
| 162 | + - name: Build Release |
| 163 | + env: |
| 164 | + GOOS: ${{ matrix.goos }} |
| 165 | + GOARCH: ${{ matrix.goarch }} |
| 166 | + run: | |
| 167 | + OUTPUT_NAME="${{ env.APP_NAME }}-${{ matrix.goos }}-${{ matrix.goarch }}${{ matrix.ext }}" |
| 168 | + go build -ldflags="-s -w -X main.Version=${GITHUB_REF#refs/tags/}" -o "${OUTPUT_NAME}" ./cmd |
| 169 | + ls -la |
| 170 | +
|
| 171 | + - name: Upload Release Asset |
| 172 | + uses: softprops/action-gh-release@v1 |
| 173 | + with: |
| 174 | + tag_name: ${{ github.ref }} |
| 175 | + name: ${{ env.APP_NAME }} ${{ github.ref }} |
| 176 | + body: | |
| 177 | + ## Release Notes |
| 178 | + |
| 179 | + ### Changes |
| 180 | + - Automated build for ${{ matrix.goos }}/${{ matrix.goarch }} |
| 181 | + |
| 182 | + ### Downloads |
| 183 | + - ${{ env.APP_NAME }}-${{ matrix.goos }}-${{ matrix.goarch }}${{ matrix.ext }} |
| 184 | + files: | |
| 185 | + ${{ env.APP_NAME }}-${{ matrix.goos }}-${{ matrix.goarch }}${{ matrix.ext }} |
| 186 | + env: |
| 187 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 188 | + |
| 189 | + - name: Create checksums |
| 190 | + run: | |
| 191 | + ${{ matrix.goos == 'windows' && 'certutil -hashfile' || 'sha256sum' }} ${{ env.APP_NAME }}-${{ matrix.goos }}-${{ matrix.goarch }}${{ matrix.ext }} | tee checksums.txt |
| 192 | +
|
| 193 | + - name: Upload Checksums |
| 194 | + uses: softprops/action-gh-release@v1 |
| 195 | + with: |
| 196 | + tag_name: ${{ github.ref }} |
| 197 | + body_path: checksums.txt |
| 198 | + env: |
| 199 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 200 | + |
| 201 | + # =========================================== |
| 202 | + # Job 5: 通知 (发布成功后) |
| 203 | + # =========================================== |
| 204 | + notify: |
| 205 | + name: Notify |
| 206 | + runs-on: ubuntu-latest |
| 207 | + needs: [release] |
| 208 | + if: startsWith(github.ref, 'refs/tags/v') |
| 209 | + steps: |
| 210 | + - name: Create GitHub Release summary |
| 211 | + run: | |
| 212 | + echo "## 🎉 Release Published" >> $GITHUB_STEP_SUMMARY |
| 213 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 214 | + echo "**Version:** ${{ github.ref_name }}" >> $GITHUB_STEP_SUMMARY |
| 215 | + echo "**Commit:** ${{ github.sha }}" >> $GITHUB_STEP_SUMMARY |
| 216 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 217 | + echo "### Downloads" >> $GITHUB_STEP_SUMMARY |
| 218 | + echo "- [Release Page](${{ github.server_url }}/${{ github.repository }}/releases/tag/${{ github.ref_name }})" >> $GITHUB_STEP_SUMMARY |
0 commit comments