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

Feat/dockerize app #47

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
26 changes: 26 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Node modules
node_modules
npm-debug.log
yarn-error.log

# Logs
logs
*.log
npm-debug.log*

# Environment variables
.env
.env.*

# Docker files
Dockerfile
docker-compose.yml
.dockerignore

# Git files
.git
.gitignore

# Others
dist
coverage
12 changes: 9 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
NODE_ENV=development

PORT=3000

DATABASE_URL="postgresql://postgres@localhost:5432/planifetsDB?schema=public"
# Database Configuration
DATABASE_URL="postgresql://postgres@localhost:5433/planifetsDB?schema=public"
POSTGRES_HOST="db"
POSTGRES_DB="planifetsDB"
POSTGRES_PASSWORD="postgres"
POSTGRES_USER="postgres"
POSTGRES_PORT=5433

LOG_LEVELS="log,error,warn,debug" # Log levels: "log,error,warn,debug,fatal,verbose"
# Logging Levels
LOG_LEVELS="log,error,warn,debug" # Options: log,error,warn,debug,fatal,verbose
85 changes: 85 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
name: CD

on:
push:
branches:
- main
tags:
- 'v*'
pull_request: # fixme: test purpose only
branches: ['main']

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build-and-test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.x]

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'yarn'
cache-dependency-path: 'yarn.lock'

- name: Install dependencies
run: yarn install --frozen-lockfile

- name: Run tests
run: yarn run test

- name: Build project
run: yarn run build

docker:
runs-on: ubuntu-latest
needs: [build-and-test]
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Docker meta
# if: github.event_name != 'pull_request'
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}

- name: Login to Registry
# if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
push: true # ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

semantic-release:
runs-on: ubuntu-latest
needs: [docker]
steps:
- name: Semantic Release
uses: cycjimmy/semantic-release-action@v4
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
11 changes: 6 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name: ci
name: CI

on:
push:
branches: [main]
Expand All @@ -13,7 +14,7 @@ jobs:
node-version: [18.x]

steps:
- name: Checkout to code
- name: Checkout code
uses: actions/checkout@v3

- name: Setup Node.js
Expand All @@ -26,11 +27,11 @@ jobs:
- name: Install dependencies
run: yarn install --frozen-lockfile

- name: lint
- name: Lint code
run: yarn run lint

- name: Run tests
run: yarn test
run: yarn run test

- name: Build
- name: Build project
run: yarn run build
43 changes: 43 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# syntax=docker/dockerfile:1

# Base dependencies
FROM node:18-alpine AS base
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install

# Development
FROM base AS development
WORKDIR /app
COPY . ./

RUN yarn install --frozen-lockfile && yarn global add @nestjs/cli && yarn prisma:generate

ENV NODE_ENV=development
# Required for hot-reloading
ENV CHOKIDAR_USEPOLLING=true

CMD [ "yarn", "start:dev" ]

# Build
FROM base AS build
WORKDIR /app
COPY . ./

RUN yarn build

# Production
FROM node:18-alpine AS production
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install --production --frozen-lockfile

COPY --from=build /app/dist ./dist
COPY prisma ./prisma
RUN yarn prisma:generate

ENV NODE_ENV=production

CMD [ "yarn", "start:prod" ]

EXPOSE 3000
6 changes: 6 additions & 0 deletions charts/planifets-backend/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v2
name: planifets-backend
description: A Helm chart for Kubernetes
type: application
version: 0.2.0
appVersion: '0.1.0'
56 changes: 56 additions & 0 deletions charts/planifets-backend/templates/app-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: "{{ .Release.Name }}-{{ .Chart.Name }}"
labels:
app: "{{ .Chart.Name }}"
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app: "{{ .Chart.Name }}"
template:
metadata:
labels:
app: "{{ .Chart.Name }}"
spec:
containers:

Check warning

Code scanning / SonarCloud

Service account permissions should be restricted Medium

Bind this resource's automounted service account to RBAC or disable automounting. See more on SonarCloud
- name: app

Check warning

Code scanning / SonarCloud

Memory limits should be enforced Medium

