Skip to content

Commit

Permalink
CI: Cache prover Docker build
Browse files Browse the repository at this point in the history
Problem: building the prover from scratch takes ages.

Solution: cache the generated Docker image and reuse it for subsequent
builds. This caching solution makes the build closer to a local dev
environment where the prover has already been built once.
  • Loading branch information
odesenfans committed Nov 13, 2023
1 parent 3279c98 commit d72b349
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ jobs:
uses: actions/checkout@v3
with:
submodules: recursive

- name: Install Rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: 1.73
override: true
components: rustfmt, clippy

- name: Set up cargo cache
uses: actions/cache@v3
continue-on-error: false
Expand All @@ -40,9 +42,33 @@ jobs:
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
restore-keys: ${{ runner.os }}-cargo-

- name: Log in to Github container registry
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin

- name: Set cache image environment variables
run: |
# Uppercase characters are not allowed in Docker tags
cache_image=$(echo ghcr.io/${GITHUB_REPOSITORY}/build-cache | tr '[:upper:]' '[:lower:]')
echo "STONE_PROVER_DOCKER_CACHE=$(echo ${cache_image})" >> $GITHUB_ENV
- name: Download Docker cache image (if available)
run: docker pull ghcr.io/$GITHUB_REPOSITORY/build-cache || true

- name: Build
run: cargo build --verbose

- name: Lint with Clippy
run: cargo clippy -- -D warnings

- name: Run tests
run: cargo test --verbose

- name: Push the image to the cache
# It's not possible to push packages from fork PRs.
if: github.event.pull_request.head.repo.full_name == github.repository
run: |
docker tag stone-prover-build:latest ${STONE_PROVER_DOCKER_CACHE}
docker push ${STONE_PROVER_DOCKER_CACHE}
19 changes: 15 additions & 4 deletions stone-prover/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ fn copy_prover_files_from_container(
Ok(())
}

fn make_docker_build_command(repo_dir: &Path, image_name: &str) -> String {
let mut docker_build_command = format!(
"docker build -t {image_name} {}",
repo_dir.to_string_lossy()
);

// Check if a cache image exists. Used by the CI/CD pipeline.
if let Ok(build_cache) = std::env::var("STONE_PROVER_DOCKER_CACHE") {
docker_build_command.push_str(&format!(" --build-from {build_cache}"));
}

docker_build_command
}

/// Build the Stone Prover and copy binaries to `output_dir`.
///
/// The prover repository contains a Dockerfile to build the prover. This function:
Expand All @@ -68,10 +82,7 @@ fn copy_prover_files_from_container(
fn build_stone_prover(repo_dir: &Path, output_dir: &Path) {
// Build the Stone Prover build Docker image
let image_name = "stone-prover-build:latest";
let docker_build_command = format!(
"docker build -t {image_name} {}",
repo_dir.to_string_lossy()
);
let docker_build_command = make_docker_build_command(repo_dir, image_name);
run_command(&docker_build_command).expect("Failed to build Stone Prover using Dockerfile");

// Run a container based on the Docker image
Expand Down

0 comments on commit d72b349

Please sign in to comment.