From a76095d1cd3c8878cf8a802f72e855409fe7f3a9 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Fri, 19 Aug 2022 14:31:35 +0200 Subject: [PATCH] feat: add multiple docker images (backport #2440) (#2442) * feat: add multiple docker images (#2440) * Change default image && copy all go deps in 1 layer * Extend workflow to push multiple docker images (cherry picked from commit 4b5eccf06d4a9aceafc62bc9b6c534c63985cf19) # Conflicts: # Dockerfile * Fix conflicts Co-authored-by: Niccolo Raspa <6024049+niccoloraspa@users.noreply.github.com> Co-authored-by: Niccolo Raspa --- .github/workflows/docker.yml | 111 +++++++++++++++++++++++++++++++---- Dockerfile | 5 +- 2 files changed, 103 insertions(+), 13 deletions(-) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 955bd5fe19d..c4f16ac0c11 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -1,4 +1,25 @@ -name: docker +# This workflow pushes new osmosis docker images on every new tag. +# +# On every new `vX.Y.Z` tag the following images are pushed: +# +# osmolabs/osmosis:X.Y.Z # is pushed +# osmolabs/osmosis:X.Y # is updated to X.Y.Z +# osmolabs/osmosis:X # is updated to X.Y.Z +# osmolabs/osmosis:latest # is updated to X.Y.Z +# +# The same osmosisd binary is copied in different base runner images: +# +# - `osmolabs/osmosis:X.Y.Z` uses `gcr.io/distroless/static` +# - `osmolabs/osmosis:X.Y.Z-distroless` uses `gcr.io/distroless/static` +# - `osmolabs/osmosis:X.Y.Z-nonroot` uses `gcr.io/distroless/static:nonroot` +# - `osmolabs/osmosis:X.Y.Z-alpine` uses `alpine:3.16` +# +# All the images above have support for linux/amd64 and linux/arm64. +# +# Due to QEMU virtualization used to build multi-platform docker images +# this workflow might take a while to complete. + +name: Push Docker Images on: push: @@ -7,6 +28,9 @@ on: env: DOCKER_REPOSITORY: osmolabs/osmosis + RUNNER_BASE_IMAGE_DISTROLESS: gcr.io/distroless/static + RUNNER_BASE_IMAGE_NONROOT: gcr.io/distroless/static:nonroot + RUNNER_BASE_IMAGE_ALPINE: alpine:3.16 jobs: docker: @@ -17,19 +41,27 @@ jobs: uses: actions/checkout@v2 - name: Set up QEMU - uses: docker/setup-qemu-action@v1 + uses: docker/setup-qemu-action@v2 - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 + uses: docker/setup-buildx-action@v2 - name: Login to DockerHub - uses: docker/login-action@v1 + uses: docker/login-action@v2 with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - - name: Docker meta - id: meta + name: Find go version + id: find_go_version + run: | + GO_VERSION=$(cat go.mod | grep -E 'go [0-9].[0-9]+' | cut -d ' ' -f 2) + echo "::set-output name=go_version::$(echo ${GO_VERSION})" + + # Distroless Docker image (default) + - + name: Docker meta (distroless) + id: meta_distroless uses: docker/metadata-action@v3 with: images: ${{ env.DOCKER_REPOSITORY }} @@ -37,16 +69,75 @@ jobs: type=semver,pattern={{version}} type=semver,pattern={{major}}.{{minor}} type=semver,pattern={{major}} + type=semver,pattern={{version}}-distroless + type=semver,pattern={{major}}.{{minor}}-distroless + type=semver,pattern={{major}}-distroless - - name: Build and push + name: Build and push (distroless) + id: build_push_distroless uses: docker/build-push-action@v2 with: file: Dockerfile context: . push: true platforms: linux/amd64,linux/arm64 - tags: ${{ steps.meta.outputs.tags }} + build-args: | + GO_VERSION=${{ steps.find_go_version.outputs.go_version }} + RUNNER_IMAGE=${{ env.RUNNER_BASE_IMAGE_DISTROLESS }} + tags: ${{ steps.meta_distroless.outputs.tags }} + # Distroless nonroot Docker image + - + name: Docker meta (nonroot) + id: meta_nonroot + uses: docker/metadata-action@v3 + with: + images: ${{ env.DOCKER_REPOSITORY }} + flavor: | + latest=false + suffix=-nonroot + tags: | + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=semver,pattern={{major}} + - + name: Build and push (nonroot) + id: build_push_nonroot + uses: docker/build-push-action@v2 + with: + file: Dockerfile + context: . + push: true + platforms: linux/amd64,linux/arm64 + build-args: | + GO_VERSION=${{ steps.find_go_version.outputs.go_version }} + RUNNER_IMAGE=${{ env.RUNNER_BASE_IMAGE_NONROOT }} + tags: ${{ steps.meta_nonroot.outputs.tags }} + + # Alpine Docker image + - + name: Docker meta (alpine) + id: meta_alpine + uses: docker/metadata-action@v3 + with: + images: ${{ env.DOCKER_REPOSITORY }} + flavor: | + latest=false + suffix=-alpine + tags: | + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=semver,pattern={{major}} - - name: Image digest - run: echo ${{ steps.docker_build.outputs.digest }} \ No newline at end of file + name: Build and push (alpine) + id: build_push_alpine + uses: docker/build-push-action@v2 + with: + file: Dockerfile + context: . + push: true + platforms: linux/amd64,linux/arm64 + build-args: | + GO_VERSION=${{ steps.find_go_version.outputs.go_version }} + RUNNER_IMAGE=${{ env.RUNNER_BASE_IMAGE_ALPINE }} + tags: ${{ steps.meta_alpine.outputs.tags }} diff --git a/Dockerfile b/Dockerfile index 7fe04348997..f368920296a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ # syntax=docker/dockerfile:1 ARG GO_VERSION="1.18" -ARG RUNNER_IMAGE="gcr.io/distroless/static:nonroot" +ARG RUNNER_IMAGE="gcr.io/distroless/static" # -------------------------------------------------------- # Builder @@ -13,8 +13,7 @@ RUN set -eux; apk add --no-cache ca-certificates build-base; apk add git linux-h # Download go dependencies WORKDIR /osmosis -COPY go.mod go.mod -COPY go.sum go.sum +COPY go.* . RUN --mount=type=cache,target=/root/.cache/go-build \ --mount=type=cache,target=/root/go/pkg/mod \ go mod download