Skip to content

Commit

Permalink
feat: update ci (kkrt-labs#852)
Browse files Browse the repository at this point in the history
* add Dockerfile.cross and update Dockerfile rpc

* update CI

* update docker files

* checkout + needs

* fix typo

* fix mkdir

* remove all the mkdir

* fix artifacts

* add additional rust c compiler flags for mbdx

* add manifest for cross

* fix artifacts

* remove extra checkout

* fix the paths

* update release CI

* add building for docker image
  • Loading branch information
greged93 authored Mar 13, 2024
1 parent e5c860e commit df05b69
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 129 deletions.
121 changes: 79 additions & 42 deletions .github/workflows/kakarot_rpc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,90 @@ on:

env:
REGISTRY_IMAGE: ghcr.io/${{ github.repository }}/node
DOCKER_USERNAME: ${{ github.actor }}

jobs:
prepare:
runs-on: ubuntu-latest-16-cores
timeout-minutes: 30
steps:
- uses: actions/checkout@v4
# Install Foundry
- name: install foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly
# Install Python
- name: Set up Python 3.9
uses: actions/setup-python@v4
with:
python-version: 3.9
- name: Load cached Poetry installation
id: cached-poetry
uses: actions/cache@v3
with:
path: ~/.local
key: poetry-${{ runner.os }}
- name: Install Poetry
if: steps.cached-poetry.outputs.cache-hit != 'true'
uses: snok/install-poetry@v1
with:
virtualenvs-create: true
virtualenvs-in-project: true
installer-parallel: true
- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v3
with:
path: .venv
key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}
- name: Setup the Kakarot submodule
run: make setup
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: artifacts
path: ./.kakarot
# Inspired by Reth CI.
# <https://github.com/paradigmxyz/reth/blob/027d50fc105fd527dca0bf56fe51b7240f119e66/.github/workflows/docker.yml>
build:
runs-on: ubuntu-latest-16-cores
needs: prepare
strategy:
matrix:
platform:
- linux/amd64
- linux/arm64
include:
- platform: linux/amd64
STARKNET_BIN_DIR: dsrifc349xgnwsv661shqljxb8pbxx2j
SINK_BIN_DIR: vr6f0crii7571q19j122dfqb1ha1m4i9
- platform: linux/arm64
STARKNET_BIN_DIR: f7va4mjqww1kkpp4il6y295dgcwq147v
SINK_BIN_DIR: 3iqnrcirqpg4s7zdy1wdh0dq17jwzmlc
timeout-minutes: 200
- platform: amd64
target: x86_64-unknown-linux-gnu
- platform: arm64
target: aarch64-unknown-linux-gnu
timeout-minutes: 60
steps:
- uses: actions/checkout@v3
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push RPC
uses: docker/build-push-action@v4
with:
push: true
tags: ${{ env.REGISTRY_IMAGE }}:${{ github.ref_name }}, ${{ env.REGISTRY_IMAGE }}:latest
context: .
file: ./docker/rpc/Dockerfile
platforms: linux/amd64,linux/arm64
build-args: GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}
- name: Build and Push Hive
uses: docker/build-push-action@v4
with:
push: true
tags: ghcr.io/${{ github.repository }}/hive:${{ github.ref_name }}, ghcr.io/${{ github.repository }}/hive:latest
context: .
file: ./docker/hive/Dockerfile
platforms: ${{ matrix.platform }}
build-args: |
GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}
APIBARA_STARKNET_BIN_DIR=${{ matrix.STARKNET_BIN_DIR }}
APIBARA_SINK_BIN_DIR=${{ matrix.SINK_BIN_DIR }}
- uses: actions/checkout@v4
- name: Download artifacts
uses: actions/download-artifact@v4
with:
name: artifacts
path: ./.kakarot
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2
with:
cache-on-failure: true
- uses: taiki-e/install-action@cross
- name: Log in to Docker
run: |
echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io --username ${DOCKER_USERNAME} --password-stdin
- name: Set up Docker builder
run: |
docker run --privileged --rm tonistiigi/binfmt --install ${{ matrix.platform }}
docker buildx create --use --name cross-builder
- name: Build and push image, tag as "latest"
run: |
RUSTFLAGS="-C link-arg=-lgcc -Clink-arg=-static-libgcc" \
cross build --bin kakarot-rpc --release --target ${{ matrix.target }}
mkdir -p ./bin/${{ matrix.platform }}
mv target/${{ matrix.target }}/release/kakarot-rpc ./bin/${{ matrix.platform }}/kakarot-rpc
docker buildx build --file ./docker/rpc/Dockerfile.cross . \
--platform linux/${{ matrix.target }} \
--tag ${{ env.REGISTRY_IMAGE }}:${{ github.ref_name }} --tag ${{ env.REGISTRY_IMAGE }}:latest --push
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ jobs:
files: lcov.info
fail_ci_if_error: false

