Skip to content

Commit 5c2c8e4

Browse files
committed
24.10.29 authguard 수정 및 CI/CD
1 parent f357d79 commit 5c2c8e4

File tree

8 files changed

+156
-2
lines changed

8 files changed

+156
-2
lines changed

.github/workflows/dev.yml

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3+
4+
name: dev-deploy
5+
6+
on:
7+
push:
8+
branches: ['dev']
9+
10+
jobs:
11+
build:
12+
runs-on: ubuntu-latest
13+
14+
strategy:
15+
matrix:
16+
node-version: [20.16.0]
17+
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
18+
19+
steps:
20+
- name: Checkout source code.
21+
uses: actions/checkout@v2
22+
23+
- name: Use Node.js ${{ matrix.node-version }}
24+
uses: actions/setup-node@v2
25+
with:
26+
node-version: ${{ matrix.node-version }}
27+
28+
- name: Check Npm -v
29+
run: npm -v
30+
31+
- name: create env file
32+
working-directory: ./
33+
run: |
34+
echo "DB_HOST=${{secrets.DEV_DB_HOST}}" >> .env
35+
echo "DB_USER=${{ secrets.DB_USER }}" >> .env
36+
echo "DB_PASSWORD=${{ secrets.DB_PASSWORD }}" >> .env
37+
echo "DB_DATABASE=${{ secrets.DEV_DB_DATABASE }}" >> .env
38+
echo "JWT_SECRET=${{ secrets.JWT_SECRET }}" >> .env
39+
echo "JWT_EXPIRATION=${{ secrets.JWT_EXPIRATION }}" >> .env
40+
echo "KAKAO_ID=${{ secrets.KAKAO_ID }}" >> .env
41+
echo "KAKAO_SECRET=${{ secrets.KAKAO_SECRET }}" >> .env
42+
echo "KAKAO_REDIRECT=${{ secrets.DEV_KAKAO_REDIRECT }}" >> .env
43+
echo "NAVER_ID=${{ secrets.NAVER_ID }}" >> .env
44+
echo "NAVER_SECRET=${{ secrets.NAVER_SECRET }}" >> .env
45+
echo "NAVER_REDIRECT=${{ secrets.DEV_NAVER_REDIRECT }}" >> .env
46+
echo "PORT"=${{ secrets.DEV_PORT }} >> .env
47+
48+
- name: copy appspec.yml
49+
run: cp appspec/appspec-dev.yml appspec.yml
50+
51+
- name: copy scripts
52+
run: cp dev-scripts/after-deploy.yml scripts/after-deploy.yml
53+
54+
- name: build server files
55+
working-directory: ./
56+
run: |
57+
yarn install
58+
yarn run build
59+
60+
- name: zip file
61+
run: zip -r build.zip ./dist ./scripts ./appspec.yml ./.env ./package.json ./node_modules ./yarn.lock
62+
63+
- name: AWS configure credentials
64+
uses: aws-actions/configure-aws-credentials@v1
65+
with:
66+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
67+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
68+
aws-region: ap-northeast-2
69+
70+
- name: upload to S3
71+
run: aws s3 cp --region ap-northeast-2 ./build.zip s3://oodd-dev-api-bucket/
72+
73+
- name: deploy with AWS codeDeploy
74+
run: aws deploy create-deployment
75+
--application-name nestjs-app-dev
76+
--deployment-config-name CodeDeployDefault.OneAtATime
77+
--deployment-group-name nestjs-app-dev-group
78+
--s3-location bucket=oodd-dev-api-bucket,bundleType=zip,key=build.zip

appspec/appspec-dev.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
version: 0.0
2+
os: linux
3+
files:
4+
- source: /
5+
destination: /home/ubuntu/build-dev
6+
overwrite: yes
7+
file_exists_behavior: OVERWRITE
8+
permissions:
9+
- object: /home/ubuntu
10+
pattern: '**'
11+
owner: ubuntu
12+
group: ubuntu
13+
14+
hooks:
15+
AfterInstall:
16+
- location: scripts/after-deploy.sh
17+
timeout: 60
18+
runas: ubuntu

