Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Finalize docker build script #693

Merged
merged 2 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 0 additions & 114 deletions .github/workflows/container.yml

This file was deleted.

66 changes: 66 additions & 0 deletions .github/workflows/image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: Publish Docker Container Images
on:
push:
branches: [main]

permissions:
contents: read
packages: write

jobs:
Per-SHA-Image:
runs-on: ubuntu-latest
defaults:
run:
working-directory: build
env:
PLATFORMS: linux/amd64,linux/arm64
steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Build Corso Binaries
run: >
./build.sh
--platforms ${{ env.PLATFORMS }}

# - name: Build Corso Binaries Locally
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the plan to switch to multiplatform-binary.sh here before merge?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not at this time, due to the cross-platform build failures, seen here. We're unable to use that script in CI deployment, unless we connive some way around it. I should actually remove that commented section.

Copy link
Contributor

@vkamra vkamra Aug 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear to me why this doesn't hit the same issue - we're still cross-compiling when running build in the golang container - right? Also - this discussion is not a blocker for the PR which I've approved.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted. And, yeah, it isn't apparent to me either. Likewise, my local build doesn't have the same issue. Taking shots in the dark here, but I suppose the container could have some built-in emulation to help it along in some way. I'd expect github's ubuntu-latest runtime to have similar. But it's difficult to know.

# run: >
# ./multiplatform-binary.sh
# --platforms ${{ env.PLATFORMS }}

# apparently everyone uses this step
- name: Set up QEMU
uses: docker/setup-qemu-action@v2

# setup Docker buld action
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v2

# retrieve credentials for ghcr.io
- name: Login to Github Packages
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# deploy the image
- name: Build image and push to GitHub Container Registry
env:
sha: git_hash=$(git rev-parse --short "$GITHUB_SHA")
uses: docker/build-push-action@v3
with:
context: .
file: ./build/Dockerfile
platforms: ${{ env.PLATFORMS }}
push: true
tags: ghcr.io/alcionai/corso:${{ env.sha }}
# use the github cache
cache-from: type=gha
cache-to: type=gha,mode=max

# check the image digest
- name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
4 changes: 3 additions & 1 deletion build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ FROM gcr.io/distroless/base-debian10

WORKDIR /

COPY ./bin/corso ./
ARG TARGETOS
ARG TARGETARCH
COPY ./bin/${TARGETOS}-${TARGETARCH}/corso ./

USER nonroot:nonroot

Expand Down
17 changes: 14 additions & 3 deletions build/build-container.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ usage() {
echo "Flags"
echo " -h|--help Help"
echo " -a|--arch Set the architecture to the specified value (default: amd64)"
echo " -l|--local Build the corso binary on your local system, rather than a go image"
echo " -p|--prefix Prefixes the image name."
echo " -s|--suffix Suffixes the version."
echo " "
Expand All @@ -30,6 +31,7 @@ OS=linux
ARCH=amd64
IMAGE_NAME_PREFIX=
IMAGE_TAG_SUFFIX=
LOCAL=

while [ "$#" -gt 0 ]
do
Expand All @@ -42,6 +44,9 @@ do
ARCH=$2
shift
;;
-l|--local)
LOCAL=1
;;
-p|--prefix)
IMAGE_NAME_PREFIX=$2
shift
Expand All @@ -62,6 +67,8 @@ do
shift
done

TARGETPLATFORM=${OS}/${ARCH}

IMAGE_TAG=${OS}-${ARCH}
if [ ! -z "${IMAGE_TAG_SUFFIX}" ]; then
IMAGE_TAG=${IMAGE_TAG}-${IMAGE_TAG_SUFFIX}
Expand All @@ -72,16 +79,20 @@ if [ ! -z "${IMAGE_NAME_PREFIX}" ]; then
IMAGE_NAME=${IMAGE_NAME_PREFIX}/${IMAGE_NAME}
fi

${SCRIPT_ROOT}/build.sh --arch ${ARCH}
if [ -z "$LOCAL" ]; then
${SCRIPT_ROOT}/build.sh --platforms "${TARGETPLATFORM}"
else
${SCRIPT_ROOT}/multiplatform-binary.sh --platforms "${TARGETPLATFORM}"
fi

echo "-----"
echo "building corso container ${IMAGE_NAME}"
echo "-----"

set -x
docker buildx build --tag ${IMAGE_NAME} \
--platform ${OS}/${ARCH} \
--file ${PROJECT_ROOT}/build/Dockerfile \
--platform ${TARGETPLATFORM} \
--file ${PROJECT_ROOT}/build/Dockerfile \
${PROJECT_ROOT}
set +x

Expand Down
67 changes: 40 additions & 27 deletions build/build.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

set -e

Expand All @@ -16,15 +16,16 @@ CORSO_MOD_CACHE=${CORSO_BUILD_PKG_MOD}/cache

