Skip to content

Commit

Permalink
Improve build-image incremental workflow
Browse files Browse the repository at this point in the history
Several improvements to workflows that flow through the build-image to
improve build and iteration times.

This includes:

We don't need this anymore, so no reason to build it for each
architecture.

I realised that when mixing the `build-image` workflow with local rust
tools, that going between them, they would invalidate each other's
incremental cache, since it was fingerprinted on absolute directories -
drastically expanding build times between iterations.

This drastically improves both the incremental build times of images,
and every other workflow through `build-image`, such as e2e testing.
(`make test-agones` can be used iteratively now).

We only use a few files for the Docker image, so let's ignore everything
else. Took the docker context down from 1GB+ to < 50MB.

On a linux machine all this brings incremental builds down from 10's of
minutes to a minute or less, since only what has changed is compiled and
pushed as we can now take advantage of rust incremental build and
docker image caching.

Work on #553
  • Loading branch information
markmandel committed Aug 9, 2022
1 parent b40ba42 commit 9ef62a4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 46 deletions.
11 changes: 2 additions & 9 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,12 @@
/sdks
/src
/tests
target/debug
target/release
target/**.d
target/*/*/build
target/*/*/deps
target/*/*/incremental
target/criterion
target/doc
target/tmp
/target

# windows files
*.exe
*.rlib

!target/build-image/x86_64-unknown-linux-gnu/release/quilkin
!dependencies-src.zip
!image/quilkin.yaml
32 changes: 14 additions & 18 deletions build/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ IMAGE_TAG ?= ${REPOSITORY}quilkin:$(package_version)
MINIKUBE_PROFILE ?= quilkin

common_rust_args := -v $(project_path):/workspace -w /workspace \
-v $(CARGO_HOME)/registry:/usr/local/cargo/registry
-v $(CARGO_HOME)/registry:/usr/local/cargo/registry \
-e "CARGO_TARGET_DIR=/workspace/target/build-image"

KUBECONFIG ?= ~/.kube/config
kubeconfig_path := $(dir $(KUBECONFIG))
Expand Down Expand Up @@ -101,37 +102,34 @@ test-docs: ensure-build-image
# Build all binaries, images and related artifacts
build: binary-archive build-image

# Build all debug and release binaries
build-all-binaries: ensure-build-image build-linux-binary build-macos-binary
# Build all binaries
build-all-binaries: ensure-build-image build-linux-binary build-macos-binary build-windows-binary

# Build an archive all debug and release binaries
# Build an archive all binaries
binary-archive: ensure-build-image build-all-binaries
docker run --rm $(common_rust_args) \
--entrypoint=bash $(BUILD_IMAGE_TAG) -c 'zip quilkin-$(package_version).zip ./target/*/*/quilkin ./target/*/*/quilkin.exe'
docker run --rm $(common_rust_args) -w /workspace/target/build-image \
--entrypoint=bash $(BUILD_IMAGE_TAG) -c 'zip ../../quilkin-$(package_version).zip ./*/*/quilkin ./*/*/quilkin.exe'

# Build release and debug binaries for x86_64-unknown-linux-gnu
# Build binary for x86_64-unknown-linux-gnu
build-linux-binary: ensure-build-image
docker run --rm $(common_rust_args) \
--entrypoint=cargo $(BUILD_IMAGE_TAG) build --target x86_64-unknown-linux-gnu
docker run --rm $(common_rust_args) \
--entrypoint=cargo $(BUILD_IMAGE_TAG) build --target x86_64-unknown-linux-gnu --release

# Build release and debug binaries for x86_64-pc-windows-gnu
# Build binary for x86_64-pc-windows-gnu
build-windows-binary: ensure-build-image
docker run --rm $(common_rust_args) \
--entrypoint=cargo $(BUILD_IMAGE_TAG) build --target x86_64-pc-windows-gnu
docker run --rm $(common_rust_args) \
--entrypoint=cargo $(BUILD_IMAGE_TAG) build --target x86_64-pc-windows-gnu --release