# The CI job for the Hive was taken from Reth workflow
# Inspired by Reth CI.
# <https://github.com/paradigmxyz/reth/blob/027d50fc105fd527dca0bf56fe51b7240f119e66/.github/workflows/hive.yml>
hive-prepare:
runs-on: ubuntu-latest-16-cores
timeout-minutes: 45
Expand Down
4 changes: 2 additions & 2 deletions .trunk/trunk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ lint:
- [email protected]
- [email protected]
- [email protected]
- shellcheck@0.9.0
- shellcheck@0.10.0
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- trufflehog@3.68.5
- trufflehog@3.69.0
- [email protected]
ignore:
- linters: [ALL]
Expand Down
5 changes: 5 additions & 0 deletions Cross.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[build]
pre-build = [
# rust-bindgen dependencies: llvm-dev libclang-dev (>= 5.0) clang (>= 5.0)
"apt-get update && apt-get install -y --no-install-recommends llvm-dev libclang-6.0-dev clang-6.0",
]
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ load-env:
run-dev: load-env
RUST_LOG=trace cargo run --bin kakarot-rpc

docker-build: setup
docker build -t kakarot-rpc . -f docker/rpc/Dockerfile

# Run Katana, Deploy Kakarot, Run Kakarot RPC
katana-rpc-up:
docker compose up -d --force-recreate
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ TL;DR:
- Run with Docker Compose:
- `make katana-rpc-up`
- To kill these processes, `make docker-down`
- Build the docker image for the RPC:
- `make docker-build`

### Prerequisites

