From 746cfe6beeb4a5b8122b02d92e5a029c62238cb4 Mon Sep 17 00:00:00 2001 From: bamen Date: Tue, 27 Aug 2024 14:35:44 +0200 Subject: [PATCH] CI/CD: Remove build_dev workflow and optimize Dockerfile - Comment out entire build_dev.yml workflow - Refactor Dockerfile for multi-arch support and local builds - Optimize image size and streamline container setup --- .github/workflows/build_dev.yml | 78 ++++++++++++++++----------------- Dockerfile | 19 ++++---- buildpush-dev.sh | 38 ++++++++++++++++ 3 files changed, 88 insertions(+), 47 deletions(-) create mode 100755 buildpush-dev.sh diff --git a/.github/workflows/build_dev.yml b/.github/workflows/build_dev.yml index c77e558..0a8c17f 100644 --- a/.github/workflows/build_dev.yml +++ b/.github/workflows/build_dev.yml @@ -1,39 +1,39 @@ -name: Build and Test Gordon - -on: - push: - branches: - - "dev" - -jobs: - build-and-test: - name: Build and Test - runs-on: ubuntu-latest - permissions: - contents: read - packages: write - env: - KO_DOCKER_REPO: ghcr.io/${{ github.repository }} - - steps: - - uses: actions/setup-go@v4 - with: - go-version: "1.23" - - - uses: actions/checkout@v3 - - # - name: Run tests - # run: go test ./... - - - name: Log in to GitHub Container Registry - uses: docker/login-action@v2 - with: - registry: ghcr.io - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} - - - uses: ko-build/setup-ko@v0.6 - - - name: Build and push with ko - run: | - ko build --platform=linux/amd64,linux/arm64 --bare --tags=dev -B ./cmd/cli --base-import-paths=false --image-refs=gordon-dev +# name: Build and Test Gordon + +# on: +# push: +# branches: +# - "dev" + +# jobs: +# build-and-test: +# name: Build and Test +# runs-on: ubuntu-latest +# permissions: +# contents: read +# packages: write +# env: +# KO_DOCKER_REPO: ghcr.io/${{ github.repository }} + +# steps: +# - uses: actions/setup-go@v4 +# with: +# go-version: "1.23" + +# - uses: actions/checkout@v3 + +# # - name: Run tests +# # run: go test ./... + +# - name: Log in to GitHub Container Registry +# uses: docker/login-action@v2 +# with: +# registry: ghcr.io +# username: ${{ github.actor }} +# password: ${{ secrets.GITHUB_TOKEN }} + +# - uses: ko-build/setup-ko@v0.6 + +# - name: Build and push with ko +# run: | +# ko build --platform=linux/amd64,linux/arm64 --bare --tags=dev -B ./cmd/cli --base-import-paths=false --image-refs=gordon-dev diff --git a/Dockerfile b/Dockerfile index d6b0862..6a0e168 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,12 +1,15 @@ -# Use a minimal base image -FROM alpine -# Install ca-certificates bundle -RUN apk update && apk add ca-certificates && rm -rf /var/cache/apk/* -# Copy the binary -COPY gordon /gordon -# Copy other necessary files -COPY .iscontainer / +# Start from scratch +FROM alpine:latest + +ARG ARCH + +# Copy the pre-built binary for the specific architecture +COPY dist/gordon-linux-${ARCH} /gordon +# Create the .iscontainer file +RUN touch /.iscontainer + # Set the entrypoint ENTRYPOINT ["/gordon"] + # Default command CMD ["serve"] diff --git a/buildpush-dev.sh b/buildpush-dev.sh new file mode 100755 index 0000000..77b3d9d --- /dev/null +++ b/buildpush-dev.sh @@ -0,0 +1,38 @@ +#!/bin/bash + +set -e + +REPO="ghcr.io/bnema/gordon" +TAG="dev" +DIST_DIR="./dist" + +# Ensure dist directory exists +mkdir -p $DIST_DIR + +# Build Go binaries for multiple platforms with CGO_ENABLED=0 +echo "Building Go binaries..." +CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o $DIST_DIR/gordon-linux-amd64 ./cmd/cli +CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o $DIST_DIR/gordon-linux-arm64 ./cmd/cli + +# Build Docker images for each architecture +echo "Building Docker images..." +docker build -t $REPO:${TAG}-amd64 --build-arg ARCH=amd64 -f Dockerfile . +docker build -t $REPO:${TAG}-arm64v8 --build-arg ARCH=arm64 -f Dockerfile . + +# Push images +echo "Pushing Docker images..." +docker push $REPO:${TAG}-amd64 +docker push $REPO:${TAG}-arm64v8 + +# Create and push multi-arch manifest +echo "Creating and pushing multi-arch manifest..." +docker manifest create $REPO:$TAG \ + $REPO:${TAG}-amd64 \ + $REPO:${TAG}-arm64v8 \ + --amend + +# Annotate the arm64 image with variant information +docker manifest annotate $REPO:$TAG \ + $REPO:${TAG}-arm64v8 --arch arm64 --variant v8 + +docker manifest push $REPO:$TAG