Skip to content

Commit

Permalink
fix: fix multi platform go build in docker push workflow (cosmos#13993)
Browse files Browse the repository at this point in the history
* publish to ghcr

* build for target os and arch

* replace deprecated set-output

* fix insufficient scope error

* replace old code with docker metadata action

* disable default latest

Co-authored-by: Marko <[email protected]>
  • Loading branch information
rnbguy and tac0turtle authored Nov 27, 2022
1 parent 60fdbd0 commit 45cc1b0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 30 deletions.
54 changes: 26 additions & 28 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ on:

permissions:
contents: read
packages: write

env:
# Use docker.io for Docker Hub if empty
REGISTRY: ghcr.io
# github.repository as <account>/<repo>
IMAGE_NAME: ${{ github.repository }}

jobs:
build:
Expand All @@ -23,43 +30,34 @@ jobs:
with:
fetch-depth: 0

- name: Prepare
id: prep
run: |
DOCKER_IMAGE=interchainio/simapp
VERSION=noop
if [[ $GITHUB_REF == refs/tags/* ]]; then
VERSION=${GITHUB_REF#refs/tags/}
elif [[ $GITHUB_REF == refs/heads/* ]]; then
VERSION=$(echo ${GITHUB_REF#refs/heads/} | sed -r 's#/+#-#g')
if [ "${{ github.event.repository.default_branch }}" = "$VERSION" ]; then
VERSION=latest
fi
fi
TAGS="${DOCKER_IMAGE}:${VERSION}"
if [[ $VERSION =~ ^v[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
TAGS="$TAGS,${DOCKER_IMAGE}:${VERSION}"
fi
echo ::set-output name=version::${VERSION}
echo ::set-output name=tags::${TAGS}
echo ::set-output name=created::$(date -u +'%Y-%m-%dT%H:%M:%SZ')
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=raw,value=latest,enable={{is_default_branch}}
type=semver,pattern={{version}}
type=semver,pattern=latest-{{major}}.{{minor}}
flavor: |
latest=false
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Login to DockerHub
- name: Log into registry ${{ env.REGISTRY }}
if: ${{ github.event_name != 'pull_request' }}
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Publish to Docker Hub
- name: Publish to GitHub Packages
uses: docker/build-push-action@v3
with:
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.prep.outputs.tags }}
tags: ${{ steps.meta.outputs.tags }}
cache-from: type=gha
cache-to: type=gha,mode=max
8 changes: 6 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# > docker run -it -p 26657:26657 -p 26656:26656 -v ~/.simappcli:/root/.simapp simapp simd keys add foo
# > docker run -it -p 26657:26657 -p 26656:26656 -v ~/.simappcli:/root/.simapp simapp simd keys list
# TODO: demo connecting rest-server (or is this in server now?)
FROM golang:1.19-alpine AS build-env
FROM --platform=$BUILDPLATFORM golang:1.19-alpine AS build-env

# Install minimum necessary dependencies
ENV PACKAGES curl make git libc-dev bash gcc linux-headers eudev-dev python3
Expand All @@ -22,8 +22,12 @@ WORKDIR /go/src/github.com/cosmos/cosmos-sdk
# Add source files
COPY . .

# Dockerfile Cross-Compilation Guide
# https://www.docker.com/blog/faster-multi-platform-builds-dockerfile-cross-compilation-guide
ARG TARGETOS TARGETARCH

# install simapp, remove packages
RUN make build
RUN GOOS=$TARGETOS GOARCH=$TARGETARCH make build


# Final image
Expand Down

0 comments on commit 45cc1b0

Please sign in to comment.