Skip to content

Commit abc960a

Browse files
committed
feat: add multiarch image and simplify ci
1 parent b9d65a1 commit abc960a

File tree

5 files changed

+55
-49
lines changed

5 files changed

+55
-49
lines changed

.github/workflows/ci.yaml

+2-14
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,8 @@ jobs:
3030
- name: Checkout
3131
uses: actions/checkout@v4
3232

33-
- name: Echo Go Cache Paths
34-
id: go-cache-paths
35-
run: |
36-
echo "GOCACHE=$(go env GOCACHE)" >> ${{ runner.os == 'Windows' && '$env:' || '$' }}GITHUB_OUTPUT
37-
echo "GOMODCACHE=$(go env GOMODCACHE)" >> ${{ runner.os == 'Windows' && '$env:' || '$' }}GITHUB_OUTPUT
38-
39-
- name: Go Build Cache
40-
uses: actions/cache@v3
41-
with:
42-
path: ${{ steps.go-cache-paths.outputs.GOCACHE }}
43-
key: ${{ runner.os }}-go-build-${{ hashFiles('**/go.**', '**.go') }}
44-
45-
# Install Go!
46-
- uses: actions/setup-go@v5
33+
- name: Setup Go
34+
uses: actions/setup-go@v5
4735
with:
4836
go-version: "~1.22"
4937

.github/workflows/release.yaml

+8-26
Original file line numberDiff line numberDiff line change
@@ -19,47 +19,29 @@ jobs:
1919
name: Build and publish
2020
runs-on: ubuntu-latest
2121
steps:
22-
- uses: actions/checkout@v4
22+
- name: Checkout
23+
uses: actions/checkout@v4
2324

24-
- name: Echo Go Cache Paths
25-
id: go-cache-paths
26-
run: |
27-
echo "GOCACHE=$(go env GOCACHE)" >> ${{ runner.os == 'Windows' && '$env:' || '$' }}GITHUB_OUTPUT
28-
echo "GOMODCACHE=$(go env GOMODCACHE)" >> ${{ runner.os == 'Windows' && '$env:' || '$' }}GITHUB_OUTPUT
29-
30-
- name: Go Build Cache
31-
uses: actions/cache@v3
32-
with:
33-
path: ${{ steps.go-cache-paths.outputs.GOCACHE }}
34-
key: ${{ runner.os }}-go-build-${{ hashFiles('**/go.**', '**.go') }}
35-
36-
- uses: actions/setup-go@v5
25+
- name: Setup Go
26+
uses: actions/setup-go@v5
3727
with:
3828
go-version: "~1.22"
3929

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

44-
- name: Build
45-
run: ./scripts/build.sh
46-
4734
- name: Docker Login
4835
uses: docker/login-action@v3
4936
with:
5037
registry: ghcr.io
5138
username: ${{ github.actor }}
5239
password: ${{ secrets.GITHUB_TOKEN }}
5340

54-
- name: Push Image
55-
run: |
56-
VERSION=$(./scripts/version.sh)
57-
BASE=ghcr.io/coder/coder-logstream-kube
58-
IMAGE=$BASE:$VERSION
59-
docker tag coder-logstream-kube:latest $IMAGE
60-
docker tag coder-logstream-kube:latest $BASE:latest
61-
docker push $IMAGE
62-
docker push $BASE:latest
41+
- name: Build and Push Docker Image
42+
run: ./scripts/build.sh
43+
env:
44+
CI: true
6345

6446
- name: Authenticate to Google Cloud
6547
uses: google-github-actions/auth@v2

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
coder-logstream-kube
1+
coder-logstream-kube-*
22
build

scripts/Dockerfile

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
FROM scratch
2-
3-
COPY ./coder-logstream-kube /coder-logstream-kube
4-
5-
ENTRYPOINT ["/coder-logstream-kube"]
1+
FROM --platform=$BUILDPLATFORM scratch AS base
2+
ARG TARGETARCH
3+
COPY ./coder-logstream-kube-${TARGETARCH} /coder-logstream-kube
4+
ENTRYPOINT ["/coder-logstream-kube"]

scripts/build.sh

+40-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,44 @@
11
#!/usr/bin/env bash
22

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

6-
CGO_ENABLED=0 go build -ldflags "-s -w" -o ./coder-logstream-kube ../
7-
docker build -t coder-logstream-kube:latest .
6+
# Set CI to false if not set
7+
CI=${CI:-false}
8+
9+
# Get current architecture
10+
current=$(go env GOARCH)
11+
# Arhcitectures to build for
12+
archs=(amd64 arm64 arm)
13+
14+
# build for all architectures
15+
for arch in "${archs[@]}"; do
16+
echo "Building for $arch"
17+
GOARCH=$arch GOOS=linux CGO_ENABLED=0 go build -ldflags "-s -w" -o ./coder-logstream-kube-"$arch" ../
18+
done
19+
20+
# We have to use docker buildx to tag multiple images with
21+
# platforms tragically, so we have to create a builder.
22+
BUILDER_NAME="coder-logstream-kube"
23+
BUILDER_EXISTS=$(docker buildx ls | grep $BUILDER_NAME || true)
24+
25+
# If builder doesn't exist, create it
26+
if [ -z "$BUILDER_EXISTS" ]; then
27+
echo "Creating dockerx builder $BUILDER_NAME..."
28+
docker buildx create --use --platform=linux/arm64,linux/amd64,linux/arm/v7 --name $BUILDER_NAME
29+
else
30+
echo "Builder $BUILDER_NAME already exists. Using it."
31+
fi
32+
33+
# Ensure the builder is bootstrapped and ready to use
34+
docker buildx inspect --bootstrap &>/dev/null
35+
36+
# Build
37+
if [ "$CI" = "false" ]; then
38+
docker buildx build --platform linux/"$current" -t coder-logstream-kube --load .
39+
else
40+
VERSION=$(../scripts/version.sh)
41+
BASE=ghcr.io/coder/coder-logstream-kube
42+
IMAGE=$BASE:$VERSION
43+
docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t "$IMAGE" -t $BASE:latest --push.
44+
fi

0 commit comments

Comments
 (0)