From 6ba71e1b1dd1f51bfa072abf44ed3957b89bec2b Mon Sep 17 00:00:00 2001 From: DongHyeonWon Date: Sat, 14 Sep 2024 18:25:39 +0900 Subject: [PATCH] test --- .github/workflows/backend-prod-cd.yml | 74 +++++++++++++++++++++++++++ backend/.dockerignore | 3 ++ backend/Dockerfile | 8 +++ backend/src/auth/auth.service.ts | 10 ---- backend/src/main.ts | 2 +- backend/src/user/user.controller.ts | 5 ++ 6 files changed, 91 insertions(+), 11 deletions(-) create mode 100644 .github/workflows/backend-prod-cd.yml create mode 100644 backend/.dockerignore create mode 100644 backend/Dockerfile diff --git a/.github/workflows/backend-prod-cd.yml b/.github/workflows/backend-prod-cd.yml new file mode 100644 index 0000000..a275494 --- /dev/null +++ b/.github/workflows/backend-prod-cd.yml @@ -0,0 +1,74 @@ +name: Siso - 밸런스 게임 벡엔드 배포 자동화 워크 플로우(backend-prod) + +on: + push: + branches: ["main"] + +permissions: + checks: write + +jobs: + detect-changes: + runs-on: ubuntu-latest + permissions: + pull-requests: read + outputs: + backend: ${{ steps.filter.outputs.backend }} # backend 변경 여부를 출력으로 설정 + frontend: ${{ steps.filter.outputs.frontend }} # frontend 변경 여부를 출력으로 설정 + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 # 모든 Git 히스토리를 가져옴 + - name: Get previous tag + id: previoustag + run: echo "PREVIOUS_TAG=$(git describe --tags --abbrev=0 HEAD^ --always)" >> $GITHUB_OUTPUT + # 이전 태그를 찾아서 환경 변수로 저장 + - uses: dorny/paths-filter@v3 + id: filter + with: + base: ${{ steps.previoustag.outputs.PREVIOUS_TAG }} # 이전 태그 기준 + ref: ${{ github.ref }} # 현재 GitHub 참조(커밋, 태그 등) + filters: | + backend: + - 'backend/**' + frontend: + - 'frontend/**' + + docker-deploy: + needs: detect-changes + if: ${{ needs.detect-changes.outputs.backend == 'true' }} + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Docker hub 로그인 + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Docker 이미지 빌드 및 푸시 + run: | + docker build -t ${{ secrets.DOCKER_USERNAME }}/siso-backend:latest ./backend + docker push ${{ secrets.DOCKER_USERNAME }}/siso-backend:latest + + ec2-deploy: + needs: docker-deploy + runs-on: ubuntu-latest + steps: + - name: EC2에 배포 + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.EC2_HOST }} + username: ${{ secrets.EC2_USERNAME }} + key: ${{ secrets.EC2_KEY }} + port: ${{ secrets.SSH_PORT }} + script: | + docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} + docker pull ${{ secrets.DOCKER_USERNAME }}/siso-backend:latest + docker stop siso-backend || true + docker rm siso-backend || true + docker run -d --env-file /home/ubuntu/env/backend.env -p 80:80 --name siso-backend ${{ secrets.DOCKER_USERNAME }}/siso-backend:latest diff --git a/backend/.dockerignore b/backend/.dockerignore new file mode 100644 index 0000000..fb0ff95 --- /dev/null +++ b/backend/.dockerignore @@ -0,0 +1,3 @@ +.git +*Dockerfile* +node_modules diff --git a/backend/Dockerfile b/backend/Dockerfile new file mode 100644 index 0000000..f1411b6 --- /dev/null +++ b/backend/Dockerfile @@ -0,0 +1,8 @@ +FROM node:18 +RUN mkdir -p /var/app +WORKDIR /var/app +COPY . . +RUN npm install +RUN npm run build +EXPOSE 80 +CMD [ "node", "dist/main.js" ] diff --git a/backend/src/auth/auth.service.ts b/backend/src/auth/auth.service.ts index c9a9c9a..92abbc4 100644 --- a/backend/src/auth/auth.service.ts +++ b/backend/src/auth/auth.service.ts @@ -39,14 +39,4 @@ export class AuthService { expiresIn: process.env.JWT_EXPIRES_IN, }); } - - // async generateRefreshToken(user: User): Promise { - // const payload = { - // userId: user.user_id, - // }; - - // const refreshToken = this.jwtService.sign(payload); - - // return refreshToken; - // } } diff --git a/backend/src/main.ts b/backend/src/main.ts index 6a66c97..2b1c105 100644 --- a/backend/src/main.ts +++ b/backend/src/main.ts @@ -4,7 +4,7 @@ import { Logger } from '@nestjs/common'; async function bootstrap() { const logger = new Logger(); - const port = 8080; + const port = 80; const app = await NestFactory.create(AppModule); await app.listen(port); logger.log(`Application is running on: ${port}`); diff --git a/backend/src/user/user.controller.ts b/backend/src/user/user.controller.ts index d28ac6a..6fe3d35 100644 --- a/backend/src/user/user.controller.ts +++ b/backend/src/user/user.controller.ts @@ -20,4 +20,9 @@ export class UserController { this.logger.log('Handling create user'); return this.userService.createUser(createUserDto); } + + @Get('/ping') + ping(): string { + return 'ping'; + } }