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

chore(BE): 백엔드 CI/CD 구축 #2

Merged
merged 10 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
38 changes: 38 additions & 0 deletions .github/workflows/backend-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: BE CI

on:
pull_request:
branches:
- '**'

jobs:
build:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./backend

steps:
- name: Git Checkout
uses: actions/checkout@v3

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version-file: .nvmrc
cache: yarn
cache-dependency-path: '**/yarn.lock'

- name: Cache dependencies
uses: actions/cache@v3
with:
path: ./node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-node-modules-

- name: Install Dependencies
run: yarn install --immutable

- name: Build
run: yarn build
91 changes: 91 additions & 0 deletions .github/workflows/backend-dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
name: BE Dev Deploy

on:
push:
branches:
- develop
paths:
- backend/**
- .github/**

jobs:
build:
env:
ENV_PATH: .env.staging
environment: staging
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./backend

steps:
- name: Git Checkout
uses: actions/checkout@v3

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version-file: .nvmrc
cache: yarn
cache-dependency-path: '**/yarn.lock'

- name: Cache dependencies
uses: actions/cache@v3
with:
path: ./node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-node-modules-

- name: Create .env file
run: |
touch ${{ env.ENV_PATH }}
echo DB_HOST=${{ secrets.DB_HOST }} >> ${{ env.ENV_PATH }}
echo DB_PORT=${{ secrets.DB_PORT }} >> ${{ env.ENV_PATH }}
echo DB_USERNAME=${{ secrets.DB_USERNAME }} >> ${{ env.ENV_PATH }}
echo DB_PASSWORD=${{ secrets.DB_PASSWORD }} >> ${{ env.ENV_PATH }}
echo DB_DATABASE=${{ secrets.DB_DATABASE }} >> ${{ env.ENV_PATH }}
echo REDIS_BINDING_PORT=${{ secrets.REDIS_BINDING_PORT }} >> ${{ env.ENV_PATH }}
echo REDIS_PORT=${{ secrets.REDIS_PORT }} >> ${{ env.ENV_PATH }}
echo REDIS_PASSWORD=${{ secrets.REDIS_PASSWORD }} >> ${{ env.ENV_PATH }}
cat ${{ env.ENV_PATH }}

- name: Install Dependencies
run: yarn install --immutable

- name: Build
run: yarn build

deploy:
needs: build
environment: staging
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./backend

steps:
- name: Copy appspec.yml
run:
cp ./appspec-dev.yml ./appspec.yml

- name: Make zip file
run: zip -qq -r ./dev.zip . -x "node_modules/*"
shell: bash

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-northeast-2

- name: Upload to S3
run: aws s3 cp --region ap-northeast-2 ./dev.zip s3://${{ secrets.AWS_S3_BUCKET_NAME }}/dev.zip

- name: Deploy to EC2 with CodeDeploy
run: aws deploy create-deployment
--deployment-config-name CodeDeployDefault.AllAtOnce
--application-name ${{ secrets.AWS_CODEDEPLOY_APPLICATION_NAME }}
--deployment-group-name ${{ secrets.AWS_CODEDEPLOY_DEPLOYMENT_GROUP_NAME }}
--s3-location bucket=${{ secrets.AWS_S3_BUCKET_NAME }},bundleType=zip,key=dev.zip
91 changes: 91 additions & 0 deletions .github/workflows/backend-prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
name: BE Prod Deploy

on:
push:
branches:
- main
paths:
- backend/**
- .github/**

jobs:
build:
env:
ENV_PATH: .env.production
environment: production
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./backend

steps:
- name: Git Checkout
uses: actions/checkout@v3

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version-file: .nvmrc
cache: yarn
cache-dependency-path: '**/yarn.lock'

- name: Cache dependencies
uses: actions/cache@v3
with:
path: ./node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-node-modules-

- name: Create .env file
run: |
touch ${{ env.ENV_PATH }}
echo DB_HOST=${{ secrets.DB_HOST }} >> ${{ env.ENV_PATH }}
echo DB_PORT=${{ secrets.DB_PORT }} >> ${{ env.ENV_PATH }}
echo DB_USERNAME=${{ secrets.DB_USERNAME }} >> ${{ env.ENV_PATH }}
echo DB_PASSWORD=${{ secrets.DB_PASSWORD }} >> ${{ env.ENV_PATH }}
echo DB_DATABASE=${{ secrets.DB_DATABASE }} >> ${{ env.ENV_PATH }}
echo REDIS_BINDING_PORT=${{ secrets.REDIS_BINDING_PORT }} >> ${{ env.ENV_PATH }}
echo REDIS_PORT=${{ secrets.REDIS_PORT }} >> ${{ env.ENV_PATH }}
echo REDIS_PASSWORD=${{ secrets.REDIS_PASSWORD }} >> ${{ env.ENV_PATH }}
cat ${{ env.ENV_PATH }}

