Merge pull request #682 from eelcocramer/docker-cross-platform

Enables cross platform building and pushing of docker images
This commit is contained in:
Josh Baker 2023-05-06 07:03:52 -07:00 committed by GitHub
commit cd957dcafd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 40 deletions

View File

@ -2,33 +2,34 @@ name: Go
on: on:
push: push:
branches: [ master ] branches: [master]
pull_request: pull_request:
branches: [ master ] branches: [master]
jobs: jobs:
build: build:
name: Build name: Build
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.19
- name: Set up Go 1.x - name: Check out code
uses: actions/setup-go@v2 uses: actions/checkout@v2
with: with:
go-version: ^1.19 fetch-depth: 0
- name: Check out code - name: Test
uses: actions/checkout@v2 run: make test
with:
fetch-depth: 0
- name: Test - name: Package
run: make test run: make package
- name: Docker push - name: Docker push
env: env:
DOCKER_LOGIN: tidwall DOCKER_LOGIN: tidwall
DOCKER_USER: tile38 DOCKER_USER: tile38
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
run: ./scripts/docker-push.sh run: ./scripts/docker-push.sh

View File

@ -1,9 +1,14 @@
FROM alpine:3.16.2 FROM alpine:3.16.2
ARG VERSION
ARG TARGETOS
ARG TARGETARCH
RUN apk add --no-cache ca-certificates RUN apk add --no-cache ca-certificates
ADD tile38-server /usr/local/bin ADD packages/tile38-$VERSION-$TARGETOS-$TARGETARCH/tile38-server /usr/local/bin
ADD tile38-cli /usr/local/bin ADD packages/tile38-$VERSION-$TARGETOS-$TARGETARCH/tile38-cli /usr/local/bin
ADD tile38-benchmark /usr/local/bin ADD packages/tile38-$VERSION-$TARGETOS-$TARGETARCH/tile38-benchmark /usr/local/bin
RUN addgroup -S tile38 && \ RUN addgroup -S tile38 && \
adduser -S -G tile38 tile38 && \ adduser -S -G tile38 tile38 && \

View File

@ -6,7 +6,7 @@ cd $(dirname "${BASH_SOURCE[0]}")/..
# GIT_BRANCH is the current branch name # GIT_BRANCH is the current branch name
export GIT_BRANCH=$(git branch --show-current) export GIT_BRANCH=$(git branch --show-current)
# GIT_VERSION - always the last verison number, like 1.12.1. # GIT_VERSION - always the last verison number, like 1.12.1.
export GIT_VERSION=$(git describe --tags --abbrev=0) export GIT_VERSION=$(git describe --tags --abbrev=0)
# GIT_COMMIT_SHORT - the short git commit number, like a718ef0. # GIT_COMMIT_SHORT - the short git commit number, like a718ef0.
export GIT_COMMIT_SHORT=$(git rev-parse --short HEAD) export GIT_COMMIT_SHORT=$(git rev-parse --short HEAD)
# DOCKER_REPO - the base repository name to push the docker build to. # DOCKER_REPO - the base repository name to push the docker build to.
@ -15,28 +15,41 @@ export DOCKER_REPO=$DOCKER_USER/tile38
if [ "$GIT_BRANCH" != "master" ]; then if [ "$GIT_BRANCH" != "master" ]; then
echo "Not pushing, not on master" echo "Not pushing, not on master"
elif [ "$DOCKER_USER" == "" ]; then elif [ "$DOCKER_USER" == "" ]; then
echo "Not pushing, DOCKER_USER not set" echo "Not pushing, DOCKER_USER not set"
exit 1 exit 1
elif [ "$DOCKER_LOGIN" == "" ]; then elif [ "$DOCKER_LOGIN" == "" ]; then
echo "Not pushing, DOCKER_LOGIN not set" echo "Not pushing, DOCKER_LOGIN not set"
exit 1 exit 1
elif [ "$DOCKER_PASSWORD" == "" ]; then elif [ "$DOCKER_PASSWORD" == "" ]; then
echo "Not pushing, DOCKER_PASSWORD not set" echo "Not pushing, DOCKER_PASSWORD not set"
exit 1 exit 1
else else
push(){ # setup cross platform builder
docker tag $DOCKER_REPO:$GIT_COMMIT_SHORT $DOCKER_REPO:$1 # https://github.com/tonistiigi/binfmt
docker push $DOCKER_REPO:$1 docker run --privileged --rm tonistiigi/binfmt --install all
echo "Pushed $DOCKER_REPO:$1" docker buildx create --name multiarch --platform linux/amd64,linux/amd64/v2,linux/amd64/v3,linux/arm64,linux/386,linux/arm/v7 --use default
}
# docker login # docker login
echo $DOCKER_PASSWORD | docker login -u $DOCKER_LOGIN --password-stdin echo $DOCKER_PASSWORD | docker login -u $DOCKER_LOGIN --password-stdin
# build the docker image
docker build -f Dockerfile -t $DOCKER_REPO:$GIT_COMMIT_SHORT .
if [ "$(curl -s https://hub.docker.com/v2/repositories/$DOCKER_REPO/tags/$GIT_VERSION/ | grep "digest")" == "" ]; then if [ "$(curl -s https://hub.docker.com/v2/repositories/$DOCKER_REPO/tags/$GIT_VERSION/ | grep "digest")" == "" ]; then
# push the newest tag # build the docker image
push "$GIT_VERSION" docker buildx build \
push "latest" -f Dockerfile \
--platform linux/arm64,linux/amd64 \
--build-arg VERSION=$GIT_VERSION \
--tag $DOCKER_REPO:$GIT_VERSION \
--tag $DOCKER_REPO:latest \
--tag $DOCKER_REPO:edge \
--push \
.
else
# build the docker image
docker buildx build \
-f Dockerfile \
--platform linux/arm64,linux/amd64 \
--build-arg VERSION=$GIT_VERSION \
--tag $DOCKER_REPO:edge \
--push \
.
fi fi
push "edge" fi
fi

View File

@ -42,5 +42,3 @@ else
zip -r -q $bdir.zip $bdir zip -r -q $bdir.zip $bdir
fi fi
# Remove build directory.
rm -rf $bdir