CORSO_BUILD_ARGS=''

platforms=
GOVER=1.18
GOOS=linux
GOARCH=amd64

while [ "$#" -gt 0 ]
do
case "$1" in
--arch)
GOARCH=$2
--platforms)
platforms=$2
shift
;;
esac
Expand All @@ -36,27 +37,39 @@ mkdir -p ${CORSO_BUILD_TMP_CACHE}
# temporary directory for caching go modules (needed for fast cross-platform build)
mkdir -p ${CORSO_BUILD_TMP_MOD}

echo "-----"
echo "building corso binary for ${GOOS}-${GOARCH}"
echo "-----"

set -x
docker run --rm \
--mount type=bind,src=${PROJECT_ROOT},dst=${CORSO_BUILD_CONTAINER} \
--mount type=bind,src=${CORSO_BUILD_TMP_CACHE},dst=${CORSO_BUILD_TMP_CACHE} \
--mount type=bind,src=${CORSO_BUILD_TMP_MOD},dst=${CORSO_BUILD_PKG_MOD} \
--workdir ${CORSO_BUILD_CONTAINER_SRC} \
--env GOMODCACHE=${CORSO_MOD_CACHE} \
--env GOCACHE=${CORSO_CACHE} \
--env GOOS=${GOOS} \
--env GOARCH=${GOARCH} \
--entrypoint /usr/local/go/bin/go \
golang:${GOVER} \
build ${CORSO_BUILD_ARGS}
set +x

mkdir -p ${PROJECT_ROOT}/bin
mv ${PROJECT_ROOT}/src/corso ${PROJECT_ROOT}/bin/corso

echo "-----"
echo "created binary image in ${PROJECT_ROOT}/bin/corso"
if [ -z "$platforms" ]; then
platforms="${GOOS}/${GOARCH}"
fi

for platform in ${platforms/,/ }
do
IFS='/' read -r -a platform_split <<< "${platform}"
GOOS=${platform_split[0]}
GOARCH=${platform_split[1]}

echo "-----"
echo "building corso binary for ${GOOS}/${GOARCH}"
echo "-----"

set -x
docker run --rm \
--mount type=bind,src=${PROJECT_ROOT},dst=${CORSO_BUILD_CONTAINER} \
--mount type=bind,src=${CORSO_BUILD_TMP_CACHE},dst=${CORSO_BUILD_TMP_CACHE} \
--mount type=bind,src=${CORSO_BUILD_TMP_MOD},dst=${CORSO_BUILD_PKG_MOD} \
--workdir ${CORSO_BUILD_CONTAINER_SRC} \
--env GOMODCACHE=${CORSO_MOD_CACHE} \
--env GOCACHE=${CORSO_CACHE} \
--env GOOS=${GOOS} \
--env GOARCH=${GOARCH} \
--entrypoint /usr/local/go/bin/go \
golang:${GOVER} \
build ${CORSO_BUILD_ARGS}
set +x

mkdir -p ${PROJECT_ROOT}/bin/${GOOS}-${GOARCH}
mv ${PROJECT_ROOT}/src/corso ${PROJECT_ROOT}/bin/${GOOS}-${GOARCH}/corso

echo "-----"
echo "created binary image in ${PROJECT_ROOT}/bin/${GOOS}-${GOARCH}/corso"
echo "-----"
done
56 changes: 56 additions & 0 deletions build/multiplatform-binary.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/bin/bash

set -e

SCRIPT_ROOT=$(dirname $(readlink -f $0))
PROJECT_ROOT=$(dirname ${SCRIPT_ROOT})

platforms=
GOVER=1.18
GOOS=linux
GOARCH=amd64

while [ "$#" -gt 0 ]
do
case "$1" in
--platforms)
platforms=$2
shift
;;
esac
shift
done

CORSO_BUILD_ARGS="$@"

if [ -z "$platforms" ]; then
platforms="${GOOS}/${GOARCH}"
fi

for platform in ${platforms/,/ }
do
IFS='/' read -r -a platform_split <<< "${platform}"
GOOS=${platform_split[0]}
GOARCH=${platform_split[1]}

echo "-----"
echo "building corso binary for ${GOOS}/${GOARCH}"
echo "-----"

OS_ARCH_DIR=${PROJECT_ROOT}/bin/${GOOS}-${GOARCH}

set -x

mkdir -p ${OS_ARCH_DIR}

cd ${PROJECT_ROOT}/src; \
GOOS=${GOOS} \
GOARCH=${GOARCH} \
go build -o ${OS_ARCH_DIR} "$CORSO_BUILD_ARGS"

set +x

echo "-----"
echo "created binary ${PROJECT_ROOT}/bin/${GOOS}-${GOARCH}/corso"
echo "-----"
done
Loading