Skip to content

Commit

Permalink
Chore/ci-cd (#2) CI/CD 구축
Browse files Browse the repository at this point in the history
1. .github/workflows/* => github action
2. scripts/* => EC2 shell script (Code Deploy hook)
3. appSpec.yml => CodeDeploy 동작 정의
4. ecosystem.config.js => pm2 configure 파일
  • Loading branch information
cgh96 committed Jul 24, 2023
1 parent 7d772e8 commit 6ec3d60
Show file tree
Hide file tree
Showing 13 changed files with 142 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}
}
},
"ignorePatterns": ["next.config.js", "jest.*.js"],
"ignorePatterns": ["*.setup.js", "*.config.js"],
"extends": [
"next/core-web-vitals",
"plugin:@typescript-eslint/recommended",
Expand Down
57 changes: 57 additions & 0 deletions .github/workflows/Deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: deploy to S3

on:
push:
branches:
- main

env:
S3_BUCKET_NAME: chagok-bucket
CODE_DEPLOY_APPLICATION_NAME: chagok-code-deploy
CODE_DEPLOY_DEPLOYMENT_GROUP_NAME: chagok

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.x]

steps:
- name: Checkout
uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: "npm"

- name: Install dependencies
run: npm ci

# 프로젝트를 빌드한다.
- name: Build next app
run: npm run build

- name: Make zip file
run: zip -qq -r ./chagok.zip . -x "node_modules/*"
# -x "node_modules/*"

- 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

# Make zip file 단계에서 압축된 빌드 파일을 S3 버킷에 업로드하는 단계이다.
- name: Upload to S3
run: aws s3 cp --region ap-northeast-2 ./chagok.zip s3://$S3_BUCKET_NAME/chagok.zip

# S3에 업로드 된 빌드 파일을 이용해 CodeDeploy가 정의된 동작을 하도록 트리거
- name: Code Deploy
run: |
aws deploy create-deployment \
--application-name ${{ env.CODE_DEPLOY_APPLICATION_NAME }} \
--deployment-config-name CodeDeployDefault.AllAtOnce \
--deployment-group-name ${{ env.CODE_DEPLOY_DEPLOYMENT_GROUP_NAME }} \
--s3-location bucket=$S3_BUCKET_NAME,bundleType=zip,key=chagok.zip
27 changes: 27 additions & 0 deletions .github/workflows/Test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Node.js CI

on:
push:
branches-ignore:
- main

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build --if-present
- run: npm test
1 change: 0 additions & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npm run test
npx lint-staged
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.yml
*.sh
21 changes: 21 additions & 0 deletions appspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
version: 0.0
os: linux

files:
- source: /
destination: /home/ubuntu/chagok
overwrite: yes
permissions:
- object: /home/ubuntu/chagok
owner: ubuntu
group: ubuntu
mode: 755
hooks:
AfterInstall:
- location: scripts/after-install.sh
timeout: 120
runas: root
ApplicationStart:
- location: scripts/app-start.sh
timeout: 300
runas: root
22 changes: 22 additions & 0 deletions ecosystem.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = {
apps: [
{
name: "project",
script: "npm",
args: "run start",
instances: 2,
autorestart: true,
watch: false,
env: {
PORT: 3000,
NODE_ENV: "development",
},
env_production: {
PORT: 3000,
NODE_ENV: "production",
},
output: "./logs/console.log",
error: "./logs/consoleError.log",
},
],
};
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
"eslint-plugin-react-hooks": "^5.0.0-canary-7118f5dd7-20230705",
"eslint-plugin-simple-import-sort": "^10.0.0",
"eslint-plugin-unused-imports": "^3.0.0",
"husky": "^8.0.0",
"jest": "^29.6.1",
"jest-environment-jsdom": "^29.6.1",
"husky": "^8.0.3",
"lint-staged": "^13.2.3",
"typescript": "^4.9.5"
},
Expand Down
6 changes: 6 additions & 0 deletions scripts/after-install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
echo "> after install"

cd /home/ubuntu/chagok
sudo rm -r node_modules
npm install
npm run build
1 change: 1 addition & 0 deletions scripts/app-start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo "app start ;)"
3 changes: 2 additions & 1 deletion src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable react/no-unknown-property */
import { Inter } from "next/font/google";
import Head from "next/head";
import Image from "next/image";
Expand All @@ -15,7 +16,7 @@ export default function Home() {
<link rel="icon" href="/favicon.ico" />
</Head>
<main className={`${styles.main} ${inter.className}`}>
<div className={styles.description}>
<div className={styles.description} css={{ backgroundColor: "blue" }}>
<p>
Get started by editing&nbsp;
<code className={styles.code}>src/pages/index.tsx</code>
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
// "jsxImportSource": "@emotion/react",
"jsxImportSource": "@emotion/react",
"incremental": true,
"baseUrl": "./src"
},
Expand Down

0 comments on commit 6ec3d60

Please sign in to comment.