Skip to content

Commit

Permalink
Merge pull request #1 from prefeitura-rio/staging
Browse files Browse the repository at this point in the history
feat: make it deployable!
  • Loading branch information
gabriel-milan authored Sep 5, 2024
2 parents 8767aeb + 9a11986 commit 51805f9
Show file tree
Hide file tree
Showing 10 changed files with 389 additions and 1 deletion.
36 changes: 36 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js
.yarn/install-state.gz

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
APP_URL="https://your-app.com"
67 changes: 67 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
FROM node:20-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 based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
RUN \
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
elif [ -f package-lock.json ]; then npm ci; \
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \
else echo "Lockfile not found." && exit 1; \
fi


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

# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry during the build.
# ENV NEXT_TELEMETRY_DISABLED 1

RUN \
if [ -f yarn.lock ]; then yarn run build; \
elif [ -f package-lock.json ]; then npm run build; \
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \
else echo "Lockfile not found." && exit 1; \
fi

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

ENV NODE_ENV production
# Uncomment the following line in case you want to disable telemetry during runtime.
# ENV NEXT_TELEMETRY_DISABLED 1

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

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

# Set the correct permission for prerender cache
RUN mkdir .next
RUN chown nextjs:nodejs .next

# 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

# server.js is created by next build from the standalone output
# https://nextjs.org/docs/pages/api-reference/next-config-js/output
CMD HOSTNAME="0.0.0.0" node server.js
40 changes: 40 additions & 0 deletions cloudbuild-prod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
steps:
# Create the .env file
- name: "gcr.io/cloud-builders/gcloud"
entrypoint: "bash"
args:
- "-c"
- |
echo "APP_URL=https://plataforma-clima.dados.rio" > .env.production
# Build the container image
- name: "gcr.io/cloud-builders/docker"
args: ["build", "-t", "gcr.io/$PROJECT_ID/plataforma-clima:$COMMIT_SHA", "."]
# Push the container image to Container Registry
- name: "gcr.io/cloud-builders/docker"
args: ["push", "gcr.io/$PROJECT_ID/plataforma-clima:$COMMIT_SHA"]
# Kustomize: set the image in the kustomization.yaml file
- name: "gcr.io/cloud-builders/gke-deploy"
dir: "k8s/prod"
entrypoint: "kustomize"
args:
- "edit"
- "set"
- "image"
- "gcr.io/project-id/plataforma-clima=gcr.io/$PROJECT_ID/plataforma-clima:$COMMIT_SHA"
# Kustomize: apply the kustomization.yaml file
- name: "gcr.io/cloud-builders/gke-deploy"
dir: "k8s/prod"
entrypoint: "kustomize"
args: ["build", ".", "-o", "prod.yaml"]
# Deploy the application to the GKE cluster
- name: "gcr.io/cloud-builders/gke-deploy"
dir: "k8s/prod"
args:
- "run"
- "--filename=prod.yaml"
- "--location=us-central1"
- "--cluster=datario"
- "--project=datario"

images:
- "gcr.io/$PROJECT_ID/plataforma-clima:$COMMIT_SHA"
40 changes: 40 additions & 0 deletions cloudbuild-staging.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
steps:
# Create the .env file
- name: "gcr.io/cloud-builders/gcloud"
entrypoint: "bash"
args:
- "-c"
- |
echo "APP_URL=https://staging.plataforma-clima.dados.rio" > .env.production
# Build the container image
- name: "gcr.io/cloud-builders/docker"
args: ["build", "-t", "gcr.io/$PROJECT_ID/plataforma-clima:$COMMIT_SHA", "."]
# Push the container image to Container Registry
- name: "gcr.io/cloud-builders/docker"
args: ["push", "gcr.io/$PROJECT_ID/plataforma-clima:$COMMIT_SHA"]
# Kustomize: set the image in the kustomization.yaml file
- name: "gcr.io/cloud-builders/gke-deploy"
dir: "k8s/staging"
entrypoint: "kustomize"
args:
- "edit"
- "set"
- "image"
- "gcr.io/project-id/plataforma-clima=gcr.io/$PROJECT_ID/plataforma-clima:$COMMIT_SHA"
# Kustomize: apply the kustomization.yaml file
- name: "gcr.io/cloud-builders/gke-deploy"
dir: "k8s/staging"
entrypoint: "kustomize"
args: ["build", ".", "-o", "staging.yaml"]
# Deploy the application to the GKE cluster
- name: "gcr.io/cloud-builders/gke-deploy"
dir: "k8s/staging"
args:
- "run"
- "--filename=staging.yaml"
- "--location=us-central1"
- "--cluster=datario"
- "--project=datario"

images:
- "gcr.io/$PROJECT_ID/plataforma-clima:$COMMIT_SHA"
5 changes: 5 additions & 0 deletions k8s/prod/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
- resources.yaml
96 changes: 96 additions & 0 deletions k8s/prod/resources.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: plataforma-clima-prod
namespace: plataforma-clima
spec:
replicas: 1
selector:
matchLabels:
app: plataforma-clima-prod
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
minReadySeconds: 5
template:
metadata:
labels:
app: plataforma-clima-prod
spec:
containers:
- name: plataforma-clima
image: gcr.io/project-id/plataforma-clima
resources:
requests:
cpu: 250m
memory: 1Gi
limits:
cpu: 250m
memory: 1Gi
livenessProbe:
httpGet:
path: /dashboard
port: 3000
initialDelaySeconds: 5
periodSeconds: 60
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 3
readinessProbe:
httpGet:
path: /dashboard
port: 3000
initialDelaySeconds: 5
periodSeconds: 60
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 3
restartPolicy: Always

---
# Service
apiVersion: v1
kind: Service
metadata:
labels:
app: plataforma-clima-prod
name: plataforma-clima-prod
namespace: plataforma-clima
spec:
ports:
- name: "http"
port: 80
targetPort: 3000
selector:
app: plataforma-clima-prod

---
# Ingress
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: plataforma-clima-prod
namespace: plataforma-clima
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /
cert-manager.io/cluster-issuer: "letsencrypt"
nginx.ingress.kubernetes.io/ssl-redirect: "true"
spec:
tls:
- hosts:
- plataforma-clima.dados.rio
secretName: plataforma-clima-prod-tls
rules:
- host: plataforma-clima.dados.rio
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: plataforma-clima-prod
port:
number: 80
5 changes: 5 additions & 0 deletions k8s/staging/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
- resources.yaml
Loading

0 comments on commit 51805f9

Please sign in to comment.