# Build release and debug binaries for x86_64-apple-darwin
# Build binary for x86_64-apple-darwin
build-macos-binary:
docker run --rm -v $(project_path):/workspace -w /workspace \
-v $(CARGO_HOME)/registry:/root/.cargo/registry \
-e "CARGO_TARGET_DIR=/workspace/target/build-image" \
-e "CC=o64-clang" -e "CXX=o64-clang++" \
joseluisq/rust-linux-darwin-builder:$(rust_toolchain) \
sh -c "rustup target add x86_64-apple-darwin && cargo build --target x86_64-apple-darwin && cargo build --release --target x86_64-apple-darwin"
sh -c "rustup target add x86_64-apple-darwin && cargo build --release --target x86_64-apple-darwin"

# Build release and debug container images.
# Build container image.
# Use either `REPOSITORY` to specify a container repository (defaults to blank/none), or use `IMAGE_TAG` argument to specify
# the entire image name and tag. Defaults to `quilkin:${version}-${git-sha}`.
build-image: ensure-build-image build-linux-binary
Expand All @@ -140,8 +138,7 @@ build-image:
--entrypoint=bash $(BUILD_IMAGE_TAG) -c 'cargo about generate license.html.hbs > license.html'
docker run --rm $(common_rust_args) \
--entrypoint=bash $(BUILD_IMAGE_TAG) -c './image/archive_dependencies.sh'
docker build -t $(IMAGE_TAG)-debug --build-arg PROFILE=debug -f $(project_path)/image/Dockerfile $(project_path)
docker build -t $(IMAGE_TAG) --build-arg PROFILE=release -f $(project_path)/image/Dockerfile $(project_path)
docker build -t $(IMAGE_TAG) -f $(project_path)/image/Dockerfile $(project_path)

# Builds Quilkin, pushes it to a repository (use REPOSITORY arg to set value)
# and then runs the Agones integration tests. See targets `build-images` and `push` for more options and details.
Expand Down Expand Up @@ -172,7 +169,6 @@ push:
ifndef SKIP_BUILD_IMAGE
push: build-image
endif
docker push $(IMAGE_TAG)-debug
docker push $(IMAGE_TAG)

# Convenience target to build and push quilkin images into a minikube instance
Expand Down
11 changes: 1 addition & 10 deletions cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,7 @@ steps:
args:
- '-c'
- 'timeout --signal=INT --preserve-status 5s docker run --rm ${_REPOSITORY}quilkin:$(make version)'
id: test-quilkin-debug
waitFor:
- build
- name: gcr.io/cloud-builders/docker
dir: ./build
entrypoint: bash
args:
- '-c'
- 'timeout --signal=INT --preserve-status 5s docker run --rm ${_REPOSITORY}quilkin:$(make version)-debug'
id: test-quilkin-release
id: test-quilkin-image
waitFor:
- build
- name: us-docker.pkg.dev/$PROJECT_ID/ci/make-docker
Expand Down
10 changes: 1 addition & 9 deletions image/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

ARG PROFILE

FROM gcr.io/distroless/cc:nonroot as base
WORKDIR /
COPY ./license.html .
COPY ./dependencies-src.zip .
COPY --chown=nonroot:nonroot ./image/quilkin.yaml /etc/quilkin/quilkin.yaml
COPY ./target/build-image/x86_64-unknown-linux-gnu/release/quilkin .

FROM base as release
COPY ./target/x86_64-unknown-linux-gnu/release/quilkin .

FROM base as debug
COPY ./target/x86_64-unknown-linux-gnu/debug/quilkin .

FROM $PROFILE
USER nonroot:nonroot
ENTRYPOINT ["/quilkin", "--config", "/etc/quilkin/quilkin.yaml", "run"]

0 comments on commit 9ef62a4

Please sign in to comment.