From 269e13eae81693849003db3d227d221b2d96441e Mon Sep 17 00:00:00 2001
From: Dobromir Palushev <dobromir@peaksandpies.com>
Date: Fri, 2 Sep 2022 20:13:33 +0200
Subject: [PATCH] updated logic of the test-job

* updated the test stage in the app Dockerfile
---
 .gitlab-ci.yml |  8 ++++++--
 app/Dockerfile | 17 ++++++++++++-----
 2 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 457cf28..542876c 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -7,9 +7,10 @@ workflow:
 
 variables:
   DOCKER_VERSION: '20.10.17'
-  K8S_VERSION: '1.24'
-  GOOGLE_CLOUD_SDK_IMAGE_VERSION: '400.0.0'
+  MONGO_VERSION: '4.4'
   K8S_NAMESPACE: 'todoapp-devops'
+  GOOGLE_CLOUD_SDK_IMAGE_VERSION: '400.0.0'
+  GCP_COMPUTE_REGION: 'europe-west4a'
   CONTAINER_TAG: '${CI_REGISTRY_IMAGE}:${CI_PIPELINE_IID}-${CI_COMMIT_SHORT_SHA}'
 
 stages:
@@ -34,11 +35,14 @@ test-image:
   services:
     - name: docker:${DOCKER_VERSION}-dind
   script:
+    - docker run -p 27017:27017 -d mongo:${MONGO_VERSION}
     - docker build
       --tag "${CONTAINER_TAG}-test"
       --target=test
       "./app"
   after_script:
+    - docker stop mongo:${MONGO_VERSION} | xargs docker rm
+    - docker image rm --force mongo:${MONGO_VERSION}
     - docker image rm --force "${CONTAINER_TAG}-test"
 
 build-image:
diff --git a/app/Dockerfile b/app/Dockerfile
index e2085b5..6568c2b 100644
--- a/app/Dockerfile
+++ b/app/Dockerfile
@@ -5,11 +5,14 @@ ARG NODEJS_VERSION='16.17.0'
 
 FROM node:$NODEJS_VERSION-alpine AS base
 
+ARG \
+    MONGODB_URL_TEST=mongodb://host.docker.internal:27017/todo-app \
+    JWT_SECRET_TEST=<my-very-secret-jwt>
+
 ENV \
-    PORT=3000 \
-    # For local dev - mongodb://host.docker.internal:27017/todo-app
-    MONGODB_URL=<should-be-dynamically-set> \
-    JWT_SECRET=<should-be-dynamically-set>
+    PORT=<dynamically-set-from-k8s> \
+    MONGODB_URL=<dynamically-set-from-k8s> \
+    JWT_SECRET=<dynamically-set-from-k8s>
 
 # ------------------------------------------------------------------
 # Test stage
@@ -33,8 +36,12 @@ COPY ./server/package*.json .
 
 RUN npm ci --no-audit --no-fund
 
+ARG \
+    MONGODB_URL_TEST \
+    JWT_SECRET_TEST
+
 # MONGODB_URL and JWT_SECRET are available as env variables
-RUN npm run test
+RUN MONGODB_URL=$MONGODB_URL_TEST JWT_SECRET=$JWT_SECRET_TEST npm run test
 
 # ------------------------------------------------------------------
 # Build stage
-- 
GitLab