From 41f45e4c9bfa9e102077e0db71dd1c0d80b79563 Mon Sep 17 00:00:00 2001 From: rick Date: Thu, 4 Jan 2024 09:42:58 +0000 Subject: [PATCH] feat: support to build and release mutiple arch images --- .github/workflows/pull-request.yaml | 28 +++++++++++++++++++++++ .github/workflows/release.yaml | 35 +++++++++++++++++++++++++++++ .goreleaser.yml | 16 ------------- Dockerfile | 15 ++++++++++++- Makefile | 6 ++--- pkg/installer/check.go | 4 ++-- 6 files changed, 82 insertions(+), 22 deletions(-) diff --git a/.github/workflows/pull-request.yaml b/.github/workflows/pull-request.yaml index 25b9389..8fbd0d0 100644 --- a/.github/workflows/pull-request.yaml +++ b/.github/workflows/pull-request.yaml @@ -96,3 +96,31 @@ jobs: - uses: gaurav-nelson/github-action-markdown-link-check@1.0.13 with: use-verbose-mode: 'yes' + + image: + runs-on: ubuntu-20.04 + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-tags: true + fetch-depth: 0 + - name: Setup Docker buildx + uses: docker/setup-buildx-action@79abd3f86f79a9d68a23c75a09a9a85889262adf + - name: Extract Docker metadata + id: meta + uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38 + with: + images: ${{ env.REGISTRY }}/linuxsuren/hd + - name: Build and push Docker image + id: build-and-push + uses: docker/build-push-action@ac9327eae2b366085ac7f6a2d02df8aa8ead720a + with: + context: . + push: false + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + platforms: linux/amd64,linux/arm64 + cache-from: type=gha + cache-to: type=gha,mode=max + build-args: VERSION=${{ steps.vars.outputs.tag }} \ No newline at end of file diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index da16c8a..4bf4737 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -30,6 +30,41 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GH_PUBLISH_SECRETS }} + image: + runs-on: ubuntu-20.04 + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-tags: true + fetch-depth: 0 + - name: Setup Docker buildx + uses: docker/setup-buildx-action@79abd3f86f79a9d68a23c75a09a9a85889262adf + - name: Log into registry ${{ env.REGISTRY }} + if: github.event_name != 'pull_request' + uses: docker/login-action@28218f9b04b4f3f62068d7b6ce6ca5b26e35336c + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GH_PUBLISH_SECRETS }} + - name: Extract Docker metadata + id: meta + uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38 + with: + images: ${{ env.REGISTRY }}/linuxsuren/hd + - name: Build and push Docker image + id: build-and-push + uses: docker/build-push-action@ac9327eae2b366085ac7f6a2d02df8aa8ead720a + with: + context: . + push: ${{ github.event_name != 'pull_request' }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + platforms: linux/amd64,linux/arm64 + cache-from: type=gha + cache-to: type=gha,mode=max + build-args: VERSION=${{ steps.vars.outputs.tag }} + msi: needs: goreleaser runs-on: windows-latest diff --git a/.goreleaser.yml b/.goreleaser.yml index deb71b6..3ca6e49 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -71,19 +71,3 @@ brews: bin.install name prefix.install_metafiles -dockers: - - goos: linux - goarch: amd64 - image_templates: - - "surenpi/hd:latest" - - "surenpi/hd:{{.Tag}}" - - "ghcr.io/linuxsuren/hd:latest" - - "ghcr.io/linuxsuren/hd:{{.Tag}}" - build_flag_templates: - - "--pull" - - "--label=org.opencontainers.image.created={{.Date}}" - - "--label=org.opencontainers.image.title={{.ProjectName}}" - - "--label=org.opencontainers.image.revision={{.FullCommit}}" - - "--label=org.opencontainers.image.version={{.Version}}" - - "--label=org.opencontainers.image.source=https://github.com/LinuxSuRen/http-downloader" - - "--platform=linux/amd64" diff --git a/Dockerfile b/Dockerfile index 827614e..17c480f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,19 @@ +FROM docker.io/golang:1.19 AS builder + +WORKDIR /workspace +COPY cmd/ cmd/ +COPY hack/ hack/ +COPY mock/ mock/ +COPY pkg/ pkg/ +COPY main.go . +COPY README.md README.md +COPY go.mod go.mod +COPY go.sum go.sum +RUN CGO_ENABLED=0 go build -ldflags "-w -s" -o /usr/local/bin/hd . + FROM alpine:3.10 -COPY hd /usr/local/bin/hd +COPY --from=builder /usr/local/bin/hd /usr/local/bin/hd RUN hd fetch CMD ["hd"] diff --git a/Makefile b/Makefile index ee985fb..81b917a 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,13 @@ build: fmt test export GOPROXY=https://goproxy.io - CGO_ENABLE=0 go build -ldflags "-w -s" -o bin/hd + CGO_ENABLED=0 go build -ldflags "-w -s" -o bin/hd build-windows: - GOOS=windows CGO_ENABLE=0 go build -ldflags "-w -s" -o bin/windows/hd.exe + GOOS=windows CGO_ENABLED=0 go build -ldflags "-w -s" -o bin/windows/hd.exe build-linux: fmt lint build-linux-no-check build-linux-no-check: export GOPROXY=https://goproxy.io - CGO_ENABLE=0 GOOS=linux go build -ldflags "-w -s" -o bin/linux/hd + CGO_ENABLED=0 GOOS=linux go build -ldflags "-w -s" -o bin/linux/hd upx bin/linux/hd test: fmt diff --git a/pkg/installer/check.go b/pkg/installer/check.go index 6d79a3a..34cdff6 100644 --- a/pkg/installer/check.go +++ b/pkg/installer/check.go @@ -575,9 +575,9 @@ func GetProxyServers() []string { if err = yaml.Unmarshal(data, proxyServer); err == nil { return proxyServer.Servers - } else { - log.Println("failed to parse config file", err) } + + log.Println("failed to parse config file", err) } else { log.Println("failed to read config file", err) }