diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..611e786 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,8 @@ +node_modules +.dockerignore +.gitignore +.prettierrc +cloudbuild.yaml +Dockerfile +k8s.yaml +README.md diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..14bd373 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,26 @@ +FROM node:14-alpine AS build + +WORKDIR /usr/src/app + +# Install dependencies +COPY ["package.json", "yarn.lock", "./"] +RUN yarn + +# Build the app +COPY . . +RUN yarn build + +FROM node:14-alpine + +ENV NODE_ENV production +WORKDIR /usr/src/app + +RUN apk add postgresql-client + +COPY ["package.json", "yarn.lock", "./"] +RUN yarn + +COPY --from=build /usr/src/app/dist ./dist + +EXPOSE 3000 +CMD yarn start:prod diff --git a/cloudbuild.yaml b/cloudbuild.yaml new file mode 100644 index 0000000..2929a1c --- /dev/null +++ b/cloudbuild.yaml @@ -0,0 +1,16 @@ +steps: + # build the container image + - name: 'gcr.io/kaniko-project/executor:latest' + args: + - --destination=gcr.io/rnkm63/backend:$BRANCH_NAME + - --cache=true + - --cache-ttl=336h + # deploy container image to GKE + - name: 'gcr.io/cloud-builders/gke-deploy' + args: + - run + - --filename=k8s.yaml + - --image=gcr.io/rnkm63/backend:$BRANCH_NAME + - --location=asia-southeast1-b + - --cluster=rnkm63 + - --namespace=dev diff --git a/k8s.yaml b/k8s.yaml new file mode 100644 index 0000000..7e312f3 --- /dev/null +++ b/k8s.yaml @@ -0,0 +1,41 @@ +--- +apiVersion: "apps/v1" +kind: "Deployment" +metadata: + name: "rnkm63-backend" + namespace: "default" + labels: + app: "rnkm63-backend" +spec: + replicas: 1 + selector: + matchLabels: + app: "rnkm63-backend" + template: + metadata: + labels: + app: "rnkm63-backend" + spec: + containers: + - name: "backend-sha256-1" + image: "gcr.io/rnkm63/backend:latest" +--- +apiVersion: "autoscaling/v2beta1" +kind: "HorizontalPodAutoscaler" +metadata: + name: "rnkm63-backend-hpa-yptr" + namespace: "default" + labels: + app: "rnkm63-backend" +spec: + scaleTargetRef: + kind: "Deployment" + name: "rnkm63-backend" + apiVersion: "apps/v1" + minReplicas: 1 + maxReplicas: 5 + metrics: + - type: "Resource" + resource: + name: "cpu" + targetAverageUtilization: 80 diff --git a/src/main.ts b/src/main.ts index 13cad38..f133a65 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,6 +3,7 @@ import { AppModule } from './app.module'; async function bootstrap() { const app = await NestFactory.create(AppModule); + app.setGlobalPrefix('api') await app.listen(3000); } bootstrap();