Skip to content

Commit a95f203

Browse files
committed
✨ Enhance build-and-push script with argument parsing and Dockerfile optimizations
1 parent de333f8 commit a95f203

3 files changed

Lines changed: 68 additions & 11 deletions

File tree

apps/backend/Dockerfile

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,28 @@ RUN npx prisma generate
4848

4949
# Remove unnecessary engines and files to reduce size
5050
RUN find node_modules/.prisma -type f -not -path "*libquery_engine-linux-musl*" -not -path "*libquery_engine-debian*" -delete || true
51-
RUN rm -rf node_modules/@prisma/engines
51+
RUN rm -rf node_modules/@prisma/engines
5252
RUN npm prune --production
5353
RUN rm -rf node_modules/.cache
5454

55+
# MINIMAL CHANGE: split prisma out so node_modules can be copied without it
56+
RUN mkdir -p /optimizer/node_modules_no_prisma \
57+
&& cp -a node_modules/. /optimizer/node_modules_no_prisma/ \
58+
&& rm -rf /optimizer/node_modules_no_prisma/@prisma \
59+
/optimizer/node_modules_no_prisma/.prisma \
60+
&& mkdir -p /optimizer/prisma_bundle \
61+
&& cp -a node_modules/@prisma /optimizer/prisma_bundle/@prisma \
62+
&& cp -a node_modules/.prisma /optimizer/prisma_bundle/.prisma
63+
5564
# Production stage - uses Alpine for minimal size
5665
FROM node:20-alpine AS release
5766
WORKDIR /app
5867

5968
# Copy minimized node_modules from the optimizer stage
60-
COPY --from=deps-optimizer /optimizer/node_modules ./node_modules
69+
# (now in two layers: big node_modules without Prisma, then Prisma only)
70+
COPY --from=deps-optimizer /optimizer/node_modules_no_prisma ./node_modules
71+
COPY --from=deps-optimizer /optimizer/prisma_bundle/.prisma ./node_modules/.prisma
72+
COPY --from=deps-optimizer /optimizer/prisma_bundle/@prisma ./node_modules/@prisma
6173

6274
# Copy only necessary files for runtime
6375
COPY --from=builder /app/apps/backend/dist ./dist

apps/backend/prisma/schema.prisma

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
generator client {
22
provider = "prisma-client-js"
3+
binaryTargets = ["linux-musl"]
34
previewFeatures = ["multiSchema"]
45
}
56

build-and-push.sh

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,50 @@ BLUE='\033[0;34m'
1010
RED='\033[0;31m'
1111
NC='\033[0m' # No Color
1212

13-
# Read build mode from the first argument (prod or dev)
14-
MODE=$1
15-
if [[ "$MODE" != "prod" && "$MODE" != "dev" ]]; then
16-
echo -e "${RED}❌ Error: Invalid mode specified. Use 'prod' or 'dev'.${NC}"
17-
echo "Usage: $0 <prod|dev>"
13+
# Parse command line arguments
14+
ONLY_PUSH=false
15+
USE_CACHE=false
16+
MODE=""
17+
18+
# Parse arguments
19+
while [[ $# -gt 0 ]]; do
20+
case $1 in
21+
--only-push)
22+
ONLY_PUSH=true
23+
shift
24+
;;
25+
--cache)
26+
USE_CACHE=true
27+
shift
28+
;;
29+
prod|dev)
30+
if [[ -n "$MODE" ]]; then
31+
echo -e "${RED}❌ Error: Multiple modes specified. Use only one mode.${NC}"
32+
exit 1
33+
fi
34+
MODE=$1
35+
shift
36+
;;
37+
*)
38+
echo -e "${RED}❌ Error: Unknown option '$1'${NC}"
39+
echo "Usage: $0 [--only-push] [--cache] <prod|dev>"
40+
exit 1
41+
;;
42+
esac
43+
done
44+
45+
# Check if mode is specified
46+
if [[ -z "$MODE" ]]; then
47+
echo -e "${RED}❌ Error: Mode not specified. Use 'prod' or 'dev'.${NC}"
48+
echo "Usage: $0 [--only-push] [--cache] <prod|dev>"
1849
exit 1
1950
fi
2051

21-
echo -e "${BLUE}🚀 Starting build and push process in '${MODE}' mode...${NC}"
52+
if [[ "$ONLY_PUSH" == "true" ]]; then
53+
echo -e "${BLUE}🚀 Starting push process in '${MODE}' mode (skipping build)...${NC}"
54+
else
55+
echo -e "${BLUE}🚀 Starting build and push process in '${MODE}' mode...${NC}"
56+
fi
2257

2358
# Set environment file and tag based on mode
2459
if [[ "$MODE" == "prod" ]]; then
@@ -66,9 +101,18 @@ tag_and_push() {
66101
# Trap to capture ctrl+c and exit gracefully
67102
trap 'echo -e "${RED}🛑 Build and push aborted${NC}"; exit 1' INT
68103

69-
# Build all services using docker compose
70-
echo -e "${BLUE}🏗️ Building Docker images for AMD64 architecture...${NC}"
71-
docker compose --env-file "${ENV_FILE}" build --no-cache
104+
# Build all services using docker compose (skip if --only-push is used)
105+
if [[ "$ONLY_PUSH" != "true" ]]; then
106+
if [[ "$USE_CACHE" == "true" ]]; then
107+
echo -e "${BLUE}🏗️ Building Docker images for AMD64 architecture (with cache)...${NC}"
108+
docker compose --env-file "${ENV_FILE}" build
109+
else
110+
echo -e "${BLUE}🏗️ Building Docker images for AMD64 architecture (no cache)...${NC}"
111+
docker compose --env-file "${ENV_FILE}" build --no-cache
112+
fi
113+
else
114+
echo -e "${BLUE}⏭️ Skipping build step (--only-push mode)${NC}"
115+
fi
72116

73117
# Tag and push images to registry in parallel
74118
echo -e "${BLUE}📦 Tagging and pushing images with tag '${TAG}' to registry in parallel...${NC}"

0 commit comments

Comments
 (0)