|
| 1 | +name: Build and Release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ "main" ] |
| 6 | + tags: [ "v*" ] |
| 7 | + pull_request: |
| 8 | + branches: [ "main" ] |
| 9 | + |
| 10 | +jobs: |
| 11 | + test: |
| 12 | + name: Run Tests |
| 13 | + runs-on: ubuntu-latest |
| 14 | + steps: |
| 15 | + - uses: actions/checkout@v4 |
| 16 | + |
| 17 | + - name: Set up Go |
| 18 | + uses: actions/setup-go@v4 |
| 19 | + with: |
| 20 | + go-version: '1.20' |
| 21 | + |
| 22 | + - name: Run tests |
| 23 | + run: go test -v ./internal/... |
| 24 | + |
| 25 | + build: |
| 26 | + name: Build Binaries |
| 27 | + needs: test |
| 28 | + runs-on: ubuntu-latest |
| 29 | + strategy: |
| 30 | + matrix: |
| 31 | + include: |
| 32 | + - os: linux |
| 33 | + arch: amd64 |
| 34 | + suffix: "" |
| 35 | + - os: linux |
| 36 | + arch: arm64 |
| 37 | + suffix: "" |
| 38 | + - os: windows |
| 39 | + arch: amd64 |
| 40 | + suffix: ".exe" |
| 41 | + - os: freebsd |
| 42 | + arch: amd64 |
| 43 | + suffix: "" |
| 44 | + |
| 45 | + steps: |
| 46 | + - uses: actions/checkout@v4 |
| 47 | + |
| 48 | + - name: Set up Go |
| 49 | + uses: actions/setup-go@v4 |
| 50 | + with: |
| 51 | + go-version: '1.20' |
| 52 | + |
| 53 | + - name: Build for ${{ matrix.os }} / ${{ matrix.arch }} |
| 54 | + env: |
| 55 | + GOOS: ${{ matrix.os }} |
| 56 | + GOARCH: ${{ matrix.arch }} |
| 57 | + run: | |
| 58 | + VERSION=$(echo $GITHUB_REF | cut -d / -f 3) |
| 59 | + if [[ "$VERSION" == "" ]]; then |
| 60 | + VERSION="dev" |
| 61 | + fi |
| 62 | + go build -v -ldflags "-X main.Version=$VERSION" -o disk-health-monitor${{ matrix.suffix }} . |
| 63 | + |
| 64 | + - name: Upload build artifacts |
| 65 | + uses: actions/upload-artifact@v3 |
| 66 | + with: |
| 67 | + name: disk-health-monitor-${{ matrix.os }}-${{ matrix.arch }} |
| 68 | + path: disk-health-monitor${{ matrix.suffix }} |
| 69 | + retention-days: 7 |
| 70 | + |
| 71 | + release: |
| 72 | + name: Create Release |
| 73 | + needs: build |
| 74 | + if: startsWith(github.ref, 'refs/tags/') |
| 75 | + runs-on: ubuntu-latest |
| 76 | + steps: |
| 77 | + - uses: actions/checkout@v4 |
| 78 | + |
| 79 | + - name: Download all artifacts |
| 80 | + uses: actions/download-artifact@v3 |
| 81 | + |
| 82 | + - name: Display structure of downloaded files |
| 83 | + run: ls -R |
| 84 | + |
| 85 | + - name: Create release archives |
| 86 | + run: | |
| 87 | + mkdir -p release |
| 88 | + VERSION=$(echo $GITHUB_REF | cut -d / -f 3) |
| 89 | + |
| 90 | + # Linux amd64 |
| 91 | + cp disk-health-monitor-linux-amd64/disk-health-monitor release/disk-health-monitor |
| 92 | + chmod +x release/disk-health-monitor |
| 93 | + tar -czf release/disk-health-monitor-${VERSION}-linux-amd64.tar.gz -C release disk-health-monitor |
| 94 | + rm release/disk-health-monitor |
| 95 | + |
| 96 | + # Linux arm64 |
| 97 | + cp disk-health-monitor-linux-arm64/disk-health-monitor release/disk-health-monitor |
| 98 | + chmod +x release/disk-health-monitor |
| 99 | + tar -czf release/disk-health-monitor-${VERSION}-linux-arm64.tar.gz -C release disk-health-monitor |
| 100 | + rm release/disk-health-monitor |
| 101 | + |
| 102 | + # FreeBSD amd64 |
| 103 | + cp disk-health-monitor-freebsd-amd64/disk-health-monitor release/disk-health-monitor |
| 104 | + chmod +x release/disk-health-monitor |
| 105 | + tar -czf release/disk-health-monitor-${VERSION}-freebsd-amd64.tar.gz -C release disk-health-monitor |
| 106 | + rm release/disk-health-monitor |
| 107 | + |
| 108 | + # Windows amd64 |
| 109 | + cp disk-health-monitor-windows-amd64/disk-health-monitor.exe release/disk-health-monitor.exe |
| 110 | + zip -j release/disk-health-monitor-${VERSION}-windows-amd64.zip release/disk-health-monitor.exe |
| 111 | + rm release/disk-health-monitor.exe |
| 112 | + |
| 113 | + - name: Create GitHub Release |
| 114 | + uses: softprops/action-gh-release@v1 |
| 115 | + with: |
| 116 | + files: | |
| 117 | + release/disk-health-monitor-* |
| 118 | + draft: false |
| 119 | + prerelease: false |
| 120 | + env: |
| 121 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments