Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare deploy sample implementation (public repo) #6

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
.next/
55 changes: 55 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
FROM node:18-alpine AS base

# Install dependencies only when needed
FROM base AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app

# Install dependencies
COPY package.json package-lock.json ./
RUN npm ci


# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

ENV NEXT_TELEMETRY_DISABLED 1

RUN npm run build

# Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app

ARG APP_ENV
ARG GIGS_PROJECT
ARG GIGS_API_KEY

ENV APP_ENV=$APP_ENV
3nvi marked this conversation as resolved.
Show resolved Hide resolved
ENV GIGS_PROJECT=$GIGS_PROJECT
ENV GIGS_API_KEY=$GIGS_API_KEY

ENV NODE_ENV production
ENV NEXT_TELEMETRY_DISABLED 1

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

COPY --from=builder /app/public ./public

# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

USER nextjs

EXPOSE 3000

ENV PORT 3000

CMD ["node", "server.js"]
3 changes: 3 additions & 0 deletions app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Metadata } from 'next'
import { Inter } from 'next/font/google'
import './globals.css'
import { injectPrivateEnvVars } from '@/lib/utils'

const inter = Inter({ subsets: ['latin'] })

Expand All @@ -9,6 +10,8 @@ export const metadata: Metadata = {
description: 'By Gigs',
}

injectPrivateEnvVars()

export default function RootLayout({
children,
}: {
Expand Down
6 changes: 0 additions & 6 deletions lib/schemas/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,3 @@ const validSubscriptionStatuses = ['pending', 'active', 'ended'] as const

type SubscriptionStatus = ElementOf<typeof validSubscriptionStatuses>

type Coverage = {
object: 'coverage'
id: string
name: string
countries: string[]
}
10 changes: 10 additions & 0 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,13 @@ export const envVarsPresent = () => {

return !!gigsProject && !!gigsAPIKey
}

const privateEnvVars = {
APP_ENV: process.env.APP_ENV,
GIGS_PROJECT: process.env.GIGS_PROJECT,
gigsanna marked this conversation as resolved.
Show resolved Hide resolved
GIGS_API_KEY: process.env.GIGS_API_KEY,
}

export function injectPrivateEnvVars() {
Object.assign(process.env, privateEnvVars)
}
Comment on lines +20 to +28
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really understand what this does and why we need it 🙈

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I answered my question myself by missing to add this in the Tracking PR in the API Docs 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally missed that question 😅 It's to ensure that relevant env variables are accessible when the project is built and running somewhere (or at least that's my understanding)

1 change: 1 addition & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const nextConfig = {
]
: []
},
output: 'standalone',
}

module.exports = nextConfig
Loading