From 607a2ce7eca5a547447b0f16926fbc996d0fa84d Mon Sep 17 00:00:00 2001 From: Wang Date: Thu, 3 Dec 2020 11:16:31 +0800 Subject: [PATCH] build buildx --- .github/workflows/build-image.yml | 55 +++++++++++++++++++++++++++++++ Dockerfile.docker-buildx | 22 +++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 .github/workflows/build-image.yml create mode 100644 Dockerfile.docker-buildx diff --git a/.github/workflows/build-image.yml b/.github/workflows/build-image.yml new file mode 100644 index 00000000..d34690b4 --- /dev/null +++ b/.github/workflows/build-image.yml @@ -0,0 +1,55 @@ +name: build-image + +on: + push: + branches: + - "image-buildx" + +jobs: + buildx: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + with: + submodules: true + + - uses: docker/setup-qemu-action@v1 + - uses: docker/setup-buildx-action@v1 + with: + driver-opts: network=host + + - id: prepare + name: Build binaries && Prepare + env: + GITHUB_REF: ${{ github.sha }} + GITHUB_SHA: ${{ github.ref }} + TARGET_PLATFORMS: linux/amd64,linux/arm64 + run: |- + export TAG=${GITHUB_SHA::7} + + if [[ ${{ github.ref }} == "refs/tags/v"* ]]; then + TAG=$(echo ${{ github.ref }} | sed -e "s/refs\/tags\///") + fi + + echo ::set-output name=target_platforms::${TARGET_PLATFORMS} + echo ::set-output name=tag::${TAG} + + - uses: docker/login-action@v1 + name: Login docker.io + with: + password: ${{ secrets.DOCKER_PASSWORD }} + registry: docker.io + username: ${{ secrets.DOCKER_USERNAME }} + + - uses: docker/build-push-action@v2 + name: Build image && May Push + with: + file: Dockerfile.docker-buildx + context: . + labels: |- + org.opencontainers.image.source=https://github.com/${{ github.repository }} + org.opencontainers.image.revision=${{ github.sha }} + platforms: ${{ steps.prepare.outputs.target_platforms }} + push: ${{ github.event_name != 'pull_request' }} + tags: | + docker.io/${{ github.repository }}:${{ steps.prepare.outputs.tag }} diff --git a/Dockerfile.docker-buildx b/Dockerfile.docker-buildx new file mode 100644 index 00000000..21cfd456 --- /dev/null +++ b/Dockerfile.docker-buildx @@ -0,0 +1,22 @@ +# syntax = docker/dockerfile:experimental + +FROM --platform=${BUILDPLATFORM} golang:1.15.5 AS builder + +WORKDIR /go/src + +COPY ./ ./ + +ARG GOPROXY +RUN --mount=type=cache,id=gomod,target=/go/pkg/mod go mod download + +ARG TARGETARCH +RUN --mount=type=cache,id=gomod,target=/go/pkg/mod CGO_ENABLED=0 GOARCH=${TARGETARCH} go build -o /go/bin/docker-buildx-${TARGETARCH} ./cmd/buildx; + +FROM docker + +RUN apk add --no-cache make + +ARG TARGETARCH +COPY --from=builder /go/bin/docker-buildx-${TARGETARCH} /root/.docker/cli-plugins/docker-buildx + +ENV DOCKER_CLI_EXPERIMENTAL=enabled \ No newline at end of file