|
1 | | -FROM node:22-bookworm-slim AS development |
2 | | -# Install build tools |
3 | | -RUN apt-get update -y && apt-get install -y \ |
| 1 | +# Build stage |
| 2 | +FROM node:20-slim AS builder |
| 3 | + |
| 4 | +# Prevent interactive prompts during package installation |
| 5 | +ENV DEBIAN_FRONTEND=noninteractive |
| 6 | + |
| 7 | +# Install build dependencies |
| 8 | +RUN apt-get update && apt-get install -y \ |
| 9 | + build-essential \ |
4 | 10 | python3 \ |
5 | 11 | libvirt-dev \ |
6 | | - jq |
| 12 | + && rm -rf /var/lib/apt/lists/* |
| 13 | + |
| 14 | +# Set up the build environment |
| 15 | +WORKDIR /app |
| 16 | + |
| 17 | +# Copy package files first to leverage caching |
| 18 | +COPY package*.json ./ |
| 19 | +COPY pnpm-lock.yaml ./ |
7 | 20 |
|
8 | | -# Install pnpm globally using npm |
9 | | -RUN npm install -g pnpm@latest |
| 21 | +# Install pnpm and dependencies |
| 22 | +RUN npm install -g pnpm && \ |
| 23 | + pnpm install --frozen-lockfile |
10 | 24 |
|
| 25 | +# Copy source files |
| 26 | +COPY . . |
| 27 | + |
| 28 | +# Build the project |
| 29 | +RUN pnpm run build |
| 30 | + |
| 31 | +# Test stage |
| 32 | +FROM node:20-slim AS test |
| 33 | + |
| 34 | +# Prevent interactive prompts during package installation |
| 35 | +ENV DEBIAN_FRONTEND=noninteractive |
| 36 | + |
| 37 | +# Install test dependencies |
| 38 | +RUN apt-get update && apt-get install -y \ |
| 39 | + build-essential \ |
| 40 | + python3 \ |
| 41 | + qemu-system-x86 \ |
| 42 | + qemu-system-arm \ |
| 43 | + qemu-efi \ |
| 44 | + qemu-efi-aarch64 \ |
| 45 | + qemu-utils \ |
| 46 | + ovmf \ |
| 47 | + libvirt-daemon-system \ |
| 48 | + libvirt-dev \ |
| 49 | + && rm -rf /var/lib/apt/lists/* |
| 50 | + |
| 51 | +# Create a non-root user for libvirt and set up permissions |
| 52 | +RUN useradd -m -s /bin/bash -g libvirt libvirt && \ |
| 53 | + usermod -aG kvm libvirt && \ |
| 54 | + mkdir -p /var/run/libvirt && \ |
| 55 | + chown root:libvirt /var/run/libvirt && \ |
| 56 | + chmod g+w /var/run/libvirt |
| 57 | + |
| 58 | +# Set up the test environment |
11 | 59 | WORKDIR /app |
| 60 | + |
| 61 | +# Copy package files and install all dependencies (including dev) |
| 62 | +COPY package*.json ./ |
| 63 | +COPY pnpm-lock.yaml ./ |
| 64 | +RUN npm install -g pnpm && \ |
| 65 | + pnpm install --frozen-lockfile |
| 66 | + |
| 67 | +# Copy built files and test files from builder stage |
| 68 | +COPY --from=builder /app/dist ./dist |
| 69 | +COPY --from=builder /app/lib ./lib |
| 70 | +COPY --from=builder /app/__tests__ ./__tests__ |
| 71 | +COPY --from=builder /app/vitest.config.ts ./vitest.config.ts |
| 72 | +COPY --from=builder /app/build ./build |
| 73 | + |
| 74 | +# Set up libvirt configuration |
| 75 | +RUN mkdir -p /etc/libvirt && \ |
| 76 | + echo 'unix_sock_group = "libvirt"' >> /etc/libvirt/libvirtd.conf && \ |
| 77 | + echo 'unix_sock_rw_perms = "0770"' >> /etc/libvirt/libvirtd.conf && \ |
| 78 | + echo 'auth_unix_rw = "none"' >> /etc/libvirt/libvirtd.conf |
| 79 | + |
| 80 | +# Create necessary directories with correct permissions |
| 81 | +RUN mkdir -p /home/libvirt/.config/libvirt && \ |
| 82 | + echo 'uri_default = "qemu:///session"' > /home/libvirt/.config/libvirt/libvirt.conf && \ |
| 83 | + chown -R libvirt:libvirt /home/libvirt/.config && \ |
| 84 | + chown -R libvirt:libvirt /app |
| 85 | + |
| 86 | +# Switch to non-root user |
| 87 | +USER libvirt |
| 88 | + |
| 89 | +# Start libvirtd and run tests |
| 90 | +CMD ["/bin/sh", "-c", "/usr/sbin/libvirtd --daemon && sleep 2 && pnpm test:coverage"] |
0 commit comments