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

Fix issues in CI pipeline + Implement multi staged build in to two separated Images #55

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
86f9573
write seperate Dockerfiles for server and client
shakyapeiris Oct 13, 2021
a97d6b2
update docker compose according to changed images
shakyapeiris Oct 13, 2021
9fb7a51
delete previously added Dockerfile and .dockerignore file
shakyapeiris Oct 13, 2021
36b6eae
add tests and build steps to github workflows
shakyapeiris Oct 14, 2021
2f5cc93
create a helper for db connection
shakyapeiris Oct 14, 2021
17305d3
change filename since using jest
shakyapeiris Oct 14, 2021
6756dfb
update dependencies
shakyapeiris Oct 14, 2021
da0cc14
implement reusable db module
shakyapeiris Oct 14, 2021
907c62d
set the timeout of test script to 2min
shakyapeiris Oct 14, 2021
8a45aa1
install mongodb-memory-server and jest
shakyapeiris Oct 16, 2021
a0f76a3
remove NODE_ENV and ran a successful multistaged image build
shakyapeiris Oct 16, 2021
4b168aa
change paths to absolute paths
shakyapeiris Oct 16, 2021
ce73dbf
set to connect main db when NODE_ENV != test
shakyapeiris Oct 16, 2021
f7f91f1
seperate mock DB connection and main DB connection to seperate functi…
shakyapeiris Oct 16, 2021
77d4dca
write tests to test players in descending order and teams in descendi…
shakyapeiris Oct 16, 2021
47804dd
setup test script with detect open handles and force exit
shakyapeiris Oct 16, 2021
0738b02
change forwaded port of server to 3003
shakyapeiris Oct 16, 2021
7872fe0
build failed for the client Dockerfile during docker-compose up
shakyapeiris Oct 16, 2021
f3cfc33
Change mongodb url to an environment variable
shakyapeiris Oct 16, 2021
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
2 changes: 0 additions & 2 deletions .dockerignore

This file was deleted.

35 changes: 23 additions & 12 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ name: CI
# events but only for the master branch
on:
push:
branches: [ master ]
branches: [master]
pull_request:
branches: [ master ]
branches: [master]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
Expand All @@ -17,21 +17,32 @@ jobs:
# The type of runner that the job will run on
runs-on: ubuntu-latest
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2

- name: Install dependencies
working-directory: ./server
run: npm i
- name: Install dependencies
working-directory: ./server
run: npm i

- name: CI tests
working-directory: ./server
run: npm run test

build-client:
# The type of runner that the job will run on
runs-on: ubuntu-latest
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2

- name: Install dependencies
working-directory: ./client
run: npm i

- name: Install dependencies
working-directory: ./client
run: npm i
- name: CI tests
working-directory: ./client
run: npm run test

- name: Production build
working-director: ./client
run: npm run build
19 changes: 0 additions & 19 deletions Dockerfile

This file was deleted.

3 changes: 3 additions & 0 deletions client/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
Dockerfile
.dockerignore
29 changes: 29 additions & 0 deletions client/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# escape=`

# client build

FROM node:14 as dep

ENV NODE_ENV=production

WORKDIR /app

COPY package*.json .

RUN npm install

FROM node:14 as serve

ENV NODE_ENV=production

WORKDIR /app

COPY --from=dep /app/node_modules /node_modules

COPY . .

RUN npm install -g @angular/cli@8.0.4

EXPOSE 4200

CMD ["ng", "serve"]
10 changes: 7 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
version: '2'

services:
server:
build: .
client:
build: client/
restart: always
ports:
- "80:3003"
- '4200:4200'
server:
build: server/
ports:
- '3003:3003'
3 changes: 3 additions & 0 deletions server/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
Dockerfile
.dockerignore
21 changes: 21 additions & 0 deletions server/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# escape=`

FROM node:14 as dep

WORKDIR /app

COPY package*.json .

RUN npm install

FROM node:14 as prod

WORKDIR /app

COPY --from=dep app/node_modules node_modules

COPY . .

EXPOSE 3003

CMD ["node", "server.js"]
35 changes: 10 additions & 25 deletions server/app.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,17 @@
const express = require("express");
const leaderBoardRouter = require("./routes/leaderBoardRouter");
const syncRouter = require("./routes/syncRouter");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const expresValidator = require("express-validator");
const cors = require("cors");
const express = require('express');
const leaderBoardRouter = require('./routes/leaderBoardRouter');
const syncRouter = require('./routes/syncRouter');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const expresValidator = require('express-validator');
const cors = require('cors');
const mockgoose = require('mockgoose');
const db = require('./helpers/db');

if(process.env.NODE_ENV === 'test'){
const Mockgoose = mockgoose.Mockgoose;
const mock = new Mockgoose(mongoose);

mock.prepareStorage()
.then(() => {
mongoose.connect(process.env.MONGO_STR, {
useNewUrlParser: true
});
});

}else{
mongoose.connect(process.env.MONGO_STR, {
useNewUrlParser: true
});
}

if (process.env.NODE_ENV != 'test') db.connect();
const app = express();

app.use(express.static("../client/dist/walk2win"));
app.use(express.static('../client/dist/walk2win'));
app.use(bodyParser.json());

app.use(cors());
Expand Down
71 changes: 71 additions & 0 deletions server/helpers/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const mongoose = require('mongoose');
// const team = require('../models/team');
// const player = require('../models/player');

exports.connectMock = (teamArr, playerArr) => {
return new Promise((resolve, reject) => {
const { MongoMemoryServer } = require('mongodb-memory-server');
MongoMemoryServer.create()
.then((mongoServer) => {
return mongoose.connect(mongoServer.getUri(), {
useNewUrlParser: true,
useUnifiedTopology: true,
});
})
.then((client) => {
const team = client.model('Team');
const player = client.model('Player');
let selected = {
team: '',
player: '',
};
team.insertMany(teamArr).then((teams) => {
selected.team =
teams[Math.round(Math.random() * 100) % 3]._id.toString();
playerArr[0].team =
teams[Math.round(Math.random() * 100) % 3]._id.toString();
playerArr[1].team =
teams[Math.round(Math.random() * 100) % 3]._id.toString();
playerArr[2].team =
teams[Math.round(Math.random() * 100) % 3]._id.toString();
playerArr[3].team =
teams[Math.round(Math.random() * 100) % 3]._id.toString();
playerArr[4].team =
teams[Math.round(Math.random() * 100) % 3]._id.toString();
playerArr[5].team =
teams[Math.round(Math.random() * 100) % 3]._id.toString();
player.insertMany(playerArr).then((players) => {
selected.player =
players[Math.round(Math.random() * 100) % 6]._id.toString();
resolve(selected);
});
});
})
.catch((err) => {
reject(err);
});
});
};

exports.connect = () => {
return new Promise((resolve, reject) => {
mongoose
.connect(
process.env.MONGO_STR,
{
useNewUrlParser: true,
useUnifiedTopology: true,
}
)
.then(() => {
resolve();
})
.catch((err) => {
reject(err);
});
});
};

exports.close = () => {
mongoose.disconnect();
};
Loading