Specify a memory limit for this container. See more on SonarCloud

Check warning

Code scanning / SonarCloud

Storage limits should be enforced Medium

Specify a storage limit for this container. See more on SonarCloud
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
ports:
- containerPort: {{ .Values.service.port }}
env:
- name: DATABASE_URL
valueFrom:
secretKeyRef:
name: "{{ .Values.postgresSecretName }}"
key: DATABASE_URL
- name: NODE_ENV
value: production
- name: PORT
value: "{{ .Values.service.port }}"
livenessProbe:
httpGet:
path: /
port: {{ .Values.service.port }}
initialDelaySeconds: 15
periodSeconds: 20
readinessProbe:
httpGet:
path: /
port: {{ .Values.service.port }}
initialDelaySeconds: 5
periodSeconds: 10
---
apiVersion: v1
kind: Service
metadata:
name: "{{ .Release.Name }}-{{ .Chart.Name }}-service"
spec:
selector:
app: "{{ .Chart.Name }}"
ports:
- protocol: TCP
port: {{ .Values.service.port }}
targetPort: {{ .Values.service.port }}
type: {{ .Values.service.type }}
17 changes: 17 additions & 0 deletions charts/planifets-backend/templates/ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: "{{ .Release.Name }}-{{ .Chart.Name }}-ingress"
spec:
ingressClassName: nginx
rules:
- host: {{ (index .Values.ingress.hosts 0).host }}
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: "{{ .Release.Name }}-{{ .Chart.Name }}-service"
port:
number: {{ .Values.service.port }}
10 changes: 10 additions & 0 deletions charts/planifets-backend/templates/postgres-pvc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: postgres-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi # todo
10 changes: 10 additions & 0 deletions charts/planifets-backend/templates/postgres-secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: v1
kind: Secret
metadata:
name: {{ .Values.postgresSecretName }}
type: Opaque
stringData:
POSTGRES_USER: {{ .Values.postgres.user }}
POSTGRES_PASSWORD: {{ .Values.postgres.password }}
POSTGRES_DB: {{ .Values.postgres.db }}
DATABASE_URL: "postgresql://{{ .Values.postgres.user }}:{{ .Values.postgres.password }}@postgres:5432/{{ .Values.postgres.db }}?schema=public"
73 changes: 73 additions & 0 deletions charts/planifets-backend/templates/postgres.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: postgres
labels:
app: postgres
spec:
replicas: 1
selector:
matchLabels:
app: postgres
template:
metadata:
labels:
app: postgres
spec:
containers:

Check warning

Code scanning / SonarCloud

Service account permissions should be restricted Medium

Bind this resource's automounted service account to RBAC or disable automounting. See more on SonarCloud
- name: postgres

Check warning

Code scanning / SonarCloud

Memory limits should be enforced Medium

Specify a memory limit for this container. See more on SonarCloud

Check warning

Code scanning / SonarCloud

Storage limits should be enforced Medium

Specify a storage limit for this container. See more on SonarCloud
image: postgres:16
ports:
- containerPort: 5432
env:
- name: POSTGRES_USER
valueFrom:
secretKeyRef:
name: {{ .Values.postgresSecretName }}
key: POSTGRES_USER
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: {{ .Values.postgresSecretName }}
key: POSTGRES_PASSWORD
- name: POSTGRES_DB
valueFrom:
secretKeyRef:
name: {{ .Values.postgresSecretName }}
key: POSTGRES_DB
volumeMounts:
- name: postgres-storage
mountPath: /var/lib/postgresql/data
livenessProbe:
exec:
command:
- pg_isready
- -U
- {{ .Values.postgres.user }}
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
exec:
command:
- pg_isready
- -U
- {{ .Values.postgres.user }}
initialDelaySeconds: 5
periodSeconds: 10
volumes:
- name: postgres-storage
persistentVolumeClaim:
claimName: postgres-pvc
---
apiVersion: v1
kind: Service
metadata:
name: postgres
spec:
selector:
app: postgres
ports:
- port: 5432
targetPort: 5432
protocol: TCP
type: ClusterIP
Loading
Loading