dev-scripts/after-deploy.sh

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
REPOSITORY=/home/ubuntu/build
3+
APP_NAME=node_app_dev
4+
5+
cd $REPOSITORY
6+
7+
# Check if the app is already running
8+
if sudo pm2 list | grep $APP_NAME > /dev/null
9+
then
10+
echo "$APP_NAME is already running. Restarting..."
11+
sudo pm2 delete $APP_NAME
12+
sudo pm2 start dist/app.js --name $APP_NAME
13+
else
14+
echo "Starting $APP_NAME"
15+
sudo pm2 start dist/app.js --name $APP_NAME
16+
fi

prod-scripts/after-deploy.sh

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
REPOSITORY=/home/ubuntu/build
3+
APP_NAME=node_app_prod
4+
5+
cd $REPOSITORY
6+
7+
# Check if the app is already running
8+
if sudo pm2 list | grep $APP_NAME > /dev/null
9+
then
10+
echo "$APP_NAME is already running. Restarting..."
11+
sudo pm2 delete $APP_NAME
12+
sudo pm2 start dist/app.js --name $APP_NAME
13+
else
14+
echo "Starting $APP_NAME"
15+
sudo pm2 start dist/app.js --name $APP_NAME
16+
fi

src/auth/auth.controller.ts

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { KakaoAuthGuard } from './guards/kakao.auth.guard';
88
import { BaseResponse } from 'src/common/response/dto';
99
import { LoginResponse } from './dto/auth.response';
1010
import { NaverAuthGuard } from './guards/naver.auth.guard';
11+
import { AuthGuard } from './guards/jwt.auth.guard';
1112

1213
@Controller('auth')
1314
@ApiTags('[서비스] Auth 관련')
@@ -48,4 +49,11 @@ export class AuthController {
4849
const jwtToken = await this.authService.socialLogin(socialUser, 'naver');
4950
return new BaseResponse<LoginResponse>(true, 'SUCCESS', { jwt: jwtToken });
5051
}
52+
53+
@UseGuards(AuthGuard)
54+
@Get('/test')
55+
async test(@Req() req: Request) {
56+
console.log(req.user);
57+
return req.user;
58+
}
5159
}

src/auth/auth.service.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,13 @@ export class AuthService {
2525
if (!userBySocial)
2626
return await this.userSerivce.createUserByKakaoOrNaver(user);
2727

28-
return await this.generateJwtToken(userBySocial);
28+
return await this.generateJwtToken({
29+
id: userBySocial.id,
30+
email: userBySocial.email,
31+
nickname: userBySocial.nickname,
32+
kakaoId: userBySocial.kakaoId ? userBySocial.kakaoId : undefined,
33+
naverId: userBySocial.naverId ? userBySocial.naverId : undefined,
34+
});
2935
}
3036

3137
async generateJwtToken(user: JwtPayload): Promise<string> {

src/auth/guards/jwt.auth.guard.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { ExecutionContext, Injectable } from '@nestjs/common';
2+
import { AuthGuard as NestAuthGuard } from '@nestjs/passport';
3+
import { Observable } from 'rxjs';
4+
5+
@Injectable()
6+
export class AuthGuard extends NestAuthGuard('jwt') {
7+
canActivate(
8+
context: ExecutionContext,
9+
): boolean | Promise<boolean> | Observable<boolean> {
10+
return super.canActivate(context);
11+
}
12+
}

src/main.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ async function bootstrap() {
77
const app = await NestFactory.create(AppModule);
88
app.useGlobalFilters(new ServiceExceptionToHttpExceptionFilter());
99
setupSwagger(app);
10-
await app.listen(8080);
10+
await app.listen(process.env.PORT);
1111
console.log(`Application is running on: ${await app.getUrl()}`);
1212
}
1313
bootstrap();

0 commit comments

Comments
 (0)