Improve docker builds

This commit is contained in:
Laura Hausmann 2023-08-04 20:35:24 +02:00
parent 5fe5165444
commit ff0d9ac476
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 11 additions and 24 deletions

View file

@ -6,22 +6,6 @@ WORKDIR /iceshrimp
# Install compilation dependencies # Install compilation dependencies
RUN apk add --no-cache --no-progress git alpine-sdk vips-dev python3 nodejs-current npm rust cargo vips RUN apk add --no-cache --no-progress git alpine-sdk vips-dev python3 nodejs-current npm rust cargo vips
# Collect sccache args
ARG SCCACHE_VERSION=v0.5.4
ARG SCCACHE_ARCHITECTURE=x86_64
ARG SCCACHE_MEMORY_LIMIT=10G
# Download and initialize sccache
RUN wget https://github.com/mozilla/sccache/releases/download/${SCCACHE_VERSION}/sccache-${SCCACHE_VERSION}-${SCCACHE_ARCHITECTURE}-unknown-linux-musl.tar.gz && \
tar xzf sccache-${SCCACHE_VERSION}-${SCCACHE_ARCHITECTURE}-unknown-linux-musl.tar.gz && \
mv sccache-${SCCACHE_VERSION}-${SCCACHE_ARCHITECTURE}-unknown-linux-musl/sccache /usr/local/bin && \
rm -rf sccache-${SCCACHE_VERSION}-${SCCACHE_ARCHITECTURE}-unknown-linux-musl*
# Set sccache as the Rust compiler, set default dir, and set cache memory limit
ENV RUSTC_WRAPPER=sccache
ENV SCCACHE_DIR=/tmp/sccache
ENV SCCACHE_CACHE_SIZE=${SCCACHE_MEMORY_LIMIT}
# Copy only the cargo dependency-related files first, to cache efficiently # Copy only the cargo dependency-related files first, to cache efficiently
COPY packages/backend/native-utils/Cargo.toml packages/backend/native-utils/Cargo.toml COPY packages/backend/native-utils/Cargo.toml packages/backend/native-utils/Cargo.toml
COPY packages/backend/native-utils/Cargo.lock packages/backend/native-utils/Cargo.lock COPY packages/backend/native-utils/Cargo.lock packages/backend/native-utils/Cargo.lock
@ -53,15 +37,18 @@ RUN corepack enable && corepack prepare yarn@stable --activate && yarn
# Save yarn cache # Save yarn cache
RUN --mount=type=cache,target=/iceshrimp/.yarncache rm -rf .yarncache/* && cp -Tr .yarn .yarncache RUN --mount=type=cache,target=/iceshrimp/.yarncache rm -rf .yarncache/* && cp -Tr .yarn .yarncache
# Copy in the rest of the native-utils rust files
COPY packages/backend/native-utils packages/backend/native-utils/
# Compile native-utils utilising sccache
RUN --mount=type=cache,target=/root/.cargo --mount=type=cache,target=/tmp/sccache --mount=type=cache,target=/iceshrimp/packages/backend/native-utils/migration/target yarn workspace native-utils build
# Copy in the rest of the files to compile # Copy in the rest of the files to compile
COPY . ./ COPY . ./
RUN env NODE_ENV=production sh -c "yarn workspace iceshrimp-js run build && yarn workspaces foreach --exclude native-utils --include sw --include client --include backend --include megalodon run build && yarn gulp"
# Fix napi-rs jank
RUN --mount=type=cache,target=/iceshrimp/.napi_buildcache cp -Tr /iceshrimp/.napi_buildcache /iceshrimp/packages/backend/native-utils/built
RUN --mount=type=cache,target=/iceshrimp/packages/backend/native-utils/target if [[ ! -f /iceshrimp/packages/backend/native-utils/built/index.js ]]; then rm -rf /iceshrimp/packages/backend/native-utils/target/release; fi
# Build the thing
RUN --mount=type=cache,target=/root/.cargo --mount=type=cache,target=/iceshrimp/packages/backend/native-utils/target env NODE_ENV=production sh -c "yarn build && yarn gulp"
# Fix napi-rs jank (part 2)
RUN --mount=type=cache,target=/iceshrimp/.napi_buildcache cp -Tr /iceshrimp/packages/backend/native-utils/built /iceshrimp/.napi_buildcache
# Prepare yarn cache (production) # Prepare yarn cache (production)
RUN --mount=type=cache,target=/iceshrimp/.yarncache_prod cp -Tr .yarncache_prod .yarn RUN --mount=type=cache,target=/iceshrimp/.yarncache_prod cp -Tr .yarncache_prod .yarn

View file

@ -36,7 +36,7 @@
"artifacts": "napi artifacts", "artifacts": "napi artifacts",
"build": "yarn build:napi && yarn build:migration", "build": "yarn build:napi && yarn build:migration",
"build:napi": "napi build --features napi --platform --release ./built/", "build:napi": "napi build --features napi --platform --release ./built/",
"build:migration": "cargo build --locked --profile release-no-lto -p migration --target-dir migration/target && cp ./migration/target/release-no-lto/migration ./built/migration", "build:migration": "cargo build --locked --profile release-no-lto -p migration && cp ./target/release-no-lto/migration ./built/migration",
"build:debug": "napi build --platform ./built/ && cargo build -p migration", "build:debug": "napi build --platform ./built/ && cargo build -p migration",
"prepublishOnly": "napi prepublish -t npm", "prepublishOnly": "napi prepublish -t npm",
"test": "yarn cargo:test && yarn build:napi && ava", "test": "yarn cargo:test && yarn build:napi && ava",