Skip to content

Commit

Permalink
Workflow integration (#7)
Browse files Browse the repository at this point in the history
* Create ci.yml

* lint

* formatting

* formatting the code

* Update ci.yml

* build workflow

* check dev env

* Update ci.yml

* workflows

* Update prod.yml

* testing

* test 2

* Update build.yml

* Update build.yml

* update comments

* Update ci.yml
  • Loading branch information
SAINIAbhishek authored Oct 4, 2024
1 parent 01723c7 commit 914d398
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 28 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Build Workflow

on:
workflow_call:
inputs:
environment:
description: 'development || production'
default: development
type: string
working-directory:
description: 'Directory where the build command will run'
type: string
default: './'

jobs:
build:
runs-on: ubuntu-latest
environment: ${{ inputs['environment'] }}
steps:
- name: Checkout Code
uses: actions/checkout@v3

- name: Restore Modules Cache
uses: actions/cache@v3
with:
path: node_modules
key: ${{ runner.os }}-auth-${{ hashFiles('package-lock.json') }}

- name: Build for ${{ inputs['environment'] }}
run: npm run build
working-directory: ${{ inputs['working-directory'] }}
env:
NODE_ENV: ${{ inputs['environment'] }}
PORT: ${{ vars.API_PORT }}
MONGO_URI: ${{ vars.MONGO_URI }}
MONGO_DB_HOST: ${{ vars.MONGO_DB_HOST }}
TOKEN_ISSUER: ${{ vars.TOKEN_ISSUER}}
TOKEN_AUDIENCE: ${{ vars.TOKEN_AUDIENCE}}
CORS_URL: ${{ vars.CORS_URL}}
FRONTEND_RESET_URL: ${{ vars.FRONTEND_RESET_URL}}
API_VERSION: ${{ vars.API_VERSION}}
ACCESS_TOKEN_SECRET_KEY: ${{ secrets.ACCESS_TOKEN_SECRET_KEY}}
MAILTRAP_TESTING_USERNAME: ${{ secrets.MAILTRAP_TESTING_USERNAME}}
MAILTRAP_TESTING_PASSWORD: ${{ secrets.MAILTRAP_TESTING_PASSWORD}}
MAILTRAP_USERNAME: ${{ secrets.MAILTRAP_USERNAME}}
MAILTRAP_PASSWORD: ${{ secrets.MAILTRAP_PASSWORD}}
MAILTRAP_TESTING_HOST: ${{ vars.MAILTRAP_TESTING_HOST}}
REFRESH_TOKEN_SECRET_KEY: ${{ secrets.REFRESH_TOKEN_SECRET_KEY}}
MAILTRAP_EMAIL_ENV: ${{ vars.MAILTRAP_EMAIL_ENV}}
74 changes: 74 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: Node Auth Application Pipeline

# Trigger the workflow
on:
push:
branches:
- '**' # Matches every branch for push events
pull_request:
branches:
- '**' # Matches every branch for pull request events

jobs:
# Step: Cache and Install Dependencies
npm:
uses: SAINIAbhishek/shared-workflows/.github/workflows/cache-install-dependencies.yml@main
with:
node-version: '20.17.0'
lock-file: 'package-lock.json'
cache-path: 'node_modules'
cache-key-prefix: 'auth'
working-directory: './'

# Step: Linting check
lint:
needs: [npm]
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3

- name: Restore Modules Cache
uses: actions/cache@v3
with:
path: node_modules
key: ${{ runner.os }}-auth-${{ hashFiles('package-lock.json') }}

# Lint the server code
- name: Checking Linting
run: npm run lint
working-directory: ./

# Step: Formatting check
format:
needs: [npm]
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3

- name: Restore Modules Cache
uses: actions/cache@v3
with:
path: node_modules
key: ${{ runner.os }}-auth-${{ hashFiles('package-lock.json') }}

- name: Checking Formatting with Prettier
run: npm run prettier
working-directory: ./

# Step: Security audit check
dependencies:
needs: [npm]
uses: SAINIAbhishek/shared-workflows/.github/workflows/security-audit.yml@main
with:
node-version: '20.17.0'
lock-file: 'package-lock.json'
cache-path: 'node_modules'
cache-key-prefix: 'auth'
working-directory: './'

# Step: Build check
dev:
needs: [lint, format, dependencies]
uses: ./.github/workflows/build.yml
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Node.js Authentication & Authorization Backend Typescript Project (Learning project)
# Node.js Authentication & Authorization Backend Typescript Project (Learning project) [![Node Auth Application Pipeline](https://github.com/SAINIAbhishek/nodejs_auth-api/actions/workflows/ci.yml/badge.svg)](https://github.com/SAINIAbhishek/nodejs_auth-api/actions/workflows/ci.yml)

This project is a scalable, well-structured Node.js backend built with TypeScript. It's designed to handle the complexity and scale of modern applications, while being optimized for different environments.

Expand Down
6 changes: 2 additions & 4 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ services:
build:
# This defines the build context for the image build
context: ./
args:
NODE_ENV: production
# This is the name we’ll use to refer to this image in Docker commands or to push to a Docker registry.
image: sainiabhishek/nodejs_auth-api:1.1.0
container_name: api
Expand All @@ -36,8 +34,8 @@ services:
environment:
MONGO_URI: mongodb://mongo:27017/
MONGO_DB_HOST: mongo
TOKEN_ISSUER: api.prod.saini.com
TOKEN_AUDIENCE: prod.saini.com
TOKEN_ISSUER: api.dev.saini.com
TOKEN_AUDIENCE: dev.saini.com
FRONTEND_RESET_URL: http://localhost:3000/
CORS_URL: http://localhost:3000
MAILTRAP_EMAIL_ENV: testing
Expand Down
4 changes: 0 additions & 4 deletions dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ FROM node:20.17.0-slim AS production
# Install curl
RUN apt-get update && apt-get install -y curl

# Argument and environment variable for production mode
ARG NODE_ENV
ENV NODE_ENV=${NODE_ENV}

# Set the working directory inside the final container
WORKDIR /app

Expand Down
2 changes: 1 addition & 1 deletion src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export const PORT = process.env.PORT;
export const CORS_URL = process.env.CORS_URL?.split(',') || [];
export const API_VERSION = process.env.API_VERSION;
export const FRONTEND_RESET_URL = process.env.FRONTEND_RESET_URL;
export const MAILTRAP_EMAIL_ENV = process.env.MAILTRAP_EMAIL_ENV || "testing";
export const MAILTRAP_EMAIL_ENV = process.env.MAILTRAP_EMAIL_ENV || 'testing';

export const DATE_FORMAT = process.env.DATE_FORMAT || 'yyyy-MM-dd';
export const DATE_FULL_FORMAT =
Expand Down
10 changes: 5 additions & 5 deletions src/controllers/HealthCheckController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import asyncHandler from 'express-async-handler';
import { SuccessResponse } from '../middleware/ApiResponse';

class HealthCheckController {

checkHealth = asyncHandler(async (req, res) => {
new SuccessResponse('The API is up and running. Health check is passed.', {}).send(res);
new SuccessResponse(
'The API is up and running. Health check is passed.',
{},
).send(res);
});

}


export default new HealthCheckController();
export default new HealthCheckController();
18 changes: 11 additions & 7 deletions src/helpers/EmailHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,28 @@ const emailFormatter = (content: string, firstname = '') => {
};

/**
* used to send emails.
* used to send emails.
*
* for sending test emails using the Nodemailer library and a service like
* Mailtrap for email delivery
* @param options
* @returns
* @param options
* @returns
*/
const emailTransporter = async (
options: Mail.Options,
): Promise<SMTPTransport.SentMessageInfo> => {
const isProdEnv = MAILTRAP_EMAIL_ENV === "production";
const isProdEnv = MAILTRAP_EMAIL_ENV === 'production';

const transporter = nodemailer.createTransport({
host: isProdEnv ? MAILTRAP_EMAIL.prod.host : MAILTRAP_EMAIL.testing.host,
port: isProdEnv ? MAILTRAP_EMAIL.prod.port : MAILTRAP_EMAIL.testing.port,
auth: {
user: isProdEnv ? MAILTRAP_EMAIL.prod.username : MAILTRAP_EMAIL.testing.username,
pass: isProdEnv ? MAILTRAP_EMAIL.prod.password : MAILTRAP_EMAIL.testing.password,
user: isProdEnv
? MAILTRAP_EMAIL.prod.username
: MAILTRAP_EMAIL.testing.username,
pass: isProdEnv
? MAILTRAP_EMAIL.prod.password
: MAILTRAP_EMAIL.testing.password,
},
});

Expand All @@ -50,5 +54,5 @@ const emailTransporter = async (

export default {
emailFormatter,
emailTransporter
emailTransporter,
};
8 changes: 2 additions & 6 deletions src/routes/v1/HealthRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ import HealthCheckController from '../../controllers/HealthCheckController';

const router = express.Router();

router
.route('/')
.get(
HealthCheckController.checkHealth
);
router.route('/').get(HealthCheckController.checkHealth);

export default router;
export default router;

0 comments on commit 914d398

Please sign in to comment.