Skip to content
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

Docker+CI: More efficient build #939

Closed
wants to merge 3 commits into from
Closed
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
21 changes: 17 additions & 4 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ jobs:
strategy:
fail-fast: false
matrix:
platform:
- linux/amd64
- linux/arm64
os:
- linux
arch:
- amd64
- arm64

steps:
- name: Checkout
Expand All @@ -43,6 +45,17 @@ jobs:
images: ${{ env.REGISTRY_IMAGE }}
tags: ${{ env.TAGS_CONFIG }}

# Build executable
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: '1.20'
Comment on lines +49 to +52
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: '1.20'
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: '1.21'

Copy link
Contributor

Choose a reason for hiding this comment

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

The downside of this approach is that there is now a place for specifying the Go version that is not updated by dependabot. Should we perhaps update the Makefile to read the Go version from the Dockerfile or something like that?

- name: Build
run: make static
env:
GOOS: ${{ matrix.os }}
GOARCH: ${{ matrix.arch }}

# Setup buildx
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
Expand Down Expand Up @@ -71,7 +84,7 @@ jobs:
uses: docker/build-push-action@v5
with:
context: .
platforms: ${{ matrix.platform }}
platforms: ${{ matrix.os }}/${{ matrix.arch }}
labels: ${{ steps.meta.outputs.labels }}
outputs: type=image,name=${{ env.REGISTRY_IMAGE }},push-by-digest=true,name-canonical=true,push=${{ github.event_name != 'pull_request' }}

Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/publish-page.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ on:
push:
branches:
- main
paths:
- docs/**
pull_request:
types:
- opened
- reopened
- synchronize
- closed
paths:
- docs/**

permissions:
contents: write
Expand Down
24 changes: 8 additions & 16 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
# download kubectl
FROM golang:1.21.3-alpine as kubectl
RUN apk add --no-cache curl
FROM golang:1.21.3 as kubectl
Copy link
Member

Choose a reason for hiding this comment

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

Since none of these stages are building code any more, you could drop the golang images altogether

RUN export VERSION=$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt) &&\
export OS=$(go env GOOS) && \
export ARCH=$(go env GOARCH) &&\
curl -o /usr/local/bin/kubectl -L https://storage.googleapis.com/kubernetes-release/release/${VERSION}/bin/${OS}/${ARCH}/kubectl &&\
chmod +x /usr/local/bin/kubectl

# build jsonnet-bundler
FROM golang:1.21.3-alpine as jb
WORKDIR /tmp
RUN apk add --no-cache git make bash &&\
git clone https://github.com/jsonnet-bundler/jsonnet-bundler &&\
ls /bin &&\
cd jsonnet-bundler &&\
make static &&\
mv _output/jb /usr/local/bin/jb
FROM golang:1.21.3 as jb
RUN export OS=$(go env GOOS) &&\
export ARCH=$(go env GOARCH) &&\
curl -o /usr/local/bin/jb -L "https://github.com/jsonnet-bundler/jsonnet-bundler/releases/download/v0.5.1/jb-${OS}-${ARCH}" &&\
chmod +x /usr/local/bin/jb

FROM golang:1.21.3-alpine as helm
WORKDIR /tmp/helm
Expand All @@ -39,15 +35,11 @@ RUN export TAG=$(curl --silent "https://api.github.com/repos/kubernetes-sigs/kus
curl -SL "https://github.com/kubernetes-sigs/kustomize/releases/download/${TAG}/kustomize_${VERSION_TAG}_${OS}_${ARCH}.tar.gz" > kustomize.tgz && \
tar -xvf kustomize.tgz

FROM golang:1.21.3 as build
WORKDIR /app
COPY . .
RUN make static
Comment on lines -42 to -45
Copy link
Member

Choose a reason for hiding this comment

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

Be nicer if this could be kept, so a docker build continues to work as expected and isn't a two-step thing where if you forget to run make static you can get an outdated binary in the image.

It should be possible to do this with make, since it can avoid rebuilding the target if the inputs haven't changed. Would require a change in Makefile to list the dependencies (*.go, go.mod and go.sum) but shouldn't be too bad to do.


# assemble final container
FROM alpine:3.18
RUN apk add --no-cache coreutils diffutils less git openssh-client
COPY --from=build /app/tk /usr/local/bin/tk
# Need to `make static` before building the container
COPY tk /usr/local/bin/tk
COPY --from=kubectl /usr/local/bin/kubectl /usr/local/bin/kubectl
COPY --from=jb /usr/local/bin/jb /usr/local/bin/jb
COPY --from=helm /tmp/helm/helm /usr/local/bin/helm
Expand Down
Loading