Skip to content

Commit

Permalink
Use unified bootrap script and action (#688)
Browse files Browse the repository at this point in the history
* Use unified bootrap script and action

This adds a new bootstrap script and boostrap action, which calls the
script. The script sets up rustup and ic-cdk-optimizer, and the action
caches the ic-cdk-optimizer install.

This has various direct and indirect benefits:

* Dependencies can be installed and checked from a single place (see
  also https://github.com/github/scripts-to-rule-them-all)
* The rust version is sourced from a single source of truth,
  `rust-toolchain.toml`, which itself has many benefits:
  * The rust version can be updated programatically
  * The build description now lives entirely in the actual codebase and
    not anymore in GHA workflow description files, meaning we can
    implement scheduled clean builds to check against the latest release
    without having potentially updated workflow descriptions interfering
* GHA jobs can use the bootstrap action which DRYs the code; note that
  the action also caches ic-cdk-optimizer, which transparently
  reduces CI time from e.g. clean builds from 12mn to 5mn

* Remove uname action
  • Loading branch information
nmattia authored Jun 17, 2022
1 parent 9e84f12 commit 39cb10c
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 65 deletions.
27 changes: 27 additions & 0 deletions .github/actions/bootstrap/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: 'bootstrap'
description: Bootstrap the Internet Identity build environment
runs:
using: "composite"
steps:
- id: uname
shell: bash
run: |
# print include system info relevant for cache compatibility
uname="$(uname -mpsr)"
echo "uname value: $uname"
uname_hash="$(echo "$uname" | shasum -a 256 | head -c 10)"
echo "uname hash value: $uname_hash"
echo "::set-output name=uname-hash::$uname_hash"
# cache ic-cdk-optimizer
# NOTE: here we make sure to only cache ic-cdk-optimizer and _not_ e.g. the cargo target, since in some cases
# (clean builds) we build from scratch
# NOTE: we include the output of uname to ensure binary compatibility (e.g. same glibc)
- uses: actions/cache@v2
with:
path: ~/.cargo/bin
key: cargo-bin-${{ hashFiles('**/Cargo.lock', 'rust-toolchain.toml') }}-${{ steps.uname.outputs.uname-hash }}

- name: Bootstrap
shell: bash
run: ./scripts/bootstrap
25 changes: 2 additions & 23 deletions .github/actions/check-build/action.yml
Original file line number Diff line number Diff line change
@@ -1,34 +1,13 @@
name: 'Check build'
description: This action performs a clean, non-Docker build of II, and optionally checks the Wasm module sha256 against the 'sha256' argument. Nothing is cached except for ic-cdk-optimizer.
description: This action performs a clean, non-Docker build of II, and optionally checks the Wasm module sha256 against the 'sha256' argument. Nothing is cached except for the bootstrap environment.
inputs:
sha256:
description: The expected sha256 of the final Wasm module
required: false
rust-version:
description: The rust version
required: true
ic-cdk-optimizer-version:
description: The ic-cdk-optimizer version
required: true
runs:
using: "composite"
steps:
- name: Install Rust
shell: bash
run: |
rustup update "${{ inputs.rust-version }}" --no-self-update
rustup default "${{ inputs.rust-version }}"
rustup target add wasm32-unknown-unknown
- run: |
if [ ! -e "$HOME/.cargo/bin/ic-cdk-optimizer" ]
then
echo "installing cdk optimizer"
cargo install ic-cdk-optimizer --version "${{ inputs.ic-cdk-optimizer-version }}"
else
echo "NOT installing cdk optimizer"
fi
shell: bash
- uses: ./.github/actions/bootstrap

# run the build
- run: npm ci
Expand Down
12 changes: 2 additions & 10 deletions .github/workflows/canister-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ jobs:
strategy:
matrix:
os: [ ubuntu-latest, macos-latest ]
env:
RUSTC_VERSION: 1.58.1
steps:
- uses: actions/checkout@v2

Expand All @@ -149,13 +147,9 @@ jobs:
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ env.RUSTC_VERSION }}-${{ hashFiles('**/Cargo.lock') }}-tests-1
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock', 'rust-toolchain.toml') }}

