Skip to content

Commit 044093a

Browse files
committed
implement docker caching for faster builds
Signed-off-by: Jack Luar <[email protected]>
1 parent d7f3827 commit 044093a

File tree

6 files changed

+125
-18
lines changed

6 files changed

+125
-18
lines changed

.github/workflows/ci-secret.yaml

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@ defaults:
1919
jobs:
2020
build-backend-docker:
2121
runs-on: self-hosted
22+
permissions:
23+
contents: read
24+
packages: write
2225
steps:
2326
- name: Setup python
2427
uses: actions/setup-python@v5
2528
with:
2629
python-version: '3.12'
2730
- name: Install uv
28-
uses: astral-sh/setup-uv@v6
31+
uses: astral-sh/setup-uv@v6
2932
- name: Checkout code
3033
uses: actions/checkout@v4
3134
- name: Setup prereqs
@@ -56,9 +59,48 @@ jobs:
5659
run: |
5760
cp ${{ secrets.PATH_TO_GOOGLE_APPLICATION_CREDENTIALS }} backend/src
5861
cp ${{ secrets.PATH_TO_GOOGLE_APPLICATION_CREDENTIALS }} evaluation/auto_evaluation/src
59-
- name: Build Docker image
62+
63+
- name: Set up Docker Buildx
64+
uses: docker/setup-buildx-action@v3
65+
66+
- name: Log in to GitHub Container Registry
67+
uses: docker/login-action@v3
68+
with:
69+
registry: ghcr.io
70+
username: ${{ github.actor }}
71+
password: ${{ secrets.GITHUB_TOKEN }}
72+
73+
- name: Build and push backend Docker image
74+
uses: docker/build-push-action@v5
75+
with:
76+
context: ./backend
77+
push: true
78+
tags: |
79+
ghcr.io/${{ github.repository }}/backend:latest
80+
ghcr.io/${{ github.repository }}/backend:${{ github.sha }}
81+
cache-from: |
82+
type=registry,ref=ghcr.io/${{ github.repository }}/backend:cache
83+
type=registry,ref=ghcr.io/${{ github.repository }}/backend:latest
84+
cache-to: type=registry,ref=ghcr.io/${{ github.repository }}/backend:cache,mode=max
85+
load: true
86+
87+
- name: Build and push frontend Docker image
88+
uses: docker/build-push-action@v5
89+
with:
90+
context: ./frontend/nextjs-frontend
91+
push: true
92+
tags: |
93+
ghcr.io/${{ github.repository }}/frontend:latest
94+
ghcr.io/${{ github.repository }}/frontend:${{ github.sha }}
95+
cache-from: |
96+
type=registry,ref=ghcr.io/${{ github.repository }}/frontend:cache
97+
type=registry,ref=ghcr.io/${{ github.repository }}/frontend:latest
98+
cache-to: type=registry,ref=ghcr.io/${{ github.repository }}/frontend:cache,mode=max
99+
load: true
100+
101+
- name: Start services with docker compose
60102
run: |
61-
make docker-up
103+
docker compose up -d
62104
63105
- name: Run LLM CI
64106
id: llm_tests

.github/workflows/ci.yaml

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@ defaults:
1717
jobs:
1818
build-backend-docker:
1919
runs-on: self-hosted
20+
permissions:
21+
contents: read
22+
packages: write
2023
steps:
2124
- name: Setup python
2225
uses: actions/setup-python@v5
2326
with:
2427
python-version: '3.12'
2528

2629
- name: Install uv
27-
uses: astral-sh/setup-uv@v6
30+
uses: astral-sh/setup-uv@v6
2831

2932
- name: Checkout code
3033
uses: actions/checkout@v4
@@ -45,6 +48,34 @@ jobs:
4548
cp .env.test .env
4649
make test
4750
48-
- name: Build Docker images
49-
run: |
50-
docker compose build
51+
- name: Set up Docker Buildx
52+
uses: docker/setup-buildx-action@v3
53+
54+
- name: Log in to GitHub Container Registry
55+
uses: docker/login-action@v3
56+
with:
57+
registry: ghcr.io
58+
username: ${{ github.actor }}
59+
password: ${{ secrets.GITHUB_TOKEN }}
60+
61+
- name: Build backend Docker image with cache
62+
uses: docker/build-push-action@v5
63+
with:
64+
context: ./backend
65+
push: false
66+
tags: ghcr.io/${{ github.repository }}/backend:pr-${{ github.event.pull_request.number }}
67+
cache-from: |
68+
type=registry,ref=ghcr.io/${{ github.repository }}/backend:cache
69+
type=registry,ref=ghcr.io/${{ github.repository }}/backend:latest
70+
cache-to: type=registry,ref=ghcr.io/${{ github.repository }}/backend:cache,mode=max
71+
72+
- name: Build frontend Docker image with cache
73+
uses: docker/build-push-action@v5
74+
with:
75+
context: ./frontend/nextjs-frontend
76+
push: false
77+
tags: ghcr.io/${{ github.repository }}/frontend:pr-${{ github.event.pull_request.number }}
78+
cache-from: |
79+
type=registry,ref=ghcr.io/${{ github.repository }}/frontend:cache
80+
type=registry,ref=ghcr.io/${{ github.repository }}/frontend:latest
81+
cache-to: type=registry,ref=ghcr.io/${{ github.repository }}/frontend:cache,mode=max

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ check:
2323

