From 18f4b42cf121dd9a5f9f9850292bba35d5eccdfe Mon Sep 17 00:00:00 2001 From: Andrea Spacca Date: Sun, 14 Jun 2020 19:53:36 +0200 Subject: [PATCH] ISSUE-313-BUILDX --- .github/workflow/buildx.yml | 67 +++++++++++++++++++++++++++++++++++++ Dockerfile | 8 ++++- 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 .github/workflow/buildx.yml diff --git a/.github/workflow/buildx.yml b/.github/workflow/buildx.yml new file mode 100644 index 0000000..8941515 --- /dev/null +++ b/.github/workflow/buildx.yml @@ -0,0 +1,67 @@ +# ref: https://github.com/crazy-max/diun/blob/master/.github/workflows/build.yml + +name: Docker +on: [push] +jobs: + build: + runs-on: ubuntu-18.04 + steps: + - name: Prepare + id: prepare + run: | + if [[ $GITHUB_REF == refs/tags/* ]]; then + echo ::set-output name=version::${GITHUB_REF#refs/tags/v} + elif [[ $GITHUB_REF == refs/heads/master ]]; then + echo ::set-output name=version::latest + elif [[ $GITHUB_REF == refs/heads/* ]]; then + echo ::set-output name=version::${GITHUB_REF#refs/heads/} + else + echo ::set-output name=version::snapshot + fi + echo ::set-output name=docker_platforms::linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/386 + echo ::set-output name=docker_image::${{ secrets.DOCKER_USERNAME }}/${{ github.event.repository.name }} + # https://github.com/crazy-max/ghaction-docker-buildx + - name: Set up Docker Buildx + id: buildx + uses: crazy-max/ghaction-docker-buildx@v1 + with: + version: latest + + - name: Environment + run: | + echo home=$HOME + echo git_ref=$GITHUB_REF + echo git_sha=$GITHUB_SHA + echo version=${{ steps.prepare.outputs.version }} + echo image=${{ steps.prepare.outputs.docker_image }} + echo platforms=${{ steps.prepare.outputs.docker_platforms }} + echo avail_platforms=${{ steps.buildx.outputs.platforms }} + # https://github.com/actions/checkout + - name: Checkout + uses: actions/checkout@v2 + + - name: Docker Buildx (no push) + run: | + docker buildx bake \ + --set ${{ github.event.repository.name }}.platform=${{ steps.prepare.outputs.docker_platforms }} \ + --set ${{ github.event.repository.name }}.output=type=image,push=false \ + --set ${{ github.event.repository.name }}.tags="${{ steps.prepare.outputs.docker_image }}:${{ steps.prepare.outputs.version }}" \ + --file Dockerfile + - name: Docker Login + if: success() + env: + DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} + run: | + echo "${DOCKER_PASSWORD}" | docker login --username "${{ secrets.DOCKER_USERNAME }}" --password-stdin + - name: Docker Buildx (push) + if: success() + run: | + docker buildx bake \ + --set ${{ github.event.repository.name }}.platform=${{ steps.prepare.outputs.docker_platforms }} \ + --set ${{ github.event.repository.name }}.output=type=image,push=true \ + --set ${{ github.event.repository.name }}.tags="${{ steps.prepare.outputs.docker_image }}:${{ steps.prepare.outputs.version }}" \ + --file docker-compose.yaml + - name: Clear + if: always() + run: | + rm -f ${HOME}/.docker/config.json \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 0018ba1..6ba642c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,12 @@ # Default to Go 1.13 ARG GO_VERSION=1.13 -FROM golang:${GO_VERSION}-alpine as build +FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine as build + +# Convert TARGETPLATFORM to GOARCH format +# https://github.com/tonistiigi/xx +COPY --from=tonistiigi/xx:golang / / + +ARG TARGETPLATFORM # Necessary to run 'go get' and to compile the linked binary RUN apk add git musl-dev