Expand Down Expand Up @@ -189,6 +191,15 @@ For Madara
make madara-rpc-up
```

### Building a [Docker Image](https://docs.docker.com/reference/cli/docker/image/build/)

In order to build a Docker Image for the RPC, you can run the below command
which will setup the local environment and compile the binary:

```console
make docker-build
```

### Sending transactions to RPC using [forge script](https://book.getfoundry.sh/reference/forge/forge-script)

An example script to run which uses a pre-funded EOA account with private key
Expand Down
104 changes: 20 additions & 84 deletions docker/rpc/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,93 +1,24 @@
FROM python:3.9.13 as compiler
# install poetry
ENV POETRY_VERSION=1.7.1
ENV PATH="$PATH:/root/.local/bin:/root/.foundry/bin"
ARG GITHUB_TOKEN
ENV GITHUB_TOKEN=$GITHUB_TOKEN

RUN curl -sSL https://install.python-poetry.org | python3 -
RUN poetry config virtualenvs.create false

WORKDIR /usr/src/compiler

# Install dependencies
RUN curl -L https://foundry.paradigm.xyz -o foundry.sh \
&& chmod +x foundry.sh \
&& ./foundry.sh \
&& foundryup \
&& apt-get update && apt-get install -y \
jq \
wget \
tar \
unzip \
zip
FROM lukemathwalker/cargo-chef:latest-rust-1 AS chef
WORKDIR /app

FROM chef AS planner
COPY . .
RUN make setup

# Define ARG for build platform
FROM --platform=$BUILDPLATFORM rust:1.75 as builder

# Set ARG for build and target platform
ARG BUILDPLATFORM
ARG TARGETPLATFORM

# Set working directory
WORKDIR /usr/src/rpc
RUN cargo chef prepare --recipe-path recipe.json

COPY --from=compiler /usr/src/compiler/.kakarot/artifacts /usr/src/rpc/.kakarot/artifacts
FROM chef AS builder
COPY --from=planner /app/recipe.json recipe.json

# Copy source code
# Build application
COPY . .
# Install system dependencies
RUN apt-get update && \
apt-get -y upgrade && \
apt-get install -y libclang-dev
RUN cargo build --release --bin kakarot-rpc

# Cross-compile the application for a given platform
RUN build_platform() { \
ARCH=$1; \
COMPILER=$2; \
LINKER=$3; \
echo "Building for $TARGETPLATFORM on $BUILDPLATFORM"; \
# Add the specified Rust target architecture
rustup target add $ARCH; \
# Update package lists and install the specified compiler
apt-get update && apt-get -y install $COMPILER libclang-dev; \
# Determine the BINDGEN_EXTRA_CLANG_ARGS based on comparison between BUILDPLATFORM and TARGETPLATFORM
if [ "$BUILDPLATFORM" = "$TARGETPLATFORM" ]; then \
BINDGEN_EXTRA_CLANG_ARGS=""; \
else \
BINDGEN_EXTRA_CLANG_ARGS="--sysroot /usr/${LINKER%-gcc}"; \
fi; \
echo "Using BINDGEN_EXTRA_CLANG_ARGS: $BINDGEN_EXTRA_CLANG_ARGS"; \
# Build the Rust application for the specified target
BINDGEN_EXTRA_CLANG_ARGS=$BINDGEN_EXTRA_CLANG_ARGS cargo build --all --release \
--target=$ARCH; \
# Copy the built binary to a common release directory
cp /usr/src/rpc/target/$ARCH/release/kakarot-rpc /usr/src/rpc/target/release/; \
} \
&& rustup self update \
&& case "$TARGETPLATFORM" in \
"linux/amd64") \
build_platform "x86_64-unknown-linux-gnu" "gcc-x86-64-linux-gnu" "x86_64-linux-gnu-gcc"; \
;; \
"linux/arm64") \
build_platform "aarch64-unknown-linux-gnu" "gcc-aarch64-linux-gnu" "aarch64-linux-gnu-gcc"; \
;; \
*) \
echo "Unknown TARGETPLATFORM: $TARGETPLATFORM"; \
exit 1; \
;; \
esac

# Create a new container from scratch to reduce image size
FROM debian:bookworm

# Install any necessary dependencies
RUN apt-get update && apt-get install -y libssl-dev ca-certificates tini curl && rm -rf /var/lib/apt/lists/*

# Set the working directory
WORKDIR /usr/src/app

# Copy the built binary from the previous container
COPY --from=builder /usr/src/rpc/target/release/kakarot-rpc /usr/local/bin
FROM debian:bookworm-slim AS runtime
WORKDIR /app
COPY --from=builder /app/target/release/kakarot-rpc /usr/local/bin

# Expose the port that the RPC server will run on
EXPOSE 9545
Expand All @@ -102,6 +33,11 @@ HEALTHCHECK --interval=3s --timeout=5s --start-period=1s --retries=5 \
--header "Content-Type: application/json" \
--data '{"jsonrpc": "2.0", "method": "eth_chainId", "id": 1}' http://${KAKAROT_RPC_URL} || exit 1

RUN apt-get update && \
apt-get -y upgrade && \
apt-get install -y libssl-dev ca-certificates tini curl && \
rm -rf /var/lib/apt/lists/*

# Seen in https://github.com/eqlabs/pathfinder/blob/4ab915a830953ed6f02af907937b46cb447d9a92/Dockerfile#L120 -
# Allows for passing args down to the underlying binary easily
ENTRYPOINT ["/usr/bin/tini", "--", "/usr/local/bin/kakarot-rpc"]
Expand Down
35 changes: 35 additions & 0 deletions docker/rpc/Dockerfile.cross
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
FROM --platform=$TARGETPLATFORM debian:bookworm

# Filled by docker buildx
ARG TARGETARCH

# Install any necessary dependencies
RUN apt-get update && \
apt-get -y upgrade && \
apt-get install -y libssl-dev ca-certificates tini curl && \
rm -rf /var/lib/apt/lists/*

# Copy the built binary. This assumes that the binary
# has already been compiled for the target architecture
COPY ./bin/$TARGETARCH/kakarot-rpc /usr/local/bin/kakarot-rpc

# Expose the port that the RPC server will run on
EXPOSE 9545
EXPOSE 3030

# this is required to have exposing ports work from docker, the default is not this.
ENV KAKAROT_RPC_URL="0.0.0.0:3030"

# Add a health check to make sure the service is healthy
HEALTHCHECK --interval=3s --timeout=5s --start-period=1s --retries=5 \
CMD curl --request POST \
--header "Content-Type: application/json" \
--data '{"jsonrpc": "2.0", "method": "eth_chainId", "id": 1}' http://${KAKAROT_RPC_URL} || exit 1

# Seen in https://github.com/eqlabs/pathfinder/blob/4ab915a830953ed6f02af907937b46cb447d9a92/Dockerfile#L120 -
# Allows for passing args down to the underlying binary easily
ENTRYPOINT ["/usr/bin/tini", "--", "/usr/local/bin/kakarot-rpc"]

# empty CMD is needed and cannot be --help because otherwise configuring from
# environment variables only would be impossible and require a workaround.
CMD []

0 comments on commit df05b69

Please sign in to comment.