Skip to content

Commit

Permalink
Deterministic builds using Docker (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Mar 27, 2024
1 parent 9dfa0b4 commit c111216
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ dist/

# environment variables
.env

# canister wasms
wasms/
31 changes: 31 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# To build run 'docker build . -t openchat'
FROM ubuntu:22.04 as builder
SHELL ["bash", "-c"]

ARG git_commit_id
ARG rust_version=1.76.0

ENV GIT_COMMIT_ID=$git_commit_id
ENV TZ=UTC

RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone && \
apt -yq update && \
apt -yqq install --no-install-recommends curl ca-certificates build-essential

# Install Rust and Cargo in /opt
ENV RUSTUP_HOME=/opt/rustup \
CARGO_HOME=/opt/cargo \
PATH=/cargo/bin:/opt/cargo/bin:$PATH

RUN curl --fail https://sh.rustup.rs -sSf \
| sh -s -- -y --default-toolchain ${rust_version}-x86_64-unknown-linux-gnu --no-modify-path && \
rustup default ${rust_version}-x86_64-unknown-linux-gnu && \
rustup target add wasm32-unknown-unknown

# Install IC Wasm
RUN cargo install --version 0.7.1 ic-wasm

COPY . /build
WORKDIR /build

RUN sh ./scripts/build-wasm.sh
5 changes: 3 additions & 2 deletions dfx.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"canisters": {
"event_store": {
"type": "custom",
"candid": "rs/canister/api/can.did",
"package": "event_store_canister_impl",
"type": "rust"
"wasm": "wasms/event_store.wasm.gz",
"build": "scripts/build-wasm.sh"
}
},
"version": 1
Expand Down
37 changes: 37 additions & 0 deletions scripts/build-wasm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash

SCRIPT=$(readlink -f "$0")
SCRIPT_DIR=$(dirname "$SCRIPT")
cd $SCRIPT_DIR/..

CANISTER_NAME="event_store"
PACKAGE="${CANISTER_NAME}_canister_impl"

if [ -z "${CARGO_HOME}" ]
then
export CARGO_HOME="${HOME}/.cargo"
fi

if [ -z "${GIT_COMMIT_ID}" ]
then
export GIT_COMMIT_ID=$(git rev-parse HEAD)
fi

echo Building package $PACKAGE
export RUSTFLAGS="--remap-path-prefix $(readlink -f ${SCRIPT_DIR}/..)=/build --remap-path-prefix ${CARGO_HOME}/bin=/cargo/bin --remap-path-prefix ${CARGO_HOME}/git=/cargo/git"
for l in $(ls ${CARGO_HOME}/registry/src/)
do
export RUSTFLAGS="--remap-path-prefix ${CARGO_HOME}/registry/src/${l}=/cargo/registry/src/github ${RUSTFLAGS}"
done
cargo build --locked --target wasm32-unknown-unknown --release --package $PACKAGE

echo Optimising wasm
if ! cargo install --list | grep -Fxq "ic-wasm v0.7.1:"
then
cargo install --version 0.7.1 ic-wasm
fi
ic-wasm ./target/wasm32-unknown-unknown/release/$PACKAGE.wasm -o ./target/wasm32-unknown-unknown/release/$PACKAGE-opt.wasm shrink

echo Compressing wasm
mkdir -p wasms
gzip -fckn target/wasm32-unknown-unknown/release/$PACKAGE-opt.wasm > ./wasms/$CANISTER_NAME.wasm.gz
22 changes: 22 additions & 0 deletions scripts/docker-build-wasm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

SCRIPT=$(readlink -f "$0")
SCRIPT_DIR=$(dirname "$SCRIPT")
cd $SCRIPT_DIR/..

GIT_COMMIT_ID=$(git rev-parse HEAD)

echo "CommitId: $GIT_COMMIT_ID"

docker build -t event_store --build-arg git_commit_id=$GIT_COMMIT_ID .

container_id=$(docker create event_store)
rm -rf wasms
docker cp $container_id:/build/wasms wasms
docker rm --volumes $container_id

cd wasms
for wasm in *; do
shasum -a 256 "$wasm"
done
cd ..

0 comments on commit c111216

Please sign in to comment.