- name: Install Dependencies
run: yarn install --immutable

- name: Build
run: yarn build

deploy:
needs: build
environment: production
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./backend

steps:
- name: Copy appspec.yml
run:
cp ./appspec-prod.yml ./appspec.yml

- name: Make zip file
run: zip -qq -r ./prod.zip . -x "node_modules/*"
shell: bash

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-northeast-2

- name: Upload to S3
run: aws s3 cp --region ap-northeast-2 ./prod.zip s3://${{ secrets.AWS_S3_BUCKET_NAME }}/prod.zip

- name: Deploy to EC2 with CodeDeploy
run: aws deploy create-deployment
--deployment-config-name CodeDeployDefault.AllAtOnce
--application-name ${{ secrets.AWS_CODEDEPLOY_APPLICATION_NAME }}
--deployment-group-name ${{ secrets.AWS_CODEDEPLOY_DEPLOYMENT_GROUP_NAME }}
--s3-location bucket=${{ secrets.AWS_S3_BUCKET_NAME }},bundleType=zip,key=prod.zip
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v20.13.1
2 changes: 2 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ lerna-debug.log*
.env.test.local
.env.production.local
.env.local
.env.staging
.env.production

# temp directory
.temp
Expand Down
20 changes: 20 additions & 0 deletions backend/appspec-dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
version: 0.0
os: linux
files:
- source: /
destination: /home/ubuntu/deploy/dev
overwrite: yes
file_exists_behavior: OVERWRITE

permissions:
- object: /home/ubuntu/deploy
pattern: '**'
owner: ubuntu
group: ubuntu
mode: 755

hooks:
AfterInstall:
- location: scripts/deploy-dev.sh
timeout: 600
runas: ubuntu
20 changes: 20 additions & 0 deletions backend/appspec-prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
version: 0.0
os: linux
files:
- source: /
destination: /home/ubuntu/deploy/prod
overwrite: yes
file_exists_behavior: OVERWRITE

permissions:
- object: /home/ubuntu/deploy
pattern: '**'
owner: ubuntu
group: ubuntu
mode: 755

hooks:
AfterInstall:
- location: scripts/deploy-prod.sh
timeout: 600
runas: ubuntu
5 changes: 3 additions & 2 deletions backend/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ version: '3.8'

services:
redis:
image: redis
image: redis:latest
restart: always
ports:
- "32769:6379"
- "${REDIS_BINDING_PORT}:${REDIS_PORT}"
command: redis-server --requirepass ${REDIS_PASSWORD}
8 changes: 7 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"start:staging": "NODE_ENV=staging pm2 start dist/main",
"start:prod": "NODE_ENV=production pm2 start dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
Expand Down Expand Up @@ -84,5 +85,10 @@
"prettier --write",
"eslint --fix --max-warnings=0"
]
},
"engines": {
"node": ">=20",
"yarn": ">=1.22",
"npm": "please-use-yarn"
}
}
6 changes: 6 additions & 0 deletions backend/scripts/deploy-dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
REPOSITORY=/home/ubuntu/deploy/dev

cd $REPOSITORY

yarn install
yarn start:staging
6 changes: 6 additions & 0 deletions backend/scripts/deploy-prod.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
REPOSITORY=/home/ubuntu/deploy/prod

cd $REPOSITORY

yarn install
yarn start:prod
8 changes: 7 additions & 1 deletion backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import { ConfigModule } from '@nestjs/config';
@Module({
imports: [
ConfigModule.forRoot({
envFilePath: '.env',
envFilePath: [
process.env.NODE_ENV === 'production'
? '.env.production'
: process.env.NODE_ENV === 'staging'
? '.env.staging'
: '.env.local',
],
isGlobal: true,
}),
TypeOrmModule.forRoot({
Expand Down
2 changes: 1 addition & 1 deletion backend/src/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
getHello(): string {
return 'Hello World!';
return `Hello World! ${process.env.NODE_ENV}`;
}
}
Loading