Skip to content

Commit

Permalink
feat: 자동 배포를 위한 파일 생성
Browse files Browse the repository at this point in the history
ci/cd의 yml에서 cd부분을 추가했습니다. ncp의 container registry를 사용하며 이미지가 push가 완료되면 action의 메시지를 통해 pull을 받습니다.
이미지를 받은 후, DockerFile을 실행하게 됩니다.
  • Loading branch information
wonholim committed Nov 20, 2023
1 parent 142e48b commit 0a84b4c
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 30 deletions.
30 changes: 0 additions & 30 deletions .github/workflows/BackEnd_CI.yml

This file was deleted.

63 changes: 63 additions & 0 deletions .github/workflows/BackEnd_CICD.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: BackEnd-CI

on:
push:
branches: 'feature/BE/*'
pull_request:
branches: 'develop'
types: [labeled, opened, synchronize, reopened]

jobs:
test:
if: contains(github.event.pull_request.labels.*.name, '💻백엔드')
runs-on: ubuntu-latest

steps:
- name: 코드 체크아웃
uses: actions/checkout@v2

- name: node 세팅
uses: actions/setup-node@v1
with:
node-version: '18'

- name: 의존성 설치
run: npm ci
working-directory: ./BackEnd

- name: 테스트 진행
run: npm test
working-directory: ./BackEnd

deploy:
needs: test
if: github.ref == 'refs/heads/develop' && github.event_name == 'push'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: docker/setup-buildx-action@v2
- uses: docker/login-action@v1
with:
registry: ${{ secrets.NCP_CONTAINER_REGISTRY }}
username: ${{ secrets.NCP_ACCESS_KEY }}
password: ${{ secrets.NCP_SECRET_KEY }}
- uses: docker/build-push-action@v2
with:
context: .
file: ./BackEnd/Dockerfile
push: true
tags: ${{ secrets.NCP_CONTAINER_REGISTRY }}/my-app:latest
- name: SSH Tunneling and Deploy
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.BASTION_HOST }}
username: ${{ secrets.BASTION_USER }}
key: ${{ secrets.BASTION_SSH_KEY }}
script: |
ssh -o StrictHostKeyChecking=no -L 9999:${{ secrets.INTERNAL_SERVER_IP }}:22 ${{ secrets.BASTION_USER }}@localhost -i ${{ secrets.BASTION_SSH_PRIVATE_KEY_PATH }} -fN
ssh -o StrictHostKeyChecking=no -p 9999 ${{ secrets.SERVER_USER }}@localhost -i ${{ secrets.SERVER_SSH_PRIVATE_KEY_PATH }} '
docker pull ${{ secrets.NCP_CONTAINER_REGISTRY }}/my-app:latest
docker stop my-app || true
docker rm my-app || true
docker run --name my-app -d -p 3000:3000 --env-file /var/env/.env ${{ secrets.NCP_CONTAINER_REGISTRY }}/my-app:latest
'
17 changes: 17 additions & 0 deletions BackEnd/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 빌드 단계
FROM node:18 AS builder
WORKDIR /app
COPY BackEnd/package*.json ./
RUN npm install
COPY BackEnd/ .
RUN npm run build

# 실행 단계
FROM node:18
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/package*.json ./
RUN npm install --only=production
RUN npm install pm2 -g
EXPOSE 3000
CMD ["pm2-runtime", "start", "dist/main.js"]

0 comments on commit 0a84b4c

Please sign in to comment.