|
| 1 | +name: Build and Upload Release Assets |
| 2 | + |
| 3 | +on: |
| 4 | + release: |
| 5 | + types: [created] |
| 6 | + |
| 7 | +env: |
| 8 | + BINARY_NAME: mc |
| 9 | + |
| 10 | +jobs: |
| 11 | + build-and-upload: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + permissions: |
| 14 | + contents: write |
| 15 | + strategy: |
| 16 | + matrix: |
| 17 | + include: |
| 18 | + - goos: linux |
| 19 | + goarch: amd64 |
| 20 | + suffix: linux-amd64 |
| 21 | + - goos: windows |
| 22 | + goarch: amd64 |
| 23 | + suffix: windows-amd64.exe |
| 24 | + - goos: darwin |
| 25 | + goarch: amd64 |
| 26 | + suffix: darwin-amd64 |
| 27 | + - goos: darwin |
| 28 | + goarch: arm64 |
| 29 | + suffix: darwin-arm64 |
| 30 | + |
| 31 | + steps: |
| 32 | + - uses: actions/checkout@v4 |
| 33 | + |
| 34 | + - name: Set up Go |
| 35 | + uses: actions/setup-go@v5 |
| 36 | + with: |
| 37 | + go-version: '1.24.5' |
| 38 | + |
| 39 | + - name: Cache Go modules |
| 40 | + uses: actions/cache@v4 |
| 41 | + with: |
| 42 | + path: ~/go/pkg/mod |
| 43 | + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} |
| 44 | + restore-keys: | |
| 45 | + ${{ runner.os }}-go- |
| 46 | +
|
| 47 | + - name: Get version from tag |
| 48 | + id: get_version |
| 49 | + run: echo "version=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT |
| 50 | + |
| 51 | + - name: Download dependencies |
| 52 | + run: go mod download |
| 53 | + |
| 54 | + - name: Build binary |
| 55 | + env: |
| 56 | + GOOS: ${{ matrix.goos }} |
| 57 | + GOARCH: ${{ matrix.goarch }} |
| 58 | + CGO_ENABLED: 0 |
| 59 | + run: | |
| 60 | + go build -a -installsuffix cgo -ldflags="-w -s -X main.Version=${{ steps.get_version.outputs.version }}" -o ${{ env.BINARY_NAME }}-${{ matrix.suffix }} |
| 61 | +
|
| 62 | + - name: Upload artifact |
| 63 | + uses: actions/upload-artifact@v4 |
| 64 | + with: |
| 65 | + name: ${{ env.BINARY_NAME }}-${{ matrix.suffix }} |
| 66 | + path: ${{ env.BINARY_NAME }}-${{ matrix.suffix }} |
| 67 | + |
| 68 | + upload-assets: |
| 69 | + needs: build-and-upload |
| 70 | + runs-on: ubuntu-latest |
| 71 | + permissions: |
| 72 | + contents: write |
| 73 | + steps: |
| 74 | + - uses: actions/checkout@v4 |
| 75 | + |
| 76 | + - name: Get version from tag |
| 77 | + id: get_version |
| 78 | + run: echo "version=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT |
| 79 | + |
| 80 | + - name: Download all artifacts |
| 81 | + uses: actions/download-artifact@v4 |
| 82 | + with: |
| 83 | + path: ./artifacts |
| 84 | + |
| 85 | + - name: Move artifacts to release directory |
| 86 | + run: | |
| 87 | + mkdir -p release |
| 88 | + find ./artifacts -type f -name "${{ env.BINARY_NAME }}-*" -exec cp {} ./release/ \; |
| 89 | + ls -la ./release/ |
| 90 | +
|
| 91 | + - name: Generate checksums |
| 92 | + run: | |
| 93 | + cd release |
| 94 | + sha256sum * > checksums.txt |
| 95 | + ls -la |
| 96 | +
|
| 97 | + - name: Upload assets to existing release |
| 98 | + env: |
| 99 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 100 | + run: | |
| 101 | + # Upload all binaries and checksums to the existing release |
| 102 | + gh release upload ${{ github.event.release.tag_name }} ./release/* --clobber |
| 103 | +
|
| 104 | + - name: Update release notes with checksums |
| 105 | + env: |
| 106 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 107 | + run: | |
| 108 | + # Get current release notes |
| 109 | + CURRENT_NOTES=$(gh release view ${{ github.event.release.tag_name }} --json body --jq '.body') |
| 110 | + |
| 111 | + # Append installation instructions and checksums if not already present |
| 112 | + if ! echo "$CURRENT_NOTES" | grep -q "### Installation"; then |
| 113 | + cat > additional_notes.md << 'EOF' |
| 114 | + |
| 115 | + ### Installation |
| 116 | + |
| 117 | + **Linux/macOS (one-liner):** |
| 118 | + ```bash |
| 119 | + curl -sfL https://github.com/${{ github.repository }}/releases/download/${{ github.event.release.tag_name }}/mc-linux-amd64 -o mc && chmod +x mc && sudo mv mc /usr/local/bin/ |
| 120 | + ``` |
| 121 | + |
| 122 | + **Windows (PowerShell):** |
| 123 | + ```powershell |
| 124 | + Invoke-WebRequest -Uri "https://github.com/${{ github.repository }}/releases/download/${{ github.event.release.tag_name }}/mc-windows-amd64.exe" -OutFile "mc.exe" |
| 125 | + ``` |
| 126 | + |
| 127 | + ### Manual Downloads |
| 128 | + - **Linux (x64):** [mc-linux-amd64](https://github.com/${{ github.repository }}/releases/download/${{ github.event.release.tag_name }}/mc-linux-amd64) |
| 129 | + - **Windows (x64):** [mc-windows-amd64.exe](https://github.com/${{ github.repository }}/releases/download/${{ github.event.release.tag_name }}/mc-windows-amd64.exe) |
| 130 | + - **macOS (Intel):** [mc-darwin-amd64](https://github.com/${{ github.repository }}/releases/download/${{ github.event.release.tag_name }}/mc-darwin-amd64) |
| 131 | + - **macOS (Apple Silicon):** [mc-darwin-arm64](https://github.com/${{ github.repository }}/releases/download/${{ github.event.release.tag_name }}/mc-darwin-arm64) |
| 132 | + |
| 133 | + ## Checksums (SHA256) |
| 134 | + ``` |
| 135 | + $(cat release/checksums.txt) |
| 136 | + ``` |
| 137 | + EOF |
| 138 | + |
| 139 | + # Combine current notes with additional notes |
| 140 | + echo "$CURRENT_NOTES" > combined_notes.md |
| 141 | + cat additional_notes.md >> combined_notes.md |
| 142 | + |
| 143 | + # Update the release with the combined notes |
| 144 | + gh release edit ${{ github.event.release.tag_name }} --notes-file combined_notes.md |
| 145 | + fi |
0 commit comments