Skip to content

Commit

Permalink
ci(workflows) add GitHub Actions workflow for preview builds (#2446)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
steebchen committed Sep 20, 2024
1 parent 82a23a5 commit 8b7d6b2
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
target/
.git/
.github/
.idea/
.vscode/
.devcontainer/
.cargo/
artifacts/
45 changes: 45 additions & 0 deletions .github/Dockerfile
Original file line number Diff line number Diff line change
@@ -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/
86 changes: 86 additions & 0 deletions .github/workflows/preview.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ justfile
spawn-and-move-db
types-test-db
examples/spawn-and-move/manifests/saya/**

artifacts/

0 comments on commit 8b7d6b2

Please sign in to comment.