Skip to content

Commit

Permalink
Merge branch 'dev' into Lanky-23-patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
Lanky-23 authored Aug 14, 2024
2 parents e53d24c + b7d4468 commit 3017815
Show file tree
Hide file tree
Showing 12 changed files with 220 additions and 69 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
name: CI

on:
push:
branches: [dev, staging, main]
pull_request:
branches:
- dev
types: [opened, synchronize, reopened]
branches: [dev, staging, main]

jobs:
test:
Expand All @@ -25,5 +27,6 @@ jobs:
run: yarn test
env:
CI: true
- name: buld the dist

- name: Build the dist
run: yarn build
81 changes: 51 additions & 30 deletions .github/workflows/dev.yml
Original file line number Diff line number Diff line change
@@ -1,40 +1,61 @@
name: Build, Test, and Deploy for Dev Branch
name: Deploy to Dev

on:
push:
branches:
- dev
workflow_run:
workflows: [CI]
types:
- completed
branches: [dev]

jobs:
build:
runs-on: self-hosted
defaults:
run:
working-directory: /var/www/aihomework/dev

deploy:
runs-on: ubuntu-latest
steps:
- name: Pull from github
id: pull
run: |
git stash
git pull origin dev
- name: install dependencies
run: yarn install
- name: Checkout repository
uses: actions/checkout@v4

# - name: Install sshpass
# run: sudo apt-get install sshpass

- name: Run Test
run: yarn test

- name: buld the dist
run: yarn build
- name: Fetch .env file from server
run: |
sshpass -p ${{ secrets.PASSWORD }} scp -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }}:/var/www/aihomework/dev/.env .env
env:
SSH_HOST: ${{ secrets.HOST }}
SSH_USERNAME: ${{ secrets.USERNAME }}
SSH_PASSWORD: ${{ secrets.PASSWORD }}

- name: migrate
run: yarn reset-db
- name: Build Docker images
run: |
docker compose --env-file .env -f docker-compose.yml build
- name: setup service file
run: sudo cp server-script/aihomeworkdev.service /etc/systemd/system
- name: List Docker images
run: docker images

- name: start the app
- name: Save Docker images to tarball
run: |
sudo systemctl daemon-reload
sudo systemctl restart aihomeworkdev
docker save hng_boilerplate_expressjs-backend:latest | gzip > dev-images.tar.gz
- name: Copy Docker images to server
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
source: "dev-images.tar.gz"
target: "~/images-tar"

- name: Deploy to server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
script: |
cd ~/dev-deployment/hng_boilerplate_expressjs
git stash
git checkout dev
git pull
docker load -i ~/images-tar/dev-images.tar.gz
docker compose -f docker-compose.yml down
docker compose -f docker-compose.yml up -d
4 changes: 1 addition & 3 deletions .github/workflows/pr-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ jobs:
name: preview
url: ${{ steps.deploy.outputs.preview-url }}
runs-on: ubuntu-latest

steps:
- name: Checkout to branch
- name: Checkout the branch
uses: actions/checkout@v4

- id: deploy
name: Pull Request Deploy
uses: hngprojects/pr-deploy@main-patch

with:
server_host: ${{ secrets.SERVER_HOST }}
server_username: ${{ secrets.SERVER_USERNAME }}
Expand Down
87 changes: 63 additions & 24 deletions .github/workflows/prod.yml
Original file line number Diff line number Diff line change
@@ -1,35 +1,74 @@
name: Build, Test, and Deploy for Prod Branch
name: Deploy to Prod

on:
push:
branches: [main]
workflow_run:
workflows: [CI]
types:
- completed
branches: [prod]

jobs:
build:
runs-on: self-hosted
defaults:
run:
working-directory: /var/www/aihomework/prod

on-success:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
steps:
- name: Pull from GitHub
- name: Checkout repository
uses: actions/checkout@v4

- name: Install sshpass
run: sudo apt-get install sshpass

- name: Fetch .env file from server
run: |
sshpass -p ${{ secrets.PASSWORD }} scp -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }}:~/prod-deployment/hng_boilerplate_expressjs/.env .env
env:
SSH_HOST: ${{ secrets.HOST }}
SSH_USERNAME: ${{ secrets.USERNAME }}
SSH_PASSWORD: ${{ secrets.PASSWORD }}

- name: Build Docker images
run: |
git stash
git pull origin main
docker compose --env-file .env -f docker-compose.production.yml build
- name: Install dependencies
run: yarn install
- name: List Docker images
run: docker images

- name: Build the dist
run: yarn build
- name: Save Docker images to tarball
run: |
docker save hng_boilerplate_expressjs-backend_prod:latest | gzip > prod-images.tar.gz
- name: migrate
run: yarn migrate
- name: Upload artifact
uses: actions/upload-artifact@v2
with:
name: prod-images
path: prod-images.tar.gz

- name: Setup service file
run: sudo cp server-script/aihomeworkprod.service /etc/systemd/system
- name: Copy Docker images to server
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
source: "prod-images.tar.gz"
target: "~/images-tar"

