Skip to content

Commit

Permalink
Add CI/CD
Browse files Browse the repository at this point in the history
  • Loading branch information
henriqueleite42 committed Jan 5, 2024
1 parent c43f8ce commit 2790b0f
Show file tree
Hide file tree
Showing 16 changed files with 142 additions and 15 deletions.
4 changes: 1 addition & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ NODE_ENV=dev
AWS_ENDPOINT=http://localstack:4566
AWS_ACCESS_KEY_ID=test
AWS_SECRET_ACCESS_KEY=test
AWS_DEFAULT_REGION=us-east-1
AWS_REGION=us-east-1

GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=

JWT_SECRET=foo

DATABASE_URL=postgresql://username:password@postgres:5432/database?schema=public

NOTIFICATIONS_EMAIL=[email protected]
49 changes: 49 additions & 0 deletions .github/workflows/api-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: CI/CD Pipeline

on:
push:
branches:
- master

jobs:
build-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: '20.x'

- name: Create .env file
uses: SpicyPizza/[email protected]
with:
file_name: .env.production
fail_on_empty: false
sort_keys: false
envkey_PORT: ${{ secrets.PORT }}
envkey_NODE_ENV: ${{ secrets.NODE_ENV }}
envkey_AWS_REGION: ${{ secrets.AWS_REGION }}
envkey_GOOGLE_CLIENT_ID: ${{ secrets.GOOGLE_CLIENT_ID }}
envkey_GOOGLE_CLIENT_SECRET: ${{ secrets.GOOGLE_CLIENT_SECRET }}
envkey_JWT_SECRET: ${{ secrets.JWT_SECRET }}
envkey_DATABASE_URL: ${{ secrets.DATABASE_URL }}

- name: Build
run: |
yarn install --ignore-scripts
yarn build
- name: AWS CodeBuild & CodeDeploy
uses: sourcetoad/aws-codedeploy-action@v1
with:
aws_access_key: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws_secret_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws_region: ${{ secrets.AWS_REGION }}
codedeploy_name: Econominhas
codedeploy_group: development_gropup
s3_bucket: econominhas-dev-api-deploy
s3_folder: api
max_polling_iterations: 60
directory: dist
2 changes: 1 addition & 1 deletion .github/workflows/dbdocs-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v4

- name: Use Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: '20.x'

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/dbdocs-validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v4

- name: Use Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: '20.x'

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/openapi-validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v4

- name: Use Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: '20.x'

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/prisma-validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v4

- name: Use Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: '20.x'

Expand Down
26 changes: 26 additions & 0 deletions appspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
version: 0.0
os: linux
files:
- source: /
destination: /home/ubuntu/econominhas
file_exists_behavior: OVERWRITE
hooks:
ApplicationStop:
- location: scripts/cd-stop.sh
timeout: 300
runas: ubuntu

AfterInstall:
- location: scripts/cd-prepare.sh
timeout: 300
runas: ubuntu

ApplicationStart:
- location: scripts/cd-start.sh
timeout: 300
runas: ubuntu

ValidateService:
- location: scripts/cd-validate.sh
timeout: 300
runas: ubuntu
11 changes: 11 additions & 0 deletions scripts/build.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#!/bin/bash

# Generate prisma client
npx prisma generate --generator client

# Build
npx nest build --builder webpack

# Create necessary folders
mkdir dist/prisma
mkdir dist/scripts

# Copy env files
cp .env.production dist/.env
Expand All @@ -16,3 +20,10 @@ cp yarn.lock dist/yarn.lock
# Copy database files
cp -r prisma/migrations dist/prisma/migrations
cp prisma/schema.prisma dist/prisma/schema.prisma

# Copy deploy files
cp appspec.yml dist/appspec.yml
cp scripts/cd-prepare.sh dist/scripts/cd-prepare.sh
cp scripts/cd-start.sh dist/scripts/cd-start.sh
cp scripts/cd-stop.sh dist/scripts/cd-stop.sh
cp scripts/cd-validate.sh dist/scripts/cd-validate.sh
14 changes: 14 additions & 0 deletions scripts/cd-prepare.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion

# Go to project folder
cd /home/ubuntu/econominhas

# Install dependencies
sudo yarn install --production=true --frozen-lockfile --ignore-scripts

# Generate prisma client
sudo npx prisma generate --generator client
8 changes: 8 additions & 0 deletions scripts/cd-start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion

# Start new build
pm2 start "yarn start:prod" --name "ECONOMINHAS"
11 changes: 11 additions & 0 deletions scripts/cd-stop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion

# Stop previous build (if running)
if pm2 status "ECONOMINHAS" | grep -q 'ECONOMINHAS';
then
pm2 delete "ECONOMINHAS"
fi
14 changes: 14 additions & 0 deletions scripts/cd-validate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion

# Validate service
if pm2 status "ECONOMINHAS" | grep -q 'online';
then
exit 0
else
pm2 status "ECONOMINHAS"
exit 1
fi
2 changes: 1 addition & 1 deletion src/adapters/implementations/s3/s3.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class S3AdapterService extends FileAdapter {

this.client = new S3Client({
endpoint: this.config.get('AWS_ENDPOINT'),
region: this.config.get('AWS_DEFAULT_REGION'),
region: this.config.get('AWS_REGION'),
credentials: {
secretAccessKey: this.config.get('AWS_SECRET_ACCESS_KEY'),
accessKeyId: this.config.get('AWS_ACCESS_KEY_ID'),
Expand Down
2 changes: 1 addition & 1 deletion src/adapters/implementations/ses/ses.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class SESAdapterService extends EmailAdapter {

this.client = new SESClient({
endpoint: this.config.get('AWS_ENDPOINT'),
region: this.config.get('AWS_DEFAULT_REGION'),
region: this.config.get('AWS_REGION'),
credentials: {
secretAccessKey: this.config.get('AWS_SECRET_ACCESS_KEY'),
accessKeyId: this.config.get('AWS_ACCESS_KEY_ID'),
Expand Down
2 changes: 1 addition & 1 deletion src/adapters/implementations/sns/sns.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class SNSAdapterService extends SmsAdapter {

this.client = new SNSClient({
endpoint: this.config.get('AWS_ENDPOINT'),
region: this.config.get('AWS_DEFAULT_REGION'),
region: this.config.get('AWS_REGION'),
credentials: {
secretAccessKey: this.config.get('AWS_SECRET_ACCESS_KEY'),
accessKeyId: this.config.get('AWS_ACCESS_KEY_ID'),
Expand Down
6 changes: 1 addition & 5 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { ConfigService } from '@nestjs/config';
import { Transform, plainToInstance } from 'class-transformer';
import {
IsEmail,
IsIn,
IsInt,
IsOptional,
Expand All @@ -28,7 +27,7 @@ class EnvVars {
@IsString()
AWS_SECRET_ACCESS_KEY: string;
@IsIn(['us-east-1'])
AWS_DEFAULT_REGION: string;
AWS_REGION: string;

@IsString()
GOOGLE_CLIENT_ID: string;
Expand All @@ -40,9 +39,6 @@ class EnvVars {

@IsString()
DATABASE_URL: string;

@IsEmail()
NOTIFICATIONS_EMAIL: string;
}

export type AppConfig = ConfigService<EnvVars>;
Expand Down

0 comments on commit 2790b0f

Please sign in to comment.