- name: Install Rust
run: |
rustup update "$RUSTC_VERSION" --no-self-update
rustup default "$RUSTC_VERSION"
rustup target add wasm32-unknown-unknown
- uses: ./.github/actions/bootstrap

- name: Create fake assets
run : |
Expand Down Expand Up @@ -558,5 +552,3 @@ jobs:
with:
# we check that ubuntu builds match the docker build
sha256: ${{ startsWith(matrix.os, 'ubuntu') && steps.sha256.outputs.sha256 || '' }}
rust-version: 1.58.1
ic-cdk-optimizer-version: 0.3.1
20 changes: 2 additions & 18 deletions .github/workflows/cargo-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,13 @@ on:
jobs:
cargo-fmt:
runs-on: ubuntu-latest
env:
RUSTC_VERSION: 1.58.1

steps:
- uses: actions/checkout@v2

- uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}-1

- name: Install Rust
run: |
rustup update "$RUSTC_VERSION" --no-self-update
rustup default "$RUSTC_VERSION"
rustup target add wasm32-unknown-unknown
rustup component add rustfmt
- uses: ./.github/actions/bootstrap

- name: Cargo fmt
run: |
rustup component add rustfmt
cargo fmt
- name: Commit Formatting changes
uses: EndBug/add-and-commit@v9
Expand Down
19 changes: 5 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,13 @@ RUN npm --version

# Install Rust and Cargo in /opt
ENV RUSTUP_HOME=/opt/rustup \
CARGO_HOME=/opt/cargo \
PATH=/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

ENV CARGO_HOME=/cargo \
CARGO_TARGET_DIR=/cargo_target \
CARGO_HOME=/cargo \
PATH=/cargo/bin:$PATH

# Install IC CDK optimizer
# (keep version in sync with scripts/build)
RUN cargo install ic-cdk-optimizer --version 0.3.1

COPY ./scripts ./scripts
COPY ./rust-toolchain.toml ./rust-toolchain.toml

RUN ./scripts/bootstrap

# Pre-build all cargo dependencies. Because cargo doesn't have a build option
# to build only the dependecies, we pretend that our project is a simple, empty
Expand All @@ -56,6 +46,7 @@ COPY Cargo.toml .
COPY src/internet_identity/Cargo.toml src/internet_identity/Cargo.toml
COPY src/internet_identity_interface/Cargo.toml src/internet_identity_interface/Cargo.toml
COPY src/canister_tests/Cargo.toml src/canister_tests/Cargo.toml
ENV CARGO_TARGET_DIR=/cargo_target
RUN mkdir -p src/internet_identity/src \
&& touch src/internet_identity/src/lib.rs \
&& mkdir -p src/internet_identity_interface/src \
Expand Down
30 changes: 30 additions & 0 deletions scripts/bootstrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash
# install build dependencies (rustup + ic-cdk-optimizer)

set -euo pipefail

SCRIPTS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$SCRIPTS_DIR/.."

function run() {
1>&2 echo "running $@"
rc=0 && "$@" || rc="$?"
if ! [ "$rc" -eq 0 ]
then
1>&2 echo "Bootstrap command failed: $@"
exit "$rc"
fi
}

rust_version=$(cat ./rust-toolchain.toml | sed -n 's/^channel[[:space:]]*=[[:space:]]"\(.*\)"/\1/p')
echo "using rust version '$rust_version'"

# here we set the toolchain to 'none' and rustup will pick up on ./rust-toolchain.toml
run curl --fail https://sh.rustup.rs -sSf | run sh -s -- -y --default-toolchain "none" --no-modify-path

echo "looking for ic-cdk-optimizer"
if ! command -v ic-cdk-optimizer
then
echo "installing ic-cdk-optimizer"
run cargo install ic-cdk-optimizer --version 0.3.1
fi

0 comments on commit 39cb10c

Please sign in to comment.