2424
.PHONY: docker-up
2525
docker-up:
26-
@docker compose -f docker-compose.yml up --build --wait
26+
@DOCKER_BUILDKIT=1 docker compose -f docker-compose.yml up --build --wait
2727

2828
.PHONY: docker-down
2929
docker-down:
3030
@docker compose -f docker-compose.yml down --remove-orphans
3131

3232
.PHONY: docker-dev
3333
docker-dev:
34-
@docker compose -f docker-compose.yml -f docker-compose.dev.yml up --build --wait
34+
@DOCKER_BUILDKIT=1 docker compose -f docker-compose.yml -f docker-compose.dev.yml up --build --wait
3535

3636
# --- Development Commands ---
3737
.PHONY: seed-credentials

backend/Dockerfile

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ FROM ghcr.io/astral-sh/uv:python3.13-bookworm-slim
22

33
WORKDIR /ORAssistant-backend
44

5+
# Install system dependencies (cacheable layer)
56
RUN apt-get update && apt-get install -y \
67
build-essential \
78
curl \
@@ -15,13 +16,22 @@ RUN apt-get update && apt-get install -y \
1516
git lfs install && \
1617
rm -rf /var/lib/apt/lists/*
1718

19+
# Install uv (cacheable layer)
1820
RUN pip install uv
1921

20-
COPY ./pyproject.toml /ORAssistant-backend/pyproject.toml
22+
# Copy only dependency files first for better caching
23+
COPY ./pyproject.toml ./uv.lock* /ORAssistant-backend/
24+
25+
# Install dependencies (cacheable layer - only rebuilds if dependencies change)
26+
RUN uv venv .venv && uv sync --dev
27+
28+
# Copy the rest of the application
2129
COPY . .
2230

23-
RUN uv venv .venv && uv sync --dev && uv run /ORAssistant-backend/src/post_install.py
31+
# Run post-install script
32+
RUN uv run /ORAssistant-backend/src/post_install.py
2433

34+
# Download dataset (cacheable layer)
2535
RUN git clone https://huggingface.co/datasets/The-OpenROAD-Project/ORAssistant_RAG_Dataset && \
2636
mkdir -p data && \
2737
mv ORAssistant_RAG_Dataset/* data/ && \

docker-compose.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ services:
1919
retries: 5
2020

2121
backend:
22+
image: ghcr.io/the-openroad-project/orassistant/backend:latest
2223
build:
2324
context: ./backend
2425
container_name: "backend"
@@ -42,8 +43,9 @@ services:
4243
timeout: ${HEALTHCHECK_TIMEOUT:-10s}
4344
retries: ${HEALTHCHECK_RETRIES:-5}
4445
start_period: ${HEALTHCHECK_START_PERIOD:-1200s}
45-
46+
4647
frontend:
48+
image: ghcr.io/the-openroad-project/orassistant/frontend:latest
4749
build:
4850
context: ./frontend/nextjs-frontend
4951
depends_on:

frontend/nextjs-frontend/Dockerfile

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# Install dependencies only when needed
22
FROM node:22-alpine AS deps
33
WORKDIR /app
4+
5+
# Copy package files (cacheable layer - only rebuilds if dependencies change)
46
COPY package.json package-lock.json* yarn.lock* ./
7+
8+
# Install dependencies
59
RUN \
610
if [ -f yarn.lock ]; then yarn install --frozen-lockfile; \
711
elif [ -f package-lock.json ]; then npm ci; \
@@ -10,21 +14,39 @@ RUN \
1014
# Rebuild the source code only when needed
1115
FROM node:22-alpine AS builder
1216
WORKDIR /app
17+
18+
# Copy dependencies from deps stage
1319
COPY --from=deps /app/node_modules ./node_modules
14-
COPY . .
20+
21+
# Copy only necessary source files
22+
COPY package.json ./
23+
COPY next.config.ts ./
24+
COPY tsconfig.json ./
25+
COPY tailwind.config.ts ./
26+
COPY postcss.config.mjs ./
27+
COPY app ./app
28+
COPY public ./public
29+
30+
# Build the application
1531
RUN npm run build
1632

1733
# Production image, copy all the files and run next
1834
FROM node:22-alpine AS runner
1935
WORKDIR /app
2036

21-
ENV NODE_ENV production
37+
ENV NODE_ENV=production
38+
39+
# Create a non-root user for security
40+
RUN addgroup --system --gid 1001 nodejs
41+
RUN adduser --system --uid 1001 nextjs
2242

2343
# Copy built assets from builder
24-
COPY --from=builder /app/public ./public
25-
COPY --from=builder /app/.next ./.next
26-
COPY --from=builder /app/node_modules ./node_modules
27-
COPY --from=builder /app/package.json ./package.json
44+
COPY --from=builder --chown=nextjs:nodejs /app/public ./public
45+
COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
46+
COPY --from=builder --chown=nextjs:nodejs /app/node_modules ./node_modules
47+
COPY --from=builder --chown=nextjs:nodejs /app/package.json ./package.json
48+
49+
USER nextjs
2850

2951
EXPOSE 3000
3052

0 commit comments

Comments
 (0)