From 8b7d6b2ce6c7abb7a885cf3bc55c825aa77adb84 Mon Sep 17 00:00:00 2001 From: Luca Steeb Date: Fri, 20 Sep 2024 13:40:14 -0400 Subject: [PATCH] ci(workflows) add GitHub Actions workflow for preview builds (#2446) * Add GitHub Actions workflow for Docker builds and deployment This commit introduces a new GitHub Actions workflow file, `preview.yml`, to automate building and deploying Docker images. The workflow includes steps for setting up the Rust toolchain, building binaries, archiving artifacts, and deploying them via Docker and Slot. This enhancement streamlines the CI/CD process, ensuring consistency and efficiency in releases. * wip * wip * fix * bin slot * fix * temp * wip * xdg * short sha * uncomment * world input * remove slot * fix * bump * remove branch deploy * dockerify all * temp preview publish * rust toolchain & fixes * docker wip * build local and run * revert * checkout fix * ls * fix * fix * fix artifacts * move cache * separate cache * minor adaptions * bump to 32 cores yolo * remove temp push trigger --- .dockerignore | 8 ++++ .github/Dockerfile | 45 ++++++++++++++++++ .github/workflows/preview.yml | 86 +++++++++++++++++++++++++++++++++++ .gitignore | 2 + 4 files changed, 141 insertions(+) create mode 100644 .dockerignore create mode 100644 .github/Dockerfile create mode 100644 .github/workflows/preview.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000000..2c7d1472f4 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,8 @@ +target/ +.git/ +.github/ +.idea/ +.vscode/ +.devcontainer/ +.cargo/ +artifacts/ diff --git a/.github/Dockerfile b/.github/Dockerfile new file mode 100644 index 0000000000..802cdbf052 --- /dev/null +++ b/.github/Dockerfile @@ -0,0 +1,45 @@ +FROM rust:slim as chef + +# Install libclang and other necessary tools +RUN apt-get update && \ + apt-get install -y clang llvm-dev libclang-dev git libtool automake autoconf make curl protobuf-compiler && \ + rm -rf /var/lib/apt/lists/* + +# Verify and set LIBCLANG_PATH environment variable +RUN find /usr -name "libclang.so*" && \ + export LIBCLANG_PATH=$(find /usr -name "libclang.so*" | head -n 1 | xargs dirname) + +COPY rust-toolchain.toml . +RUN rustup install $(cat rust-toolchain.toml | grep channel | cut -d' ' -f3 | tr -d '"') +RUN rustup component add cargo clippy rust-docs rust-std rustc rustfmt + +RUN cargo install cargo-chef + +FROM chef AS planner + +WORKDIR /app +COPY . . +RUN cargo chef prepare --recipe-path recipe.json + +FROM chef AS builder + +WORKDIR /app +COPY --from=planner /app/recipe.json recipe.json +# Build dependencies - this is the caching Docker layer! +RUN cargo chef cook --release --recipe-path recipe.json +RUN cargo build --release --bins # pre-cache some stuff + +# Build application +COPY . . +ENV PATH="/root/.cargo/bin:${PATH}" + +RUN cargo build --release --bins + +FROM rust:1-alpine + +WORKDIR / + +COPY --from=builder /app/target/release/katana /app/artifacts/ +COPY --from=builder /app/target/release/torii /app/artifacts/ +COPY --from=builder /app/target/release/sozo /app/artifacts/ +COPY --from=builder /app/target/release/saya /app/artifacts/ diff --git a/.github/workflows/preview.yml b/.github/workflows/preview.yml new file mode 100644 index 0000000000..fc1d296883 --- /dev/null +++ b/.github/workflows/preview.yml @@ -0,0 +1,86 @@ +name: preview + +on: + workflow_dispatch: + +jobs: + publish: + runs-on: ubuntu-latest-32-cores + + steps: + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Cache Docker layers + uses: actions/cache@v4 + with: + path: /tmp/.buildx-cache/prebuild + key: ${{ runner.os }}-buildx-${{ github.ref_name }}-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-buildx-${{ github.ref_name }} + ${{ runner.os }}-buildx- + + - name: Cache Docker layers + uses: actions/cache@v4 + with: + path: /tmp/.buildx-cache/build + key: ${{ runner.os }}-buildx-${{ github.ref_name }}-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-buildx-${{ github.ref_name }} + ${{ runner.os }}-buildx- + + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Login to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Set outputs + id: vars + run: | + set -eux + git config --global --add safe.directory "${{ github.workspace }}" + echo "sha_short=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT" + + - name: Build Docker image + uses: docker/build-push-action@v5 + with: + context: . + file: .github/Dockerfile + load: true + tags: build-local:latest + cache-from: type=local,src=/tmp/.buildx-cache/prebuild + cache-to: type=local,dest=/tmp/.buildx-cache-new/prebuild,mode=max + platforms: linux/amd64 + + - name: Build local binaries + run: | + set -eux + docker run --rm -v "$(pwd)/artifacts/$PLATFORM:/artifacts" build-local:latest /bin/sh -c "cp -r /app/artifacts/* /artifacts/" + env: + PLATFORM: linux/amd64 + + - name: Build and push docker image + uses: docker/build-push-action@v3 + with: + push: true + tags: ghcr.io/${{ github.repository }}:preview--${{ steps.vars.outputs.sha_short }} + cache-from: type=local,src=/tmp/.buildx-cache/build + cache-to: type=local,dest=/tmp/.buildx-cache-new/build,mode=max + platforms: linux/amd64 + build-contexts: | + artifacts=artifacts + + - # Temp fix + # https://github.com/docker/build-push-action/issues/252 + # https://github.com/moby/buildkit/issues/1896 + name: Move cache + run: | + rm -rf /tmp/.buildx-cache/prebuild + mv /tmp/.buildx-cache-new/prebuild /tmp/.buildx-cache/prebuild + rm -rf /tmp/.buildx-cache/build + mv /tmp/.buildx-cache-new/build /tmp/.buildx-cache/build diff --git a/.gitignore b/.gitignore index cf16ca3022..75b10b8482 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,5 @@ justfile spawn-and-move-db types-test-db examples/spawn-and-move/manifests/saya/** + +artifacts/