- name: Start the app
run: |
sudo systemctl daemon-reload
sudo systemctl restart aihomeworkprod
- name: Deploy to server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
script: |
cd ~/prod-deployment/hng_boilerplate_expressjs
git stash
git checkout prod
git pull
docker load -i ~/images-tar/prod-images.tar.gz
docker compose -f docker-compose.production.yml down
docker compose -f docker-compose.production.yml up -d
on-failure:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'failure' }}
steps:
- run: echo "CI Workflow failed. Prod deployment was not triggered."
58 changes: 58 additions & 0 deletions .github/workflows/staging.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: Deploy to staging

on:
workflow_run:
workflows: [CI]
types:
- completed
branches: [staging]

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Fetch .env file from server
run: |
sshpass -p ${{ secrets.PASSWORD }} scp -o StrictHostKeyChecking=no ${{ secrets.USERNAME }}@${{ secrets.HOST }}:~/staging-deployment/hng_boilerplate_expressjs/.env .env
env:
SSH_HOST: ${{ secrets.HOST }}
SSH_USERNAME: ${{ secrets.USERNAME }}
SSH_PASSWORD: ${{ secrets.PASSWORD }}

- name: Build Docker images
run: |
docker compose --env-file .env -f docker-compose.staging.yml build
- name: List Docker images
run: docker images

- name: Save Docker images to tarball
run: |
docker save hng_boilerplate_expressjs-backend_staging:latest | gzip > staging-images.tar.gz
- name: Copy Docker images to server
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
source: "staging-images.tar.gz"
target: "~/images-tar"

- name: Deploy to server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
script: |
cd ~/staging-deployment/hng_boilerplate_expressjs
git stash
git checkout staging
git pull
docker load -i ~/images-tar/staging-images.tar.gz
docker compose -f docker-compose.staging.yml down
docker compose -f docker-compose.staging.yml up -d
5 changes: 2 additions & 3 deletions docker-compose.production.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
services:
backend:
backend_prod:
container_name: backend_prod
build:
context: .
args:
- NODE_ENV=${NODE_ENV}
ports:
- 4444:8000
env_file:
- .env
environment:
NODE_ENV: production
DB_HOST: backend_db_prod
DATABASE_URL: postgres://${DB_USER}:${DB_PASSWORD}@backend_db_prod:5432/${DB_NAME}
networks:
Expand Down
5 changes: 2 additions & 3 deletions docker-compose.staging.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
services:
backend:
backend_staging:
container_name: backend_staging
build:
context: .
args:
- NODE_ENV=${NODE_ENV}
ports:
- 3333:8000
env_file:
- .env
environment:
NODE_ENV: staging
DB_HOST: backend_db_staging
DATABASE_URL: postgres://${DB_USER}:${DB_PASSWORD}@backend_db_staging:5432/${DB_NAME}
networks:
Expand Down
5 changes: 3 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
version: "3.8"
services:
backend:
build:
context: .
args:
- NODE_ENV=${NODE_ENV}
ports:
- 2222:8000
env_file:
- .env
environment:
NODE_ENV: development
DB_HOST: backend_db
DATABASE_URL: postgres:// ${DB_USER}:${DB_PASSWORD}@backend_db:5432/${DB_NAME}
networks:
- app
Expand Down
13 changes: 13 additions & 0 deletions server-script/aihomeworkstaging.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[Unit]
Description=AIHomework-Dev
After=network.target

[Service]
WorkingDirectory=/var/www/aihomework/staging
ExecStart=/bin/bash /var/www/aihomework/dev/server-script/startappstaging.sh
#Restart=on-failure
#RestartSec=20s
StartLimitInterval=0

[Install]
WantedBy=multi-user.target
5 changes: 5 additions & 0 deletions server-script/startappstaging.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

cd /var/www/aihomework/staging/
mkdir -p logs
/usr/bin/yarn start >> logs/stagingoutput.log 2>&1
14 changes: 13 additions & 1 deletion src/models/faq.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { Entity, PrimaryGeneratedColumn, Column } from "typeorm";
import {
Entity,
PrimaryGeneratedColumn,
Column,
CreateDateColumn,
UpdateDateColumn,
} from "typeorm";
import ExtendedBaseEntity from "./extended-base-entity";
import { UserRole } from "../enums/userRoles";

Expand All @@ -18,6 +24,12 @@ class FAQ extends ExtendedBaseEntity {

@Column({ nullable: false, default: UserRole.SUPER_ADMIN })
createdBy: string;

@CreateDateColumn()
created_at: Date;

@UpdateDateColumn()
updated_at: Date;
}

export { FAQ };
3 changes: 3 additions & 0 deletions src/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ export class User extends ExtendedBaseEntity {
@Column({ nullable: true })
otp: number;

@Column({ default: false })
is_superadmin: boolean;

@Column({ nullable: true })
otp_expires_at: Date;

Expand Down

0 comments on commit 3017815

Please sign in to comment.