Skip to content

Commit

Permalink
inject sccache in the docker image
Browse files Browse the repository at this point in the history
  • Loading branch information
gpmayorga committed Sep 16, 2023
1 parent 9fddac4 commit 9ca1e9a
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 26 deletions.
7 changes: 7 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
**/target/
.github/
scripts/
docker-compos
.gitignore
docker
README.md
14 changes: 13 additions & 1 deletion .github/workflows/build-docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,25 @@ jobs:
type=semver,pattern={{major}}
type=edge
- name: Configure sccache
uses: actions/github-script@v6
with:
script: |
core.exportVariable('ACTIONS_CACHE_URL', process.env.ACTIONS_CACHE_URL || '');
core.exportVariable('ACTIONS_RUNTIME_TOKEN', process.env.ACTIONS_RUNTIME_TOKEN || '');
- name: Build and push centrifugeio/centrifuge-chain
uses: docker/build-push-action@v5
env:
FEATURES: ${{ matrix.target == 'test' && '"fast-runtime"' || '' }}
BUILDKIT_PROGRESS: plain
DOCKER_BUILDKIT: 1
with:
context: .
file: ./docker/centrifuge-chain/Dockerfile
build-args: |
FEATURES=${{ matrix.target == 'test' && '"fast-runtime"' || '' }}
SCCACHE_GHA_ENABLED="true"
RUSTC_WRAPPER=sccache
push: ${{ github.ref == '/refs/heads/main' && true || false }}
tags: ${{ steps.meta.outputs.tags }}
# Cache options:
Expand Down
45 changes: 45 additions & 0 deletions ci/install-sccache-tarball.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/bash
# Used by centrifuge Linux Docker image docker/centrifuge-chain/Dockerfile
set -eux
# Define URLs and file names
URL="https://github.com/mozilla/sccache/releases/download/v0.5.4/"
TARBALL_URL="${URL}/sccache-v0.5.4-aarch64-unknown-linux-musl.tar.gz"
CHECKSUM_URL="${URL}/sccache-v0.5.4-aarch64-unknown-linux-musl.tar.gz.sha256"
TARBALL_FILENAME="sccache.tar.gz"
CHECKSUM_FILENAME="sccache.sha256"

# Define the target directory where you want to extract the binary
TARGET_DIR="/usr/local/cargo/bin"

# Download the tarball and checksum
echo "Downloading tarball..."
curl -L "$TARBALL_URL" -o "$TARBALL_FILENAME"

echo "Downloading checksum..."
curl -L "$CHECKSUM_URL" -o "$CHECKSUM_FILENAME"

# Verify the checksum
echo "Verifying checksum..."
EXPECTED_SHA256=$(cat "$CHECKSUM_FILENAME" | awk '{print $1}')
ACTUAL_SHA256=$(sha256sum "$TARBALL_FILENAME" | awk '{print $1}')

if [ "$ACTUAL_SHA256" != "$EXPECTED_SHA256" ]; then
echo "Checksum verification failed. Aborting."
rm "$TARBALL_FILENAME" "$CHECKSUM_FILENAME"
exit 1
fi

# Extract the tarball
echo "Extracting tarball..."
mkdir sccache
tar -vxzf sccache.tar.gz -C ./sccache/ --strip-components 1

# Copy the sccache binary to the target directory
echo "Copying sccache binary to $TARGET_DIR"
cp "sccache/sccache" "$TARGET_DIR/"

# Clean up downloaded files and extracted folder
rm "$TARBALL_FILENAME" "$CHECKSUM_FILENAME"
rm -rf "sccache"

echo "Installation completed successfully."
5 changes: 0 additions & 5 deletions docker/centrifuge-chain/.dockerignore

This file was deleted.

55 changes: 35 additions & 20 deletions docker/centrifuge-chain/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
# https://github.com/paritytech/polkadot-sdk/blob/master/docker/dockerfiles/polkadot/polkadot_injected_release.Dockerfile

# ToDo: create a CI/builder image with preloaded tools
FROM docker.io/library/ubuntu:22.04 as builder
FROM --platform=linux/amd64 docker.io/library/ubuntu:22.04 as builder

# Defaults
ENV RUST_BACKTRACE 1
ENV DEBIAN_FRONTEND=noninteractive
ARG FEATURES=""
ARG RUST_TOOLCHAIN="1.66"

RUN apt-get update && \
# apt-get dist-upgrade -y -o Dpkg::Options::="--force-confold" && \
Expand All @@ -21,26 +19,40 @@ FROM docker.io/library/ubuntu:22.04 as builder
libclang-dev \
protobuf-compiler \
curl

RUN curl https://sh.rustup.rs -sSf | sh -s -- -y
ENV PATH="${PATH}:/root/.cargo/bin"


ENV RUSTUP_HOME=/usr/local/rustup \
CARGO_HOME=/usr/local/cargo \
PATH=/usr/local/cargo/bin:$PATH
# install rustup, use minimum components
RUN curl -L "https://static.rust-lang.org/rustup/dist/x86_64-unknown-linux-gnu/rustup-init" \
-o rustup-init; \
chmod +x rustup-init; \
./rustup-init -y --no-modify-path --profile minimal --default-toolchain stable; \
rm rustup-init;
# install sccache
COPY ./ci/install-sccache-tarball.sh ./
RUN chmod +x install-sccache-tarball.sh && \
./install-sccache-tarball.sh

# BUILD
COPY . centrifuge-chain
WORKDIR /centrifuge-chain
RUN echo $(ls -l /centrifuge-chain/)
RUN \
rustup-init -y --profile minimal --default-toolchain stable; \
cargo install sccache;

ARG FEATURES=""
ARG RUST_TOOLCHAIN="1.66"
ARG RUSTC_WRAPPER=sccache
ARG SCCACHE_GHA_ENABLED="false"

RUN rustup default $RUST_TOOLCHAIN && \
rustup target add wasm32-unknown-unknown --toolchain $RUST_TOOLCHAIN

# RUN cargo build "--release" --features=${FEATURES}

RUN echo $(sccache --show-stats)
COPY . centrifuge-chain
WORKDIR /centrifuge-chain
RUN --mount=type=cache,target=/root/.cache/sccache \
cargo build "--release" --features=${FEATURES}
RUN sccache --show-stats

# ===== SECOND STAGE ======
# ToDo: create a secure image as a base for the binary
FROM docker.io/library/ubuntu:22.04
FROM --platform=linux/amd64 docker.io/library/ubuntu:22.04

LABEL io.centrifuge.image.authors="[email protected]" \
io.centrifuge.image.vendor="Centrifuge" \
Expand All @@ -67,13 +79,16 @@ FROM docker.io/library/ubuntu:22.04
# RUN mv /usr/share/ca* /tmp && \
# rm -rf /usr/share/* && \
# mv /tmp/ca-certificates /usr/share/

# minimize the attack surface
# rm -rf /usr/bin /usr/sbin /usr/lib/python* && \
# RUN rm -rf /usr/bin /usr/sbin /usr/lib/python*

RUN apt-get autoremove -y && apt-get clean && rm -rf /var/lib/apt/lists/* ;
RUN mkdir -p /root/.local/share/centrifuge-chain && \
ln -s /root/.local/share/centrifuge-chain /data



ENV RUST_BACKTRACE 1
USER centrifuge
EXPOSE 30333 9933 9944
VOLUME ["/data"]
Expand Down

0 comments on commit 9ca1e9a

Please sign in to comment.