Skip to content

Commit 58c47b0

Browse files
committed
feat: add build and deploy action
1 parent 07c3d39 commit 58c47b0

File tree

3 files changed

+169
-0
lines changed

3 files changed

+169
-0
lines changed

.github/workflows/deploy.yml

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
name: Build and Deploy to Vercel
2+
3+
on:
4+
pull_request:
5+
types: [closed]
6+
branches: [main, master]
7+
jobs:
8+
build:
9+
if: github.event.pull_request.merged == true
10+
11+
name: "Build Application"
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout Code
16+
uses: actions/checkout@v4
17+
18+
- name: Setup Node.js
19+
uses: actions/setup-node@v4
20+
with:
21+
node-version: "20"
22+
23+
- name: Install pnpm
24+
uses: pnpm/action-setup@v2
25+
with:
26+
version: 8
27+
run_install: false
28+
29+
- name: Get pnpm store directory
30+
id: pnpm-cache
31+
shell: bash
32+
run: |
33+
echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT
34+
35+
- name: Setup pnpm cache
36+
uses: actions/cache@v3
37+
with:
38+
path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
39+
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
40+
restore-keys: |
41+
${{ runner.os }}-pnpm-store-
42+
43+
- name: Install dependencies
44+
run: pnpm install --frozen-lockfile
45+
46+
# Set up environment variables from GitHub secrets
47+
- name: Set up environment variables
48+
run: |
49+
# Create .env file
50+
touch .env
51+
# Add all secrets that start with "NEXT_PUBLIC_" to .env file
52+
for secret in $(compgen -A variable | grep NEXT_PUBLIC_); do
53+
echo "$secret=${!secret}" >> .env
54+
done
55+
56+
env:
57+
# Add other environment variables as needed
58+
CLERK_SECRET_KEY: ${{ secrets.CLERK_SECRET_KEY }}
59+
CONVEX_DEPLOYMENT: ${{ secrets.CONVEX_DEPLOYMENT }}
60+
61+
- name: Build
62+
run: pnpm build
63+
64+
# Upload the build artifacts to be used by the deploy job
65+
- name: Upload build artifacts
66+
uses: actions/upload-artifact@v3
67+
with:
68+
name: build-output
69+
path: |
70+
.next
71+
public
72+
package.json
73+
pnpm-lock.yaml
74+
next.config.js
75+
.env
76+
77+
deploy:
78+
name: Deploy Application to Vercel
79+
runs-on: ubuntu-latest
80+
needs: build
81+
environment: production
82+
steps:
83+
- name: Checkout Code
84+
uses: actions/checkout@v4
85+
86+
# Download build artifacts from previous job
87+
- name: Download build artifacts
88+
uses: actions/download-artifact@v3
89+
with:
90+
name: build-output
91+
92+
- name: Setup Node.js
93+
uses: actions/setup-node@v4
94+
with:
95+
node-version: "20"
96+
97+
- name: Install pnpm
98+
uses: pnpm/action-setup@v2
99+
with:
100+
version: 8
101+
run_install: false
102+
103+
# Install Vercel CLI
104+
- name: Install Vercel CLI
105+
run: npm install --global vercel@latest
106+
107+
# Deploy to Vercel
108+
- name: Deploy to Vercel Production
109+
run: vercel --token ${{ secrets.VERCEL_TOKEN }} --prod --confirm --env-file .env
110+
env:
111+
VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
112+
VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}

Dockerfile

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Use Node.js LTS as base image
2+
FROM node:20-alpine AS base
3+
4+
# Install pnpm globally
5+
RUN npm install -g pnpm
6+
7+
# Set working directory
8+
WORKDIR /app
9+
10+
# Install dependencies only when needed
11+
FROM base AS deps
12+
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine for understanding why libc6-compat might be needed
13+
RUN apk add --no-cache libc6-compat
14+
15+
# Copy package.json and pnpm-lock.yaml (if available)
16+
COPY package.json pnpm-lock.yaml* ./
17+
18+
# Install dependencies using pnpm
19+
RUN pnpm install --frozen-lockfile
20+
21+
# Build the app
22+
FROM base AS builder
23+
WORKDIR /app
24+
COPY --from=deps /app/node_modules ./node_modules
25+
COPY . .
26+
27+
# Build the application
28+
RUN pnpm build
29+
30+
# Production image, copy all the files and run next
31+
FROM base AS runner
32+
WORKDIR /app
33+
34+
ENV NODE_ENV production
35+
36+
# Create a non-root user and switch to it for better security
37+
RUN addgroup --system --gid 1001 nodejs &&
38+
adduser --system --uid 1001 nextjs &&
39+
chown -R nextjs:nodejs /app
40+
41+
# Copy the build output and necessary files
42+
COPY --from=builder /app/public ./public
43+
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
44+
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
45+
46+
# Switch to non-root user
47+
USER nextjs
48+
49+
# Expose the port the app will run on
50+
EXPOSE 3000
51+
52+
# Set the environment variable to use the standalone output
53+
ENV PORT 3000
54+
55+
# Start the application
56+
CMD ["node", "server.js"]

next.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { NextConfig } from "next";
22

33
const nextConfig: NextConfig = {
44
/* config options here */
5+
output: "standalone",
56
};
67

78
export default nextConfig;

0 commit comments

Comments
 (0)