From f77617f529998dc810ecb29e324b88c626a5cb91 Mon Sep 17 00:00:00 2001 From: Shengjing Zhu Date: Tue, 8 Mar 2022 12:22:51 +0800 Subject: [PATCH] feat: publish docker image in ci --- .github/workflows/ci.yaml | 55 +++++++++++++++++++++++++++++++++++++++ Dockerfile | 10 +++++++ docker-bake.hcl | 20 ++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 .github/workflows/ci.yaml create mode 100644 Dockerfile create mode 100644 docker-bake.hcl diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..0232ce1 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,55 @@ +name: ci + +on: + push: + branches: + - "master" + tags: + - "v*" + pull_request: + branches: + - "master" + +jobs: + docker: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + + - name: Docker meta + id: meta + uses: docker/metadata-action@v3 + with: + images: zhusj/wghttp,ghcr.io/zhsj/wghttp + tags: | + type=ref,event=branch + type=ref,event=pr + type=semver,pattern={{version}} + + - name: Login to DockerHub + if: github.event_name != 'pull_request' + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_PASSWORD }} + + - name: Login to GitHub Container Registry + if: github.event_name != 'pull_request' + uses: docker/login-action@v1 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Build + uses: docker/bake-action@v1 + with: + files: | + ./docker-bake.hcl + ${{ steps.meta.outputs.bake-file }} + targets: build + push: ${{ github.event_name != 'pull_request' }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..87cc88b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM --platform=$BUILDPLATFORM golang as builder +WORKDIR /app +COPY . . +RUN go mod vendor +ARG TARGETARCH +RUN CGO_ENABLED=0 GOARCH=$TARGETARCH go build -v -trimpath -ldflags="-w -s" . + +FROM scratch +COPY --from=builder /app/wghttp / +ENTRYPOINT ["/wghttp"] diff --git a/docker-bake.hcl b/docker-bake.hcl new file mode 100644 index 0000000..885a952 --- /dev/null +++ b/docker-bake.hcl @@ -0,0 +1,20 @@ +target "docker-metadata-action" {} + +target "build" { + inherits = ["docker-metadata-action"] + context = "./" + platforms = [ + "linux/386", + "linux/amd64", + "linux/arm", + "linux/arm64", + "linux/mips", + "linux/mips64", + "linux/mips64le", + "linux/mipsle", + "linux/ppc64", + "linux/ppc64le", + "linux/riscv64", + "linux/s390x", + ] +}