Skip to content

feat: build and publish multiarch image #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 30, 2024
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
16 changes: 2 additions & 14 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,8 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- name: Echo Go Cache Paths
id: go-cache-paths
run: |
echo "GOCACHE=$(go env GOCACHE)" >> ${{ runner.os == 'Windows' && '$env:' || '$' }}GITHUB_OUTPUT
echo "GOMODCACHE=$(go env GOMODCACHE)" >> ${{ runner.os == 'Windows' && '$env:' || '$' }}GITHUB_OUTPUT

- name: Go Build Cache
uses: actions/cache@v3
with:
path: ${{ steps.go-cache-paths.outputs.GOCACHE }}
key: ${{ runner.os }}-go-build-${{ hashFiles('**/go.**', '**.go') }}

# Install Go!
Comment on lines -33 to -45
Copy link
Member Author

Choose a reason for hiding this comment

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

setup-go has a built-in cache that works as expected.

- uses: actions/setup-go@v5
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: "~1.22"

Expand Down
34 changes: 8 additions & 26 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,47 +19,29 @@ jobs:
name: Build and publish
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Checkout
uses: actions/checkout@v4

- name: Echo Go Cache Paths
id: go-cache-paths
run: |
echo "GOCACHE=$(go env GOCACHE)" >> ${{ runner.os == 'Windows' && '$env:' || '$' }}GITHUB_OUTPUT
echo "GOMODCACHE=$(go env GOMODCACHE)" >> ${{ runner.os == 'Windows' && '$env:' || '$' }}GITHUB_OUTPUT

- name: Go Build Cache
uses: actions/cache@v3
with:
path: ${{ steps.go-cache-paths.outputs.GOCACHE }}
key: ${{ runner.os }}-go-build-${{ hashFiles('**/go.**', '**.go') }}

- uses: actions/setup-go@v5
Comment on lines -24 to -36
Copy link
Member Author

Choose a reason for hiding this comment

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

setup-go has a built-in cache that works as expected.

- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: "~1.22"

- name: Get Version
run: echo "version=$(./scripts/version.sh)" >> $GITHUB_OUTPUT
id: version

- name: Build
run: ./scripts/build.sh

- name: Docker Login
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Push Image
run: |
VERSION=$(./scripts/version.sh)
BASE=ghcr.io/coder/coder-logstream-kube
IMAGE=$BASE:$VERSION
docker tag coder-logstream-kube:latest $IMAGE
docker tag coder-logstream-kube:latest $BASE:latest
docker push $IMAGE
docker push $BASE:latest
Comment on lines -54 to -62
Copy link
Member Author

Choose a reason for hiding this comment

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

moved pushing and building to the ./scripts/build.sh

- name: Build and Push Docker Image
run: ./scripts/build.sh
env:
CI: true

- name: Authenticate to Google Cloud
uses: google-github-actions/auth@v2
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
coder-logstream-kube
coder-logstream-kube-*
build
9 changes: 4 additions & 5 deletions scripts/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
FROM scratch

COPY ./coder-logstream-kube /coder-logstream-kube

ENTRYPOINT ["/coder-logstream-kube"]
FROM --platform=$BUILDPLATFORM scratch AS base
ARG TARGETARCH
Copy link
Member

Choose a reason for hiding this comment

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

COPY ./coder-logstream-kube-${TARGETARCH} /coder-logstream-kube
ENTRYPOINT ["/coder-logstream-kube"]
43 changes: 40 additions & 3 deletions scripts/build.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,44 @@
#!/usr/bin/env bash

cd $(dirname "${BASH_SOURCE[0]}")
cd "$(dirname "${BASH_SOURCE[0]}")"
set -euxo pipefail

CGO_ENABLED=0 go build -ldflags "-s -w" -o ./coder-logstream-kube ../
docker build -t coder-logstream-kube:latest .
# Set CI to false if not set
CI=${CI:-false}

# Get current architecture
current=$(go env GOARCH)
# Arhcitectures to build for
archs=(amd64 arm64 arm)

# build for all architectures
for arch in "${archs[@]}"; do
echo "Building for $arch"
GOARCH=$arch GOOS=linux CGO_ENABLED=0 go build -ldflags "-s -w" -o ./coder-logstream-kube-"$arch" ../
done

# We have to use docker buildx to tag multiple images with
# platforms tragically, so we have to create a builder.
BUILDER_NAME="coder-logstream-kube"
BUILDER_EXISTS=$(docker buildx ls | grep $BUILDER_NAME || true)

# If builder doesn't exist, create it
if [ -z "$BUILDER_EXISTS" ]; then
echo "Creating dockerx builder $BUILDER_NAME..."
docker buildx create --use --platform=linux/arm64,linux/amd64,linux/arm/v7 --name $BUILDER_NAME
else
echo "Builder $BUILDER_NAME already exists. Using it."
fi

# Ensure the builder is bootstrapped and ready to use
docker buildx inspect --bootstrap &>/dev/null

# Build
if [ "$CI" = "false" ]; then
docker buildx build --platform linux/"$current" -t coder-logstream-kube --load .
else
VERSION=$(../scripts/version.sh)
BASE=ghcr.io/coder/coder-logstream-kube
IMAGE=$BASE:$VERSION
docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t "$IMAGE" -t $BASE:latest --push.
fi