|
| 1 | +name: Release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: write |
| 9 | + packages: write |
| 10 | + |
| 11 | +jobs: |
| 12 | + release: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + outputs: |
| 15 | + tag: ${{ steps.version.outputs.tag }} |
| 16 | + should_release: ${{ steps.version.outputs.should_release }} |
| 17 | + steps: |
| 18 | + - uses: actions/checkout@v4 |
| 19 | + with: |
| 20 | + fetch-depth: 0 |
| 21 | + |
| 22 | + - name: Read version |
| 23 | + id: version |
| 24 | + run: | |
| 25 | + if ! git diff --name-only ${{ github.event.before }} ${{ github.sha }} | grep -q "^VERSION$"; then |
| 26 | + echo "should_release=false" >> $GITHUB_OUTPUT |
| 27 | + exit 0 |
| 28 | + fi |
| 29 | +
|
| 30 | + VERSION=$(tr -d '\n' < VERSION) |
| 31 | + if [ -z "$VERSION" ]; then |
| 32 | + echo "VERSION file is empty" |
| 33 | + exit 1 |
| 34 | + fi |
| 35 | +
|
| 36 | + TAG="v$VERSION" |
| 37 | + if git rev-parse "$TAG" >/dev/null 2>&1; then |
| 38 | + echo "Tag $TAG already exists" |
| 39 | + exit 1 |
| 40 | + fi |
| 41 | +
|
| 42 | + echo "tag=$TAG" >> $GITHUB_OUTPUT |
| 43 | + echo "should_release=true" >> $GITHUB_OUTPUT |
| 44 | +
|
| 45 | + - name: Create GitHub release |
| 46 | + if: steps.version.outputs.should_release == 'true' |
| 47 | + env: |
| 48 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 49 | + run: | |
| 50 | + gh release create "${{ steps.version.outputs.tag }}" \ |
| 51 | + --title "${{ steps.version.outputs.tag }}" \ |
| 52 | + --notes "Release ${{ steps.version.outputs.tag }}" |
| 53 | +
|
| 54 | + build-binaries: |
| 55 | + runs-on: ubuntu-latest |
| 56 | + needs: release |
| 57 | + if: needs.release.outputs.should_release == 'true' |
| 58 | + strategy: |
| 59 | + matrix: |
| 60 | + include: |
| 61 | + - goos: linux |
| 62 | + goarch: amd64 |
| 63 | + - goos: linux |
| 64 | + goarch: arm64 |
| 65 | + - goos: darwin |
| 66 | + goarch: amd64 |
| 67 | + - goos: darwin |
| 68 | + goarch: arm64 |
| 69 | + - goos: windows |
| 70 | + goarch: amd64 |
| 71 | + steps: |
| 72 | + - uses: actions/checkout@v4 |
| 73 | + |
| 74 | + - name: Set up Go |
| 75 | + uses: actions/setup-go@v5 |
| 76 | + with: |
| 77 | + go-version: '1.21' |
| 78 | + |
| 79 | + - name: Build |
| 80 | + env: |
| 81 | + GOOS: ${{ matrix.goos }} |
| 82 | + GOARCH: ${{ matrix.goarch }} |
| 83 | + CGO_ENABLED: 0 |
| 84 | + run: | |
| 85 | + EXT="" |
| 86 | + if [ "${{ matrix.goos }}" = "windows" ]; then |
| 87 | + EXT=".exe" |
| 88 | + fi |
| 89 | + go build -ldflags="-s -w" -o homedash-${{ matrix.goos }}-${{ matrix.goarch }}${EXT} . |
| 90 | +
|
| 91 | + - name: Upload release asset |
| 92 | + env: |
| 93 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 94 | + run: | |
| 95 | + EXT="" |
| 96 | + if [ "${{ matrix.goos }}" = "windows" ]; then |
| 97 | + EXT=".exe" |
| 98 | + fi |
| 99 | + gh release upload "${{ needs.release.outputs.tag }}" \ |
| 100 | + "homedash-${{ matrix.goos }}-${{ matrix.goarch }}${EXT}" |
| 101 | +
|
| 102 | + docker: |
| 103 | + runs-on: ubuntu-latest |
| 104 | + needs: release |
| 105 | + if: needs.release.outputs.should_release == 'true' |
| 106 | + permissions: |
| 107 | + contents: read |
| 108 | + packages: write |
| 109 | + |
| 110 | + strategy: |
| 111 | + matrix: |
| 112 | + platform: |
| 113 | + - linux/amd64 |
| 114 | + - linux/arm64/v8 |
| 115 | + - linux/arm/v7 |
| 116 | + |
| 117 | + steps: |
| 118 | + - uses: actions/checkout@v4 |
| 119 | + |
| 120 | + - name: Set up QEMU |
| 121 | + uses: docker/setup-qemu-action@v3 |
| 122 | + |
| 123 | + - name: Set up Docker Buildx |
| 124 | + uses: docker/setup-buildx-action@v3 |
| 125 | + |
| 126 | + - name: Log in to GitHub Container Registry |
| 127 | + uses: docker/login-action@v3 |
| 128 | + with: |
| 129 | + registry: ghcr.io |
| 130 | + username: ${{ github.actor }} |
| 131 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 132 | + |
| 133 | + - name: Extract metadata |
| 134 | + id: meta |
| 135 | + uses: docker/metadata-action@v5 |
| 136 | + with: |
| 137 | + images: ghcr.io/${{ github.repository }} |
| 138 | + tags: | |
| 139 | + type=raw,value=latest |
| 140 | + type=raw,value=${{ needs.release.outputs.tag }} |
| 141 | + flavor: | |
| 142 | + suffix=-${{ matrix.platform }} |
| 143 | +
|
| 144 | + - name: Build and push Docker image |
| 145 | + uses: docker/build-push-action@v5 |
| 146 | + with: |
| 147 | + context: . |
| 148 | + platforms: ${{ matrix.platform }} |
| 149 | + push: true |
| 150 | + tags: ${{ steps.meta.outputs.tags }} |
| 151 | + labels: ${{ steps.meta.outputs.labels }} |
| 152 | + cache-from: type=gha,scope=${{ matrix.platform }} |
| 153 | + cache-to: type=gha,mode=max,scope=${{ matrix.platform }} |
| 154 | + provenance: false |
| 155 | + |
| 156 | + docker-manifest: |
| 157 | + runs-on: ubuntu-latest |
| 158 | + needs: docker |
| 159 | + if: needs.release.outputs.should_release == 'true' |
| 160 | + permissions: |
| 161 | + contents: read |
| 162 | + packages: write |
| 163 | + |
| 164 | + steps: |
| 165 | + - name: Log in to GitHub Container Registry |
| 166 | + uses: docker/login-action@v3 |
| 167 | + with: |
| 168 | + registry: ghcr.io |
| 169 | + username: ${{ github.actor }} |
| 170 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 171 | + |
| 172 | + - name: Extract metadata |
| 173 | + id: meta |
| 174 | + uses: docker/metadata-action@v5 |
| 175 | + with: |
| 176 | + images: ghcr.io/${{ github.repository }} |
| 177 | + tags: | |
| 178 | + type=raw,value=latest |
| 179 | + type=raw,value=${{ needs.release.outputs.tag }} |
| 180 | +
|
| 181 | + - name: Create and push multi-arch manifest |
| 182 | + run: | |
| 183 | + TAGS="${{ steps.meta.outputs.tags }}" |
| 184 | + for TAG in $TAGS; do |
| 185 | + docker buildx imagetools create -t $TAG \ |
| 186 | + ${TAG}-linux-amd64 \ |
| 187 | + ${TAG}-linux-arm64-v8 \ |
| 188 | + ${TAG}-linux-arm-v7 |
| 189 | + done |
0 commit comments