diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 128b05a..a2d85f5 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -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! - - uses: actions/setup-go@v5 + - name: Setup Go + uses: actions/setup-go@v5 with: go-version: "~1.22" diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index ee2e0fe..eb8c1e2 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -19,21 +19,11 @@ 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 + - name: Setup Go + uses: actions/setup-go@v5 with: go-version: "~1.22" @@ -41,9 +31,6 @@ jobs: 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: @@ -51,15 +38,10 @@ jobs: 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 + - name: Build and Push Docker Image + run: ./scripts/build.sh + env: + CI: true - name: Authenticate to Google Cloud uses: google-github-actions/auth@v2 diff --git a/.gitignore b/.gitignore index d3a526c..5278fea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -coder-logstream-kube +coder-logstream-kube-* build \ No newline at end of file diff --git a/scripts/Dockerfile b/scripts/Dockerfile index 3706754..fe869e7 100644 --- a/scripts/Dockerfile +++ b/scripts/Dockerfile @@ -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 ./coder-logstream-kube-${TARGETARCH} /coder-logstream-kube +ENTRYPOINT ["/coder-logstream-kube"] \ No newline at end of file diff --git a/scripts/build.sh b/scripts/build.sh index 1d70e46..a95a878 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -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