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

add skaffold #6

Open
wants to merge 4 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
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@
**/values.dev.yaml
**/build
**/dist
.nx/
LICENSE
README.md
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ Session.vim
### windows
Desktop.ini
$RECYCLE.BIN/
Thumbs.db
Thumbs.db

## commonly used temp files and directories
*.log
Expand All @@ -213,4 +213,6 @@ TAGS
.vercel

# nx
.nx
.nx

infrastructure/kustomize/postgres/base/charts
33 changes: 33 additions & 0 deletions apps/api/Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# syntax=docker/dockerfile:1

ARG NODE_VERSION=18.17.0
ARG PNPM_VERSION=8.9.2

################################################################################
# Use node image for base image for all stages.
FROM node:${NODE_VERSION}-alpine as base

# Set working directory for all build stages.
WORKDIR /usr/src/app

# Install pnpm.
RUN --mount=type=cache,target=/root/.npm \
npm install -g pnpm@${PNPM_VERSION}

RUN --mount=type=bind,source=package.json,target=package.json \
--mount=type=bind,source=pnpm-lock.yaml,target=pnpm-lock.yaml \
--mount=type=cache,target=/root/.local/share/pnpm/store \
pnpm install --frozen-lockfile

COPY . .

# Generate the Prisma bindings.
RUN pnpm prisma:generate

FROM base as final

# Expose the ports that the application listens on.
EXPOSE 3100 9100

# Run the application.
CMD ["pnpm", "dev:api"]
33 changes: 33 additions & 0 deletions apps/www/Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# syntax=docker/dockerfile:1

ARG NODE_VERSION=18.17.0
ARG PNPM_VERSION=8.9.2

################################################################################
# Use node image for base image for all stages.
FROM node:${NODE_VERSION}-alpine as base

# Set working directory for all build stages.
WORKDIR /usr/src/app

# Install pnpm.
RUN --mount=type=cache,target=/root/.npm \
npm install -g pnpm@${PNPM_VERSION}

RUN --mount=type=bind,source=package.json,target=package.json \
--mount=type=bind,source=pnpm-lock.yaml,target=pnpm-lock.yaml \
--mount=type=cache,target=/root/.local/share/pnpm/store \
pnpm install --frozen-lockfile

COPY . .

# Generate the Prisma bindings.
RUN pnpm prisma:generate

FROM base as final

# Expose the ports that the application listens on.
EXPOSE 4200

# Run the application.
CMD ["pnpm", "dev:www", "--", "--hostname=0.0.0.0"]
20 changes: 20 additions & 0 deletions infrastructure/kustomize/api/base/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: api
spec:
selector:
matchLabels:
run: api
replicas: 1
template:
metadata:
labels:
run: api
spec:
containers:
- name: api
image: api
ports:
- containerPort: 3100

3 changes: 3 additions & 0 deletions infrastructure/kustomize/api/base/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resources:
- deployment.yaml
- service.yaml
13 changes: 13 additions & 0 deletions infrastructure/kustomize/api/base/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: v1
kind: Service
metadata:
name: api
labels:
run: api
spec:
ports:
- port: 3100
protocol: TCP
targetPort: 3100
selector:
run: api
20 changes: 20 additions & 0 deletions infrastructure/kustomize/api/local/ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: api
annotations:
kubernetes.io/ingress.class: nginx
spec:
rules:
# Replace with your domain
# You can use sslip.io for testing
- host: api.k8s.orb.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: api
port:
number: 3100
21 changes: 21 additions & 0 deletions infrastructure/kustomize/api/local/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
resources:
- ../../postgres/base
- ../base
- ingress.yaml
namePrefix: local-

configMapGenerator:
- name: api-configmap
literals:
- DATABASE_URL=postgresql://postgres:postgres@local-postgresql:5432/postgres

patches:
- patch: |-
- op: add
path: "/spec/template/spec/containers/0/envFrom"
value:
- configMapRef:
name: local-api-configmap
target:
kind: Deployment
name: api
12 changes: 12 additions & 0 deletions infrastructure/kustomize/postgres/base/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
helmCharts:
- name: postgresql
valuesInline:
global:
postgresql:
auth:
postgresPassword: postgres
username: postgres
password: postgres
database: postgres
releaseName: postgresql
repo: https://charts.bitnami.com/bitnami
19 changes: 19 additions & 0 deletions infrastructure/kustomize/www/base/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: www
spec:
selector:
matchLabels:
run: www
replicas: 1
template:
metadata:
labels:
run: www
spec:
containers:
- name: www
image: www
ports:
- containerPort: 3000
3 changes: 3 additions & 0 deletions infrastructure/kustomize/www/base/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resources:
- deployment.yaml
- service.yaml
13 changes: 13 additions & 0 deletions infrastructure/kustomize/www/base/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: v1
kind: Service
metadata:
name: www
labels:
run: www
spec:
ports:
- port: 3000
protocol: TCP
targetPort: 3000
selector:
run: www
20 changes: 20 additions & 0 deletions infrastructure/kustomize/www/local/ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: www
annotations:
kubernetes.io/ingress.class: nginx
spec:
rules:
# Replace with your domain
# You can use sslip.io for testing
- host: www.k8s.orb.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: www
port:
number: 3000
21 changes: 21 additions & 0 deletions infrastructure/kustomize/www/local/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
resources:
- ../base
- ingress.yaml
namePrefix: local-

patches:
- patch: |-
- op: replace
path: "/spec/ports/0/targetPort"
value: 4200
target:
kind: Service
name: www

- patch: |-
- op: replace
path: "/spec/template/spec/containers/0/ports/0/containerPort"
value: 4200
target:
kind: Deployment
name: www
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"build:www": "nx run www:build",
"build:docker:api": "docker build -f apps/api/Dockerfile .",
"build:docker:www": "docker build -f apps/www/Dockerfile .",
"dev": "skaffold dev --port-forward",
"dev:api": "nx run api:serve:development --watch=true",
"dev:www": "nx run www:serve:development --watch=true",
"docker:dev": "docker compose -f docker-compose.base.yaml -f docker-compose.dev.yaml",
Expand Down
15 changes: 13 additions & 2 deletions packages/database/src/lib/prisma.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import { Injectable, OnModuleInit } from '@nestjs/common';
import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';

@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
private readonly logger = new Logger(PrismaService.name);

async onModuleInit() {
await this.$connect();
for (let i = 0; i < 10; i++) {
try {
await this.$connect();
return;
} catch (e) {
this.logger.error(`Failed to connect to database (${i})`);
this.logger.error(e);
await new Promise((r) => setTimeout(r, 1000));
}
}
}
}
44 changes: 44 additions & 0 deletions skaffold.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
apiVersion: skaffold/v4beta9
kind: Config
metadata:
name: nx-next-nest-prisma-ory-template
build:
artifacts:
- image: api
context: .
docker:
dockerfile: apps/api/Dockerfile.dev
sync:
manual:
# sync src files since we are running a dev server
- src: 'apps/api/*'
dest: /usr/src/app
- src: 'packages/**/*'
dest: /usr/src/app

- image: www
context: .
docker:
dockerfile: apps/www/Dockerfile.dev
sync:
manual:
# sync src files since we are running a dev server
- src: 'apps/www/*'
dest: /usr/src/app
- src: 'packages/**/*'
dest: /usr/src/app
local:
useBuildkit: true
push: false

deploy:
statusCheckDeadlineSeconds: 30
tolerateFailuresUntilDeadline: true

manifests:
kustomize:
paths:
- infrastructure/kustomize/api/local
- infrastructure/kustomize/www/local
buildArgs:
- "--enable-helm"