From b1224ed76c24a29b4626f3f2819a43048fa7daf2 Mon Sep 17 00:00:00 2001 From: Gsk54 Date: Tue, 10 Jan 2023 15:28:19 +0100 Subject: [PATCH] add ci --check --- .env.dist | 2 +- .gitlab-ci.yml | 24 ++++++++++++++++++++++++ ci/.env.ci | 15 +++++++++++++++ ci/Dockerfile | 29 +++++++++++++++++++++++++++++ ci/wait-up.sh | 12 ++++++++++++ docker-compose.ci.yml | 41 +++++++++++++++++++++++++++++++++++++++++ package.json | 23 +++++++++++++---------- 7 files changed, 135 insertions(+), 11 deletions(-) create mode 100644 .gitlab-ci.yml create mode 100644 ci/.env.ci create mode 100644 ci/Dockerfile create mode 100644 ci/wait-up.sh create mode 100644 docker-compose.ci.yml diff --git a/.env.dist b/.env.dist index bd44b1c..058bb27 100644 --- a/.env.dist +++ b/.env.dist @@ -3,7 +3,7 @@ SERVICE_URL=0.0.0.0 SERVICE_PORT=5002 # PRISMA -DATABASE_URL="postgresql://auth:auth@db:5432/auth?schema=public" +DATABASE_URL="postgresql://auth:auth@v3-auth-db:5432/auth?schema=public" # RABBIT MQ RMQ_URI=amqp://v3-broker:5672 diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..5610682 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,24 @@ +image: docker:20.10.22 + +stages: + - test + +############## +# TEST STAGE # +############## + +integration-test: + stage: test + image: docker/compose:latest + variables: + DOCKER_TLS_CERTDIR: "" + services: + - docker:dind + script: + - docker-compose -f docker-compose.ci.yml --env-file ci/.env.ci up -d + - sh ci/wait-up.sh + - docker exec -t v3-auth sh -c "npm run test:integration:ci" + coverage: /All files[^|]*\|[^|]*\s+([\d\.]+)/ + rules: + - if: '$CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH || $CI_COMMIT_MESSAGE =~ /--check/ || $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH' + when: always diff --git a/ci/.env.ci b/ci/.env.ci new file mode 100644 index 0000000..5a43d95 --- /dev/null +++ b/ci/.env.ci @@ -0,0 +1,15 @@ +# SERVICE +SERVICE_URL=0.0.0.0 +SERVICE_PORT=5002 + +# PRISMA +DATABASE_URL="postgresql://auth:auth@db:5432/auth?schema=public" + +# RABBIT MQ +RMQ_URI=amqp://broker:5672 + +# MESSAGE BROKER +BROKER_IMAGE=rabbitmq:3-alpine + +# POSTGRES +POSTGRES_IMAGE=postgres:15.0 diff --git a/ci/Dockerfile b/ci/Dockerfile new file mode 100644 index 0000000..4b2a20c --- /dev/null +++ b/ci/Dockerfile @@ -0,0 +1,29 @@ +################### +# BUILD FOR CI TESTING +################### + +FROM node:18-alpine3.16 + +# Create app directory +WORKDIR /usr/src/app + +# A wildcard is used to ensure both package.json AND package-lock.json are copied +COPY package*.json ./ + +# Install app dependencies +RUN npm ci + +# Bundle app source +COPY . . + +# Generate prisma client +RUN npx prisma generate + +# Create a "dist" folder +RUN npm run build + +# Run unit tests +RUN npm run test:unit:ci + +# Start the server +CMD [ "node", "dist/main.js" ] diff --git a/ci/wait-up.sh b/ci/wait-up.sh new file mode 100644 index 0000000..6b6d286 --- /dev/null +++ b/ci/wait-up.sh @@ -0,0 +1,12 @@ +#!/bin/bash +testlog() { + docker logs v3-auth-db-test | grep -q "database system is ready to accept connections" +} + +testlog 2> /dev/null +while [ $? -ne 0 ]; +do + sleep 5 + echo "Waiting for Test DB to be up..." + testlog 2> /dev/null +done diff --git a/docker-compose.ci.yml b/docker-compose.ci.yml new file mode 100644 index 0000000..bc1e38d --- /dev/null +++ b/docker-compose.ci.yml @@ -0,0 +1,41 @@ +version: '3.8' + +services: + api: + container_name: v3-auth + build: + dockerfile: ci/Dockerfile + context: . + env_file: + - ci/.env.ci + ports: + - 5002:5002 + depends_on: + - db + - broker + networks: + - v3-network + + db: + container_name: v3-auth-db-test + image: ${POSTGRES_IMAGE} + environment: + POSTGRES_DB: auth + POSTGRES_USER: auth + POSTGRES_PASSWORD: auth + ports: + - 5602:5432 + networks: + - v3-network + + broker: + container_name: v3-broker + image: ${BROKER_IMAGE} + ports: + - 5672:5672 + networks: + - v3-network + +networks: + v3-network: + name: v3-network diff --git a/package.json b/package.json index dc128b4..742d9d1 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { - "name": "auth", + "name": "mobicoop-v3-auth", "version": "0.0.1", - "description": "", - "author": "", + "description": "Mobicoop V3 Auth Service", + "author": "sbriat", "private": true, - "license": "UNLICENSED", + "license": "AGPL", "scripts": { "prebuild": "rimraf dist", "build": "nest build", @@ -14,13 +14,16 @@ "start:debug": "nest start --debug --watch", "start:prod": "node dist/main", "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", - "test": "npm run migrate-test && dotenv -e .env.test jest", - "test:unit": "jest --testPathIgnorePatterns 'integration' --verbose", - "test:integration": "npm run migrate-test && dotenv -e .env.test -- jest --testPathPattern 'integration' --verbose", - "test:cov": "npm run migrate-test && dotenv -e .env.test -- jest --coverage", + "test": "npm run migrate:test && dotenv -e .env.test jest", + "test:unit": "jest --testPathPattern 'tests/unit/' --verbose", + "test:unit:ci": "jest --testPathPattern 'tests/unit/' --coverage", + "test:integration": "npm run migrate:test && dotenv -e .env.test -- jest --testPathPattern 'tests/integration/' --verbose", + "test:integration:ci": "npm run migrate:test:ci && dotenv -e ci/.env.ci -- jest --testPathPattern 'tests/integration/'", + "test:cov": "npm run migrate:test && dotenv -e .env.test -- jest --coverage", "test:e2e": "jest --config ./test/jest-e2e.json", - "migrate": "docker exec v3-user sh -c 'npx prisma migrate dev'", - "migrate-test": "dotenv -e .env.test -- npx prisma migrate dev --name postgres-init" + "migrate": "docker exec v3-auth sh -c 'npx prisma migrate dev'", + "migrate:test": "dotenv -e .env.test -- npx prisma migrate deploy", + "migrate:test:ci": "dotenv -e ci/.env.ci -- npx prisma migrate deploy" }, "dependencies": { "@automapper/classes": "^8.7.7",