Build and Release #1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Build and Release | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| version: | |
| description: '版本号 (例如: 0.0.1)' | |
| required: true | |
| default: '0.0.1' | |
| jobs: | |
| build: | |
| runs-on: windows-latest | |
| strategy: | |
| matrix: | |
| include: | |
| - goos: windows | |
| goarch: amd64 | |
| artifact-name: CodeStatistics-windows-amd64.exe | |
| - goos: linux | |
| goarch: amd64 | |
| artifact-name: CodeStatistics-linux-amd64 | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Go 1.23.x | |
| uses: actions/setup-go@v4 | |
| with: | |
| go-version: 1.23.x | |
| - name: Cache Go modules | |
| uses: actions/cache@v3 | |
| with: | |
| path: | | |
| ~/.cache/go-build | |
| ~/go/pkg/mod | |
| key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} | |
| restore-keys: | | |
| ${{ runner.os }}-go- | |
| - name: Build for ${{ matrix.goos }}-${{ matrix.goarch }} | |
| env: | |
| GOOS: ${{ matrix.goos }} | |
| GOARCH: ${{ matrix.goarch }} | |
| CGO_ENABLED: 0 | |
| run: | | |
| go build -ldflags="-s -w -X main.Version=${{ github.event.inputs.version }}" -o ${{ matrix.artifact-name }} main.go | |
| - name: Upload artifact | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: ${{ matrix.artifact-name }} | |
| path: ${{ matrix.artifact-name }} | |
| release: | |
| needs: build | |
| runs-on: windows-latest | |
| if: github.event_name == 'workflow_dispatch' || startsWith(github.ref, 'refs/tags/') | |
| permissions: | |
| contents: write | |
| steps: | |
| - name: Download all artifacts | |
| uses: actions/download-artifact@v4 | |
| with: | |
| path: ./artifacts | |
| - name: List downloaded files | |
| run: | | |
| echo "Current directory: $(pwd)" | |
| echo "Artifacts directory contents:" | |
| Get-ChildItem -Path "./artifacts" -Recurse | Format-Table Name, FullName | |
| - name: Get current date | |
| id: date | |
| run: | | |
| $buildDate = Get-Date -Format "yyyy.MM.dd" | |
| echo "builddate=$buildDate" >> $env:GITHUB_OUTPUT | |
| echo "Build date: $buildDate" | |
| - name: Create release | |
| uses: softprops/action-gh-release@v1 | |
| with: | |
| tag_name: v${{ github.event.inputs.version }} | |
| name: Release v${{ github.event.inputs.version }} | |
| body: | | |
| ## CodeStatistics v${{ github.event.inputs.version }} | |
| ### 构建信息 | |
| - 版本号: ${{ github.event.inputs.version }} | |
| - 构建日期: ${{ steps.date.outputs.builddate }} | |
| - 构建平台: Windows & Linux | |
| - Go版本: 1.23.x | |
| ### 下载 | |
| - Windows (AMD64): `CodeStatistics-windows-amd64.exe` | |
| - Linux (AMD64): `CodeStatistics-linux-amd64` | |
| files: | | |
| ./artifacts/CodeStatistics-windows-amd64.exe/CodeStatistics-windows-amd64.exe | |
| ./artifacts/CodeStatistics-linux-amd64/CodeStatistics-linux-amd64 | |
| draft: false | |
| prerelease: false | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |