Skip to content

Commit

Permalink
fix #298 gh action docker image build failure (#301)
Browse files Browse the repository at this point in the history
* chore: fix badge of docker build gh action

* fix: Dockerfile for multiarch

* fix: #298 Docker build failure

* chore: add usage of Docker and notes for dictionary
  • Loading branch information
KEINOS committed May 9, 2023
1 parent 4b8aea7 commit 772362e
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 221 deletions.
85 changes: 0 additions & 85 deletions .github/workflows/build-docker-latest.yml

This file was deleted.

88 changes: 15 additions & 73 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
name: Release

on:
workflow_dispatch:
push:
tags:
- 'v*'
env:
NAME_IMAGE: "${{ secrets.DOCKER_USERNAME }}/kagome"

jobs:
goreleaser:
Expand All @@ -14,93 +13,36 @@ jobs:
- name: Checkout
uses: actions/checkout@v3
with:
# required for the changelog of goreleaser to work correctly.
fetch-depth: 0

# Setup Go
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version-file: 'go.mod'
cache: true
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v4
with:
version: latest
args: release --rm-dist --config .goreleaser.yml
env:
GITHUB_TOKEN: ${{ secrets.GO_RELEASER_TOKEN }}

docker:
runs-on: ubuntu-latest
steps:
# Clone and checkout the repo. "fetch-depth" is needed in order to get the
# version tag information.
- name: Checkout the code
uses: actions/checkout@v3
with:
fetch-depth: 0

# Install QEMU emulators for Buildx
# Multiarch build
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
with:
platforms: all

# Install Buildx to build multi-arch docker images.
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v2
with:
version: latest

# View available platforms
- name: Available platforms of Buildx
run: echo ${{ steps.buildx.outputs.platforms }}

# Login to Docker Hub. You need to set the below two secreat env variables
# in the repo's [Settings]-[Secrets] settings in advance:
# DOCKER_USERNAME ... Login user name of Docker Hub
# DOCKER_PASSWORD ... Access Token from https://hub.docker.com/settings/security
- name: Login to DockerHub
if: success() && github.event_name != 'pull_request'
# Login to DockerHub before push.
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

# To make the "latest" tag work not only for x86_64(Intel/AMD) but with ARMs
# such as ARM v6, v7 and ARM64 architectures, we need a temporary workaround
# due to the bug of Docker. https://github.com/moby/moby/issues/37647
- name: Build the images
# Build images for each architecture and push them.
run: |
docker system prune -f -a && \
docker buildx build --push --tag ${{ env.NAME_IMAGE }}:arm32v6 --platform linux/arm/v6 --build-arg GOARCH=arm --build-arg GOARM=6 . && \
docker buildx build --push --tag ${{ env.NAME_IMAGE }}:arm32v7 --platform linux/arm/v7 --build-arg GOARCH=arm --build-arg GOARM=7 . && \
docker buildx build --push --tag ${{ env.NAME_IMAGE }}:arm64 --platform linux/arm64 --build-arg GOARCH=arm64 . && \
docker buildx build --push --tag ${{ env.NAME_IMAGE }}:amd64 --platform linux/amd64 .
- name: Pull back the images
# We need to pull back the built images to use the manifest which Docker
# Hub did attach.
run: |
docker system prune -f -a && \
docker pull ${{ env.NAME_IMAGE }}:arm32v6 && \
docker pull ${{ env.NAME_IMAGE }}:arm32v7 && \
docker pull ${{ env.NAME_IMAGE }}:arm64 && \
docker pull ${{ env.NAME_IMAGE }}:amd64
- name: Create current manifest and push
# Create manifest of current Kagome version with "vX.Y.Z" tag.
run: |
git_tag=$(git describe --tag) && \
version_curr=$(echo "${git_tag}" | grep -o -E "(v[0-9]+\.){1}[0-9]+(\.[0-9]+)?" | head -n1) && \
echo '- Removing latest image (prevent conflict)' && \
docker image rm -f ${{ env.NAME_IMAGE }}:latest && \
echo "- Creating manifest for Kagome version: ${version_curr}" && \
export DOCKER_CLI_EXPERIMENTAL=enabled && \
docker manifest create ${{ env.NAME_IMAGE }}:${version_curr} \
${{ env.NAME_IMAGE }}:arm32v6 \
${{ env.NAME_IMAGE }}:arm32v7 \
${{ env.NAME_IMAGE }}:arm64 \
${{ env.NAME_IMAGE }}:amd64 && \
docker manifest annotate ${{ env.NAME_IMAGE }}:${version_curr} ${{ env.NAME_IMAGE }}:arm32v6 --variant v6l && \
docker manifest annotate ${{ env.NAME_IMAGE }}:${version_curr} ${{ env.NAME_IMAGE }}:arm32v7 --variant v7l && \
docker manifest inspect ${{ env.NAME_IMAGE }}:${version_curr} && \
docker manifest push ${{ env.NAME_IMAGE }}:${version_curr} --purge
# Build and release
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v4
with:
version: latest
args: release --config .goreleaser.yml --clean
env:
GITHUB_TOKEN: ${{ secrets.GO_RELEASER_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
101 changes: 98 additions & 3 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,56 @@
# =============================================================================
# GoReleaser configuration file for Kagome
# =============================================================================
# For local testing run:
# goreleaser --skip-publish --clean --snapshot
#
# Note:
# To build multiarch Docker image, you may be required to install Docker buildx
# plugin and QEMU.
env:
- GO111MODULE=on

before:
hooks:
- go mod download

builds:
- env:
- CGO_ENABLED=0
binary: kagome
goos:
- linux
- darwin
- windows
goarch:
- amd64
- arm
- arm64
- arm
# Variant for ARM32
goarm:
- "5"
- "6"
- "7"
# Ignore ARM32 build for both macOS and Windows
ignore:
- goos: darwin
goarch: arm
- goos: windows
goarch: arm
mod_timestamp: '{{ .CommitTimestamp }}'
flags:
- -trimpath
ldflags:
- -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{ .CommitDate }} -X main.builtBy=goreleaser
- -s -w -extldflags '-static' -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{ .CommitDate }} -X main.builtBy=goreleaser

# macOS universal binaries. It will join the binaries for AMD64 and Amd64 into
# a single binary. Suitable for Apple Silicon (Arm64) and Intel (amd64).
universal_binaries:
-
name_template: 'kagome'
# Remove each after joining
replace: true

brews:
- tap:
owner: ikawaha
Expand All @@ -32,4 +64,67 @@ brews:
install: |
bin.install "kagome"
test: |
system "#{bin}/kagome version"
system "#{bin}/kagome version"
# Build Docker images. It will copy the built binary from the "builds" step above.
dockers:
- image_templates:
- "ikawaha/kagome:{{ .Version }}-amd64"
use: buildx
goarch: amd64
dockerfile: Dockerfile
build_flag_templates:
- "--platform=linux/amd64"

- image_templates:
- "ikawaha/kagome:{{ .Version }}-arm64"
use: buildx
goarch: arm64
dockerfile: Dockerfile
build_flag_templates:
- "--platform=linux/arm64/v8"

- image_templates:
- "ikawaha/kagome:{{ .Version }}-arm32v5"
use: buildx
goarch: arm
goarm: 5
dockerfile: Dockerfile
build_flag_templates:
- "--platform=linux/arm/v5"

- image_templates:
- "ikawaha/kagome:{{ .Version }}-arm32v6"
use: buildx
goarch: arm
goarm: 6
dockerfile: Dockerfile
build_flag_templates:
- "--platform=linux/arm/v6"

- image_templates:
- "ikawaha/kagome:{{ .Version }}-arm32v7"
use: buildx
goarch: arm
goarm: 7
dockerfile: Dockerfile
build_flag_templates:
- "--platform=linux/arm/v7"

# Create multiarch manifest file of Docker image. This will bind all the images
# above into a single manifest file for "latest" and each version tag.
docker_manifests:
- name_template: ikawaha/kagome:{{ .Version }}
image_templates:
- ikawaha/kagome:{{ .Version }}-amd64
- ikawaha/kagome:{{ .Version }}-arm64
- ikawaha/kagome:{{ .Version }}-arm32v5
- ikawaha/kagome:{{ .Version }}-arm32v6
- ikawaha/kagome:{{ .Version }}-arm32v7
- name_template: ikawaha/kagome:latest
image_templates:
- ikawaha/kagome:{{ .Version }}-amd64
- ikawaha/kagome:{{ .Version }}-arm64
- ikawaha/kagome:{{ .Version }}-arm32v5
- ikawaha/kagome:{{ .Version }}-arm32v6
- ikawaha/kagome:{{ .Version }}-arm32v7
42 changes: 3 additions & 39 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,39 +1,3 @@
# This is a Docker image of Kagome on Alpine Linux.
# =================================================

# 1st stage: Build binary.
# ------------------------
FROM --platform=$BUILDPLATFORM golang:alpine AS build-app

ARG BUILDPLATFORM
ARG TARGETPLATFORM
ARG GOOS=linux
ARG GOARCH=amd64
ARG GOARM=

# Copy the current dir including .git dir for versioning.
COPY . /go/src/github.com/ikawaha/kagome
WORKDIR /go/src/github.com/ikawaha/kagome

# Shell script to build the image (with the tag as version).
RUN \
apk --no-cache add git && \
version_app=$(git describe --tag) && \
echo "- Running on ${BUILDPLATFORM}, building for ${TARGETPLATFORM}" && \
echo "- Current platform: $(uname -a)" && \
echo "- Current git tag: ${version_app}" && \
echo "- Current Go version: $(go version)" && \
echo "- Kagome version to be build:${version_app}" && \
CGO_ENABLED=0 GOOS=${GOOS} GOARCH=${GOARCH} GOARM=${GOARM} go build \
--ldflags "-w -s -extldflags \"-static\" -X 'main.version=${version_app}'" \
-o /go/bin/kagome \
/go/src/github.com/ikawaha/kagome && \
echo "- Smoke test (run kagome version command) ... $(/go/bin/kagome version)"

# 2nd stage: Copy only the built binary to shrink the image size.
# ---------------------------------------------------------------
FROM --platform=$BUILDPLATFORM alpine:latest

COPY --from=build-app /go/bin/kagome /usr/local/bin/kagome

ENTRYPOINT [ "kagome" ]
FROM scratch
COPY kagome /
ENTRYPOINT ["/kagome"]

0 comments on commit 772362e

Please sign in to comment.