Skip to content

Commit

Permalink
Add BUILD_LOCAL to build tools (#565)
Browse files Browse the repository at this point in the history
Sometimes it's just faster and easier to build locally and create images
and binaries that way, rather than use the build image.

This could potentially be extended to running tests as well, but limited
it to building artifacts for now.

Work on #553

Co-authored-by: XAMPPRocky <[email protected]>
  • Loading branch information
markmandel and XAMPPRocky authored Aug 12, 2022
1 parent 27ac6b8 commit 1ede3d9
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@
*.exe
*.rlib

!target/build-image/x86_64-unknown-linux-gnu/release/quilkin
!target/image/quilkin
!dependencies-src.zip
!image/quilkin.yaml
36 changes: 31 additions & 5 deletions build/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ kube_mount_args := -v $(kubeconfig_path):/root/.kube -v $(helm_config):/root/.co
minikube_args := --network=host -v ~/.minikube:$(HOME)/.minikube
gcloud_mount_args := -v $(build_path)/.config/gcloud:/root/.config/gcloud

cargo_build_x86_64_linux := build --release --target x86_64-unknown-linux-gnu
cargo_build_x86_64_apple := build --release --target x86_64-apple-darwin
cargo_build_x86_64_windows := build --release --target x86_64-pc-windows-gnu

# _____ _
# |_ _|_ _ _ __ __ _ ___| |_ ___
# | |/ _` | '__/ _` |/ _ \ __/ __|
Expand Down Expand Up @@ -108,32 +112,54 @@ build-all-binaries: ensure-build-image build-linux-binary build-macos-binary bui
# Build an archive all binaries
binary-archive: ensure-build-image build-all-binaries
docker run --rm $(common_rust_args) -w $(CARGO_TARGET_DIR) \
--entrypoint=bash $(BUILD_IMAGE_TAG) -c 'zip ../../quilkin-$(package_version).zip ./*/*/quilkin ./*/*/quilkin.exe'
--entrypoint=bash $(BUILD_IMAGE_TAG) -c 'zip ../../quilkin-$(package_version).zip ./*/release/quilkin ./*/release/quilkin.exe'

# Build binary for x86_64-unknown-linux-gnu
# Build binary for x86_64-unknown-linux-gnu.
# Use BUILD_LOCAL=1 to build through local cargo rather than through the build container.
build-linux-binary: ensure-build-image
ifdef BUILD_LOCAL
cargo $(cargo_build_x86_64_linux)
else
docker run --rm $(common_rust_args) \
--entrypoint=cargo $(BUILD_IMAGE_TAG) build --target x86_64-unknown-linux-gnu --release
--entrypoint=cargo $(BUILD_IMAGE_TAG) $(cargo_build_x86_64_linux)
endif

# Build binary for x86_64-pc-windows-gnu
# Use BUILD_LOCAL=1 to build through local cargo rather than through the build container.
build-windows-binary: ensure-build-image
ifdef BUILD_LOCAL
cargo $(cargo_build_x86_64_windows)
else
docker run --rm $(common_rust_args) \
--entrypoint=cargo $(BUILD_IMAGE_TAG) build --target x86_64-pc-windows-gnu --release
--entrypoint=cargo $(BUILD_IMAGE_TAG) $(cargo_build_x86_64_windows)
endif

# Build binary for x86_64-apple-darwin
# Use BUILD_LOCAL=1 to build through local cargo rather than through the build container.
build-macos-binary:
ifdef BUILD_LOCAL
cargo $(cargo_build_x86_64_apple)
else
docker run --rm -v $(project_path):/workspace -w /workspace \
-v $(CARGO_HOME)/registry:/root/.cargo/registry \
-e "CARGO_TARGET_DIR=$(CARGO_TARGET_DIR)" \
-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 --release --target x86_64-apple-darwin"
sh -c "rustup target add x86_64-apple-darwin && cargo $(cargo_build_x86_64_apple)"
endif

# 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}`.
# Use BUILD_LOCAL=1 to build the binary through local cargo rather than through the build container.
build-image: ensure-build-image build-linux-binary
build-image:
-mkdir -p "$(project_path)/target/image/"
ifdef BUILD_LOCAL
cp "$(project_path)/target/x86_64-unknown-linux-gnu/release/quilkin" "$(project_path)/target/image/"
else
cp "$(project_path)/target/build-image/x86_64-unknown-linux-gnu/release/quilkin" "$(project_path)/target/image/"
endif
docker run --rm $(common_rust_args) \
--entrypoint=bash $(BUILD_IMAGE_TAG) -c 'cargo about generate license.html.hbs > license.html'
docker run --rm $(common_rust_args) \
Expand Down
21 changes: 21 additions & 0 deletions build/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,27 @@ integration testing tooling.
To build all operating system binaries, as well as the container image for your current development version of
Quilkin, run `make build`.

#### Escape with BUILD_LOCAL

Depending on your OS and setup, running everything through a Docker container may have a performance impact (Hi WSL! 👋).

If you have `cargo` installed locally, you can use the `BUILD_LOCAL` env variable to force the `Make` system use
your local `cargo` configuration rather than build in our Docker image, which can result in a faster iterative
development loop for some platforms.

For example:

```shell
# Builds the quilkin container image through our build image
$ make build-image
```
```shell
# Builds the quilkin binary locally first, before building the Quilkin container image.
$ BUILD_LOCAL=1 make build-image
```

See `make help` for all the targets this applies to.

#### Show all commands

There are more targets available than just the above.
Expand Down
2 changes: 1 addition & 1 deletion image/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ 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 .
COPY ./target/image/quilkin .

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

0 comments on commit 1ede3d9

Please sign in to comment.