Skip to content

Commit

Permalink
feat/CI-CD-to-run-tests-on-merge (#38)
Browse files Browse the repository at this point in the history
* feat(CI): add first version of backend CI to run tests

* feat(CI/CD): add entrypoint scripts for backend docker

* fix(backend.yaml): wait for services to be healthy
  • Loading branch information
alexis-moins authored Nov 20, 2023
1 parent 05f4e63 commit f148482
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 102 deletions.
12 changes: 5 additions & 7 deletions .env.local
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ FRONTEND_PORT=3000
ADMINER_PORT=8080
ADMINER_DESIGN=lucas-sandery

MYSQL_ROOT_PASSWORD=root
POSTGRES_USER=john
POSTGRES_PASSWORD=doe

MYSQL_USER=john
MYSQL_PASSWORD=doe

# Needs root privileges to manage DB
DATABASE_URL=mysql://root:root@database:3306/count_of_money
TEST_DATABASE_URL=mysql://root:root@database:3306/count_of_money_test
# Note: change user:password to match the fields above!
DATABASE_URL=postgresql://john:doe@database:5432/count_of_money?schema=public
TEST_DATABASE_URL=postgresql://john:doe@database:5432/count_of_money_test?schema=public
25 changes: 7 additions & 18 deletions .github/workflows/backend.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs

name: Node.js CI
name: Backend tests

on:
push:
Expand All @@ -12,21 +9,13 @@ on:

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build --if-present
- run: npm test

- name: Set environment variables
run: cp .env.local .env

- name: Start tests
run: docker compose up test
7 changes: 4 additions & 3 deletions back/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ FROM node:18-alpine

WORKDIR /app

COPY entrypoint.sh .
COPY test-entrypoint.sh .

COPY package.json .
COPY package-lock.json .

RUN npm clean-install

COPY . .

RUN npx prisma generate

EXPOSE 3000

# Enable file watching inside the container (hot-reload)
ENV CHOKIDAR_USEPOLLING=true

CMD npm run dev
ENTRYPOINT [ "/app/entrypoint.sh" ]
8 changes: 8 additions & 0 deletions back/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh

set -ex

npx prisma generate
npx prisma migrate deploy

npm run dev
2 changes: 1 addition & 1 deletion back/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"license": "MIT",
"scripts": {
"dev": "tsx watch src/index.ts",
"test": "jest",
"test": "jest --passWithNoTests",
"lint": "biome lint src/",
"format": "biome format --write src/"
},
Expand Down

This file was deleted.

50 changes: 0 additions & 50 deletions back/prisma/migrations/20231113152055_add_lucia_auth/migration.sql

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
-- CreateTable
CREATE TABLE "User" (
"id" TEXT NOT NULL,

CONSTRAINT "User_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Session" (
"id" TEXT NOT NULL,
"user_id" TEXT NOT NULL,
"active_expires" BIGINT NOT NULL,
"idle_expires" BIGINT NOT NULL,

CONSTRAINT "Session_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Key" (
"id" TEXT NOT NULL,
"hashed_password" TEXT,
"user_id" TEXT NOT NULL,

CONSTRAINT "Key_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "User_id_key" ON "User"("id");

-- CreateIndex
CREATE UNIQUE INDEX "Session_id_key" ON "Session"("id");

-- CreateIndex
CREATE INDEX "Session_user_id_idx" ON "Session"("user_id");

-- CreateIndex
CREATE UNIQUE INDEX "Key_id_key" ON "Key"("id");

-- CreateIndex
CREATE INDEX "Key_user_id_idx" ON "Key"("user_id");

-- AddForeignKey
ALTER TABLE "Session" ADD CONSTRAINT "Session_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Key" ADD CONSTRAINT "Key_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
2 changes: 1 addition & 1 deletion back/prisma/migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "mysql"
provider = "postgresql"
2 changes: 1 addition & 1 deletion back/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ generator client {
}

datasource db {
provider = "mysql"
provider = "postgresql"
url = env("DATABASE_URL")
}

Expand Down
8 changes: 8 additions & 0 deletions back/test-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh

set -ex

npx prisma generate
npx prisma migrate deploy

npm run test
25 changes: 13 additions & 12 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ version: "3.9"
services:
back:
build: ./back
container_name: back

container_name: back
restart: always

environment:
DATABASE_URL: ${DATABASE_URL}

Expand All @@ -15,25 +16,27 @@ services:
volumes:
- ./back/src:/app/src
- ./back/prisma:/app/prisma
- ./back/package.json:/app/package.json

depends_on:
- database
- database

test:
build: ./back
container_name: test

entrypoint: npm run test
container_name: test
entrypoint: /app/test-entrypoint.sh

environment:
DATABASE_URL: ${TEST_DATABASE_URL}

volumes:
- ./back/src:/app/src
- ./back/prisma:/app/prisma
- ./back/package.json:/app/package.json

depends_on:
- database
- database

adminer:
image: adminer
Expand All @@ -51,20 +54,18 @@ services:
- database:db

database:
image: mysql:8.0
image: postgres:16-alpine
container_name: database

restart: always

environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: count_of_money

MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
POSTGRES_DB: count_of_money
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}

volumes:
- database-data:/var/lib/mysql
- database-data:/var/lib/postgresql/data

volumes:
database-data:
6 changes: 6 additions & 0 deletions scripts/test-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

set -ex

npx prisma migrate dev
npm run test

0 comments on commit f148482

Please sign in to comment.