Skip to content

Commit

Permalink
fix(docker): auto-build sdk & hub in build.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFlurry committed Jan 30, 2025
1 parent 7fd4211 commit ee5a532
Show file tree
Hide file tree
Showing 16 changed files with 137 additions and 131 deletions.
33 changes: 24 additions & 9 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
*
# MARK: Docker-specific
.git/

!Cargo.lock
!Cargo.toml
!sdks/api/full/rust/Cargo.toml
!sdks/api/full/rust/src
!packages
!resources/legacy/proto
# MARK: Copied from .gitignore ===
**/.DS_Store

!docker/monolith
docker/monolith/*.md
# Rust
**/target

# JavaScript
**/.cache/
**/.turbo
**/.yarn
**/dist/
**/node_modules/
**/target
.turbo

# Site
site/public/rss/
!site/public/rss/.gitkeep

site/src/generated/
!site/src/generated/errorPages.json
!site/src/generated/apiPages.json
!site/src/generated/meta.json

63 changes: 12 additions & 51 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,66 +1,27 @@
# Keep all .gitignore files in this file so we can easily keep it in sync with .dockerignore
#
# Make sure to update .dockerignore appropriately

**/.DS_Store
**/symbolCache.db

# Terraform
**/*.tfstate
**/*.tfstate.*
**/.terraform.*
**/.terraform/
infra/tf/*/_bolt.tf

# Vagrant
.vagrant-remote
.vagrant-digitalocean-token
**/.vagrant/

# Bolt
.bolt-cache.json
.volumes/
Bolt.local.toml

/namespaces/*
!/namespaces/.gitkeep
!/namespaces/README.md

/secrets/*
!/secrets/.gitkeep
!/secrets/README.md

# Generated code
gen/build_script.sh
gen/test_build_script.sh
gen/svc/
gen/tf/
gen/docker/
gen/tests/
gen/k8s/

# K3s
volumes/

# Rust
lib/**/Cargo.lock
!lib/bolt/Cargo.lock
!lib/job-runner/Cargo.lock
# Allows symlinking target instead of being a directory
**/target
**/target/
**/*.rustfmt

# JavaScript
**/.cache/
**/.turbo
**/.yarn
**/dist/
**/node_modules/
**/target
.turbo

tests/basic-game/.env
# Site
site/public/rss/
!site/public/rss/.gitkeep

site/src/generated/
!site/src/generated/errorPages.json
!site/src/generated/apiPages.json
!site/src/generated/meta.json

# Added by cargo

/target
.yarn/cache
.yarn/install-state.gz
.turbo
1 change: 1 addition & 0 deletions docker/dev-full/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ services:
build:
context: ../..
dockerfile: docker/universal/Dockerfile
target: server-full
restart: unless-stopped
command: sleep infinity
environment:
Expand Down
24 changes: 16 additions & 8 deletions docker/monolith/build-scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ set -euf -o pipefail

TARGET_ARCH=$(uname -m | sed 's/aarch64/arm64/' | sed 's/x86_64/amd64/')

apt-get update

# Fixes "Failed to fetch" error
apt-get install --reinstall -y apt-transport-https

# Install required packages
#
# The FDB version should match `cluster::workflows::server::install::install_scripts::components::fdb::FDB_VERSION`
apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
apt-get install -y \
libclang-dev \
ca-certificates \
openssl \
Expand All @@ -18,12 +23,15 @@ apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
apt-transport-https \
dirmngr \
netcat-openbsd \
procps && \
(curl -L https://github.com/golang-migrate/migrate/releases/download/v4.18.1/migrate.linux-${TARGET_ARCH}.tar.gz | tar xvz) && \
mv migrate /usr/local/bin/migrate && \
curl -fsSL https://deno.land/x/install/install.sh | sh && \
ln -s /root/.deno/bin/deno /usr/local/bin/deno && \
curl -Lf -o /lib/libfdb_c.so "https://github.com/apple/foundationdb/releases/download/7.1.60/libfdb_c.x86_64.so"
procps

(curl -L https://github.com/golang-migrate/migrate/releases/download/v4.18.1/migrate.linux-${TARGET_ARCH}.tar.gz | tar xvz)
mv migrate /usr/local/bin/migrate

curl -fsSL https://deno.land/x/install/install.sh | sh
ln -s /root/.deno/bin/deno /usr/local/bin/deno

curl -Lf -o /lib/libfdb_c.so "https://github.com/apple/foundationdb/releases/download/7.1.60/libfdb_c.x86_64.so"

# === Traefik ===
curl -sSLf https://github.com/traefik/traefik/releases/download/v${TRAEFIK_VERSION}/traefik_v${TRAEFIK_VERSION}_linux_${TARGET_ARCH}.tar.gz | \
Expand All @@ -45,7 +53,7 @@ useradd -m -s /bin/bash clickhouse && \
curl -fsSL 'https://packages.clickhouse.com/rpm/lts/repodata/repomd.xml.key' | gpg --dearmor -o /usr/share/keyrings/clickhouse-keyring.gpg && \
echo "deb [signed-by=/usr/share/keyrings/clickhouse-keyring.gpg] https://packages.clickhouse.com/deb stable main" | tee /etc/apt/sources.list.d/clickhouse.list && \
apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -y clickhouse-client clickhouse-server
apt-get install -y clickhouse-client clickhouse-server

# === NATS ===
useradd -m -s /bin/bash nats && \
Expand Down
48 changes: 34 additions & 14 deletions docker/universal/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
# TODO(RVT-4168): Compile libfdb from scratch for ARM
FROM --platform=linux/amd64 rust:1.82.0-bookworm AS builder

ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -y \
apt-get install -y \
curl \
g++ \
git \
Expand All @@ -25,8 +26,17 @@ RUN apt-get update && \
pkg-config \
protobuf-compiler \
wget && \
curl -fsSL https://deb.nodesource.com/setup_22.x | bash - && \
apt-get install -y nodejs && \
corepack enable && \
curl -Lf -o /lib/libfdb_c.so "https://github.com/apple/foundationdb/releases/download/7.1.60/libfdb_c.x86_64.so"

# Disable interactive prompt
ENV COREPACK_ENABLE_DOWNLOAD_PROMPT=0

# TODO: REMOVE
ENV FONTAWESOME_PACKAGE_TOKEN=E7A94808-3467-4150-B90D-EABDAEB9E0B4

WORKDIR /app

COPY . .
Expand All @@ -38,28 +48,35 @@ RUN \
--mount=type=cache,target=/usr/local/cargo/git,id=univseral-cargo-git \
--mount=type=cache,target=/usr/local/cargo/registry,id=univseral-cargo-registry \
--mount=type=cache,target=/app/target,id=univseral-target \
RUSTFLAGS="--cfg tokio_unstable" cargo build -vv --bin rivet-server --bin rivet-client --bin rivet-isolate-v8-runner --bin rivet-container-runner && \
--mount=type=cache,target=/app/node_modules,id=universal-node-modules \
--mount=type=cache,target=/app/.turbo,id=universal-turbo \
--mount=type=cache,target=/root/.cache,id=universal-user-cache \
--mount=type=cache,target=/root/.npm,id=universal-user-npm \
--mount=type=cache,target=/root/.yarn,id=universal-user-yarn \
RUSTFLAGS="--cfg tokio_unstable" cargo build --bin rivet-server --bin rivet-client --bin rivet-isolate-v8-runner --bin rivet-container-runner && \
mkdir /app/dist/ && \
mv target/debug/rivet-server target/debug/rivet-client target/debug/rivet-isolate-v8-runner target/debug/rivet-container-runner /app/dist/
cp target/debug/rivet-server target/debug/rivet-client target/debug/rivet-isolate-v8-runner target/debug/rivet-container-runner /app/dist/

# MARK: Server (Full)
FROM debian:12.1-slim AS server-full
FROM debian:12.9-slim AS server-full

ENV DEBIAN_FRONTEND=noninteractive
# - Install curl for health checks
# - Install go-migrate for running migrations
# - Install database clients to be able to run `rivet db shell ...` (Redis, Postgres, ClickHouse)
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -y \
apt-get install -y \
ca-certificates \
openssl \
curl \
redis-tools \
postgresql-client \
gpg && \
gpg \
dirmngr && \
curl -fsSL 'https://packages.clickhouse.com/rpm/lts/repodata/repomd.xml.key' | gpg --dearmor -o /usr/share/keyrings/clickhouse-keyring.gpg && \
echo "deb [signed-by=/usr/share/keyrings/clickhouse-keyring.gpg] https://packages.clickhouse.com/deb stable main" | tee /etc/apt/sources.list.d/clickhouse.list && \
apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -y clickhouse-client && \
apt-get install -y clickhouse-client && \
(curl -L https://github.com/golang-migrate/migrate/releases/download/v4.18.1/migrate.linux-amd64.tar.gz | tar xvz) && \
mv migrate /usr/local/bin/migrate && \
apt-get clean && \
Expand All @@ -70,10 +87,11 @@ COPY --from=builder /app/dist/rivet-server /usr/bin/rivet-server
CMD ["/usr/bin/rivet-server"]

# MARK: Server (Slim)
FROM debian:12.1-slim AS server-slim
FROM debian:12.9-slim AS server-slim

RUN DEBIAN_FRONTEND=noninteractive apt-get update -y && \
apt-get install -y --no-install-recommends ca-certificates openssl && \
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update -y && \
apt-get install -y ca-certificates openssl && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

Expand All @@ -82,9 +100,10 @@ COPY --from=builder /app/dist/rivet-server /usr/bin/rivet-server
CMD ["/usr/bin/rivet-server"]

# MARK: Runner base
FROM --platform=linux/amd64 debian:12.1-slim AS base-runner
RUN DEBIAN_FRONTEND=noninteractive apt-get update -y && \
apt-get install -y --no-install-recommends ca-certificates openssl curl && \
FROM --platform=linux/amd64 debian:12.9-slim AS base-runner
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update -y && \
apt-get install -y ca-certificates openssl curl && \
curl -Lf -o /lib/libfdb_c.so "https://github.com/apple/foundationdb/releases/download/7.1.60/libfdb_c.x86_64.so"

# MARK: Runner (Full)
Expand All @@ -103,7 +122,8 @@ COPY --from=builder /app/dist/rivet-container-runner /usr/local/bin/
ENTRYPOINT ["rivet-client"]

# MARK: Monlith
FROM --platform=linux/amd64 debian:12.1-slim AS monolith
FROM --platform=linux/amd64 debian:12.9-slim AS monolith
ENV DEBIAN_FRONTEND=noninteractive

ARG TRAEFIK_VERSION=3.2.1
ARG COCKROACHDB_VERSION=24.2.3
Expand Down
5 changes: 1 addition & 4 deletions frontend/apps/hub/src/components/dialogs/secret-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ export default function SecretDialogContent(props: ContentProps) {
<Text>
Rivet 2.0
<br />
<Code>{__APP_GIT_BRANCH__}</Code>@
<Code>{__APP_GIT_COMMIT__}</Code>
<br />
<Code>{__APP_RIVET_NAMESPACE__ || "unknown"}</Code>
<Code>{__APP_BUILD_ID__}</Code>
</Text>
<Text>
<Link
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const selfProfileIdentityIdQueryOptions = () => {

export const changelogQueryOptions = () => {
return queryOptions({
queryKey: ["changelog", __APP_GIT_COMMIT__],
queryKey: ["changelog", __APP_BUILD_ID__],
staleTime: timing.hours(1),
queryFn: async () => {
const response = await fetch("https://rivet.gg/changelog.json");
Expand Down
2 changes: 1 addition & 1 deletion frontend/apps/hub/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ initThirdPartyProviders();
rivetClient.cloud
.bootstrap()
.then((response) => {
run({ cacheKey: [response.deployHash, __APP_GIT_COMMIT__].join("-") });
run({ cacheKey: [response.deployHash, __APP_BUILD_ID__].join("-") });
})
.catch(() => {
run();
Expand Down
4 changes: 1 addition & 3 deletions frontend/apps/hub/src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/// <reference types="vite/client" />

declare const __APP_GIT_BRANCH__: string;
declare const __APP_GIT_COMMIT__: string;
declare const __APP_RIVET_NAMESPACE__: string;
declare const __APP_BUILD_ID__: string;

declare module "vite-plugin-favicons-inject" {
import type { Plugin } from "vite";
Expand Down
18 changes: 7 additions & 11 deletions frontend/apps/hub/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { execSync } from "node:child_process";
import path from "node:path";
import { sentryVitePlugin } from "@sentry/vite-plugin";
import { transformerNotationFocus } from "@shikijs/transformers";
Expand All @@ -8,13 +7,9 @@ import * as shiki from "shiki";
import { type Plugin, defineConfig } from "vite";
import vitePluginFaviconsInject from "vite-plugin-favicons-inject";

const GIT_BRANCH =
process.env.CF_PAGES_BRANCH ||
execSync("git rev-parse --abbrev-ref HEAD").toString().trim();

const GIT_SHA =
process.env.CF_PAGES_COMMIT_SHA ||
execSync("git rev-parse HEAD").toString().trim();
// These are only needed in CI. They'll be undefined in dev.
const GIT_BRANCH = process.env.CF_PAGES_BRANCH;
const GIT_SHA = process.env.CF_PAGES_COMMIT_SHA;

// https://vitejs.dev/config/
export default defineConfig({
Expand Down Expand Up @@ -44,9 +39,10 @@ export default defineConfig({
port: 5080,
},
define: {
__APP_GIT_BRANCH__: JSON.stringify(GIT_BRANCH),
__APP_GIT_COMMIT__: JSON.stringify(GIT_SHA),
__APP_RIVET_NAMESPACE__: JSON.stringify(process.env.CF_PAGES_BRANCH),
// Provide a unique build ID for cache busting
__APP_BUILD_ID__: JSON.stringify(
`${new Date().toISOString()}@${crypto.randomUUID()}`,
),
},
resolve: {
alias: {
Expand Down
3 changes: 2 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
docker_compose := "docker compose -f docker/dev-full/docker-compose.yml"
# `--parallel 1` because apt has issues when pulling too many containers in parallel
docker_compose := "docker compose -f docker/dev-full/docker-compose.yml --progress=plain --parallel 1"

[group('rust')]
watch:
Expand Down
6 changes: 3 additions & 3 deletions packages/api/ui/src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ impl Router {
) -> GlobalResult<Vec<u8>> {
let content_str = std::str::from_utf8(content)?;

let replacement_count = content_str.matches("__VITE_APP_API_URL__").count();
let replacement_count = content_str.matches("__APP_API_URL__").count();
ensure!(
replacement_count > 0,
"Expected at least one occurrence of __VITE_APP_API_URL__, found {}",
"Expected at least one occurrence of __APP_API_URL__, found {}",
replacement_count
);

let public_origin =
util::url::to_string_without_slash(&config.server()?.rivet.api_public.public_origin());
let replaced_content = content_str.replace("__VITE_APP_API_URL__", &public_origin);
let replaced_content = content_str.replace("__APP_API_URL__", &public_origin);

Ok(replaced_content.into_bytes())
}
Expand Down
Loading

0 comments on commit ee5a532

Please sign in to comment.