Skip to content

Commit

Permalink
refactor: 🔧 Migrate to use pnpm (#288)
Browse files Browse the repository at this point in the history
* refactor: 🔧 Migrate to use pnpm

* chore: 💚 Use pnpm instead of npx

* chore: ♻️ Update dockerfile to use pnpm, run actions on push

* chore: 💚 Update Docker image build
  • Loading branch information
edwinhern authored Sep 11, 2024
1 parent bf00242 commit 567c411
Show file tree
Hide file tree
Showing 11 changed files with 3,679 additions and 5,591 deletions.
19 changes: 7 additions & 12 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
name: Build

on:
push:
branches: ["main"]
pull_request:
branches: ["main"]
on: [push]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Use the latest stable Node.js
uses: actions/setup-node@v4
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with:
node-version: 'node'
cache: 'npm'
node-version-file: '.nvmrc'
cache: 'pnpm'

- name: Install Dependencies
run: npm ci
run: pnpm install

- name: Run Build
run: npm run build
run: pnpm run build
9 changes: 2 additions & 7 deletions .github/workflows/code-quality.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
name: Code Quality

on:
push:
branches: ["main"]
pull_request:
branches: ["main"]
on: [push]

jobs:
quality:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- uses: actions/checkout@v4
- name: Setup Biome
uses: biomejs/setup-biome@v2
with:
Expand Down
32 changes: 13 additions & 19 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
name: Docker Image CI

on:
push:
branches: ["main"]
pull_request:
branches: ["main"]
on: [push]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3

# Login step only executed for pushes to main
- name: Login to GitHub Container Registry
Expand All @@ -22,17 +19,14 @@ jobs:
username: ${{ github.actor }}
password: ${{ secrets.GH_Token }}

# Build and tag the production Docker image
- name: Build the production Docker image
run: |
docker build . --file Dockerfile --target runner --tag ghcr.io/${{ github.repository }}:${{ github.sha }}
if [ "${{ github.ref }}" == "refs/heads/main" ]; then
docker tag ghcr.io/${{ github.repository }}:${{ github.sha }} ghcr.io/${{ github.repository }}:latest
fi
# Push the production Docker image
- name: Push the production Docker image
if: github.ref == 'refs/heads/main'
run: |
docker push ghcr.io/${{ github.repository }}:${{ github.sha }}
docker push ghcr.io/${{ github.repository }}:latest
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: ${{ github.ref == 'refs/heads/main' }}
tags: |
ghcr.io/${{ github.repository }}:${{ github.sha }}
${{ github.ref == 'refs/heads/main' && 'ghcr.io/${{ github.repository }}:latest' || '' }}
cache-from: type=gha
cache-to: type=gha,mode=max
5 changes: 1 addition & 4 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx --no-install commitlint --edit "$1"
pnpm --no-install commitlint --edit "$1"
5 changes: 1 addition & 4 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx lint-staged
pnpm lint-staged
5 changes: 1 addition & 4 deletions .husky/pre-push
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm run build
pnpm build
51 changes: 34 additions & 17 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,34 +1,51 @@
# Stage 1: Building the app
# Build stage
FROM node:20-alpine AS builder
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable

# Set the working directory in the Docker container
WORKDIR /app

# Copy the package.json and package-lock.json (or yarn.lock) files
COPY package*.json ./
# Copy package.json and pnpm-lock.yaml
COPY package.json pnpm-lock.yaml* ./

# Install dependencies
RUN npm ci
# Install dependencies (including devDependencies)
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile

# Copy the rest of your app's source code from your host to your image filesystem.
# Copy all files
COPY . .

# Build the Next.js application
RUN npm run build
# Build the application
RUN pnpm run build

# Stage 3: Run the app in production mode
# Production stage
FROM node:20-alpine AS runner
WORKDIR /app

# Copy the build output from the builder stage
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

# Copy necessary files from the builder stage
COPY --from=builder /app/next.config.mjs ./
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static

# Set correct permissions for the .next directory
RUN chown nextjs:nodejs .next

# Switch to nextjs user
USER nextjs

# Create cache directory with correct permissions
RUN mkdir -p .next/cache

# Expose the port Next.js runs on
EXPOSE 3000

# Command to run the app
CMD ["npm", "start"]
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"

CMD ["node", "server.js"]
1 change: 1 addition & 0 deletions next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
output: "standalone",
experimental: {
optimizePackageImports: ["framer-motion"],
webVitalsAttribution: ["FCP", "LCP", "CLS", "FID", "TTFB", "INP"],
Expand Down
Loading

0 comments on commit 567c411

Please sign in to comment.