Skip to content

Commit

Permalink
Merge pull request #79 from boostcampwm-2024/back/main
Browse files Browse the repository at this point in the history
[BE] 브랜치 병합
  • Loading branch information
uuuo3o authored Nov 14, 2024
2 parents ca816b5 + 6efbd2c commit 8622129
Show file tree
Hide file tree
Showing 112 changed files with 3,220 additions and 386 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: deploy

on:
push:
branches: [main, alpha]
branches: [dev]
pull_request:
branches: [main, alpha]
branches: [dev]

env:
DOCKER_IMAGE: ${{ vars.DOCKERHUB_USERNAME }}/juga-docker
Expand All @@ -13,38 +13,48 @@ env:
jobs:
build-and-deploy:
runs-on: ubuntu-latest
strategy:
max-parallel: 1
matrix:
app:
[
{ name: 'be', dir: 'BE', port: 3000, container: 'juga-docker-be' },
{ name: 'fe', dir: 'FE', port: 5173, container: 'juga-docker-fe' },
]

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
cache-dependency-path: ./BE/package-lock.json
cache-dependency-path: ./${{matrix.app.dir}}/package-lock.json

- name: Create .env file
run: |
touch ./BE/.env
echo "${{ secrets.ENV }}" > ./BE/.env
touch ./${{ matrix.app.dir }}/.env
echo "${{ secrets.ENV }}" > ./${{matrix.app.dir}}/.env
- name: Install dependencies
working-directory: ./BE
working-directory: ./${{matrix.app.dir}}
continue-on-error: true
run: npm ci

- name: Run tests
working-directory: ./BE
if: ${{ matrix.app.name == 'be' }}
working-directory: ./${{matrix.app.dir}}
run: npm test
env:
CI: true

- name: Run linter
working-directory: ./BE
working-directory: ./${{matrix.app.dir}}
run: npm run lint

- name: Build application
working-directory: ./BE
working-directory: ./${{matrix.app.dir}}
run: npm run build

- name: Login to Docker Hub
Expand All @@ -54,16 +64,16 @@ jobs:
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build and Push Docker Image
working-directory: ./BE
working-directory: ./${{ matrix.app.dir }}
env:
NCP_ACCESS_KEY: ${{ secrets.NCP_ACCESS_KEY }}
NCP_SECRET_KEY: ${{ secrets.NCP_SECRET_KEY }}
run: |
docker build -t ${{ env.DOCKER_IMAGE }}:${{ env.DOCKER_TAG }} .
docker tag ${{ env.DOCKER_IMAGE }}:${{ env.DOCKER_TAG }} ${{ env.DOCKER_IMAGE }}:latest
docker build -t ${{ env.DOCKER_IMAGE }}-${{ matrix.app.name }}:${{ env.DOCKER_TAG }} .
docker tag ${{ env.DOCKER_IMAGE }}-${{ matrix.app.name }}:${{ env.DOCKER_TAG }} ${{ env.DOCKER_IMAGE }}-${{ matrix.app.name }}:latest
docker push ${{ env.DOCKER_IMAGE }}:${{ env.DOCKER_TAG }}
docker push ${{ env.DOCKER_IMAGE }}:latest
docker push ${{ env.DOCKER_IMAGE }}-${{ matrix.app.name }}:${{ env.DOCKER_TAG }}
docker push ${{ env.DOCKER_IMAGE }}-${{ matrix.app.name }}:latest
- name: Get Github Actions IP
id: ip
Expand Down Expand Up @@ -95,15 +105,25 @@ jobs:
script: |
docker system prune -af
echo "${{ secrets.ENV }}" > .env
docker pull ${{ env.DOCKER_IMAGE }}:${{ env.DOCKER_TAG }}
docker stop juga-docker || true
docker rm juga-docker || true
docker network create juga-network || true
docker run -d \
--name redis \
--network juga-network \
-p 6379:6379 \
-v redis_data:/data \
redis:latest redis-server --appendonly yes
docker pull ${{ env.DOCKER_IMAGE }}-${{ matrix.app.name }}:${{ env.DOCKER_TAG }}
docker stop ${{ matrix.app.container }} || true
docker rm ${{ matrix.app.container }} || true
docker run -d \
--name juga-docker \
-p 3000:3000 \
--name ${{ matrix.app.container }} \
--network juga-network \
-p ${{ matrix.app.port }}:${{ matrix.app.port }} \
--env-file .env \
${{ env.DOCKER_IMAGE }}:${{ env.DOCKER_TAG }}
${{ env.DOCKER_IMAGE }}-${{ matrix.app.name }}:${{ env.DOCKER_TAG }}
- name: Remove Github Action Ip to Security group
run: |
Expand Down
132 changes: 132 additions & 0 deletions .github/workflows/deply-alpha.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
name: deploy

on:
push:
branches: [alpha]
pull_request:
branches: [alpha]

env:
DOCKER_IMAGE: ${{ vars.DOCKERHUB_USERNAME }}/juga-docker
DOCKER_TAG: ${{ github.sha }}

jobs:
build-and-deploy:
runs-on: ubuntu-latest
strategy:
max-parallel: 1
matrix:
app:
[
{ name: 'be', dir: 'BE', port: 3000, container: 'juga-docker-be' },
{ name: 'fe', dir: 'FE', port: 5173, container: 'juga-docker-fe' },
]

steps:
- uses: actions/checkout@v4

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
cache-dependency-path: ./${{matrix.app.dir}}/package-lock.json

- name: Create .env file
run: |
touch ./${{ matrix.app.dir }}/.env
echo "${{ secrets.ENV }}" > ./${{matrix.app.dir}}/.env
- name: Install dependencies
working-directory: ./${{matrix.app.dir}}
continue-on-error: true
run: npm ci

- name: Run tests
if: ${{ matrix.app.name == 'be' }}
working-directory: ./${{matrix.app.dir}}
run: npm test
env:
CI: true

- name: Run linter
working-directory: ./${{matrix.app.dir}}
run: npm run lint

- name: Build application
working-directory: ./${{matrix.app.dir}}
run: npm run build

- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ vars.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build and Push Docker Image
working-directory: ./${{ matrix.app.dir }}
env:
NCP_ACCESS_KEY: ${{ secrets.NCP_ACCESS_KEY }}
NCP_SECRET_KEY: ${{ secrets.NCP_SECRET_KEY }}
run: |
docker build -t ${{ env.DOCKER_IMAGE }}-${{ matrix.app.name }}:${{ env.DOCKER_TAG }} .
docker tag ${{ env.DOCKER_IMAGE }}-${{ matrix.app.name }}:${{ env.DOCKER_TAG }} ${{ env.DOCKER_IMAGE }}-${{ matrix.app.name }}:latest
docker push ${{ env.DOCKER_IMAGE }}-${{ matrix.app.name }}:${{ env.DOCKER_TAG }}
docker push ${{ env.DOCKER_IMAGE }}-${{ matrix.app.name }}:latest
- name: Get Github Actions IP
id: ip
uses: haythem/[email protected]

- name: Setting NCP CLI & Credentials
run: |
cd ~
wget https://www.ncloud.com/api/support/download/5/65
unzip 65
mkdir ~/.ncloud
echo -e "[DEFAULT]\nncloud_access_key_id = ${{ secrets.NCP_ACCESS_KEY }}\nncloud_secret_access_key = ${{ secrets.NCP_SECRET_KEY }}\nncloud_api_url = ${{ secrets.NCP_API_URI }}" >> ~/.ncloud/configure
- name: Add Github Action Ip to Security group
run: |
cd ~
ls -la
chmod -R 777 ~/cli_linux
cd ~/cli_linux
./ncloud vserver addAccessControlGroupInboundRule --regionCode KR --vpcNo ${{ secrets.NCP_VPC_ID }} --accessControlGroupNo ${{ secrets.NCP_ACG_ID }} --accessControlGroupRuleList "protocolTypeCode='TCP', ipBlock='${{ steps.ip.outputs.ipv4 }}/32', portRange='${{ secrets.SSH_PORT }}'"
- name: Deploy to NCP Server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.NCP_ALPHA_SERVER_HOST }}
username: ${{ secrets.NCP_ALPHA_SERVER_USERNAME }}
key: ${{ secrets.NCP_SERVER_SSH_KEY }}
port: 22
script: |
docker system prune -af
echo "${{ secrets.ENV }}" > .env
docker network create juga-network || true
docker run -d \
--name redis \
--network juga-network \
-p 6379:6379 \
-v redis_data:/data \
redis:latest redis-server --appendonly yes
docker pull ${{ env.DOCKER_IMAGE }}-${{ matrix.app.name }}:${{ env.DOCKER_TAG }}
docker stop ${{ matrix.app.container }} || true
docker rm ${{ matrix.app.container }} || true
docker run -d \
--name ${{ matrix.app.container }} \
--network juga-network \
-p ${{ matrix.app.port }}:${{ matrix.app.port }} \
--env-file .env \
${{ env.DOCKER_IMAGE }}-${{ matrix.app.name }}:${{ env.DOCKER_TAG }}
- name: Remove Github Action Ip to Security group
run: |
chmod -R 777 ~/cli_linux
cd ~/cli_linux
./ncloud vserver removeAccessControlGroupInboundRule --regionCode KR --vpcNo ${{ secrets.NCP_VPC_ID }} --accessControlGroupNo ${{ secrets.NCP_ACG_ID }} --accessControlGroupRuleList "protocolTypeCode='TCP', ipBlock='${{ steps.ip.outputs.ipv4 }}/32', portRange='${{ secrets.SSH_PORT }}'"
1 change: 1 addition & 0 deletions BE/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
Dockerfile
node_modules
dist
.env*
1 change: 1 addition & 0 deletions BE/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ module.exports = {
'class-methods-use-this': 'off',
'@typescript-eslint/no-unsafe-assignment': 'off',
'@typescript-eslint/no-unsafe-member-access': 'off',
'@typescript-eslint/naming-convention': 'off',
},
};
19 changes: 14 additions & 5 deletions BE/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
FROM node:20
RUN mkdir -p /var/app
WORKDIR /var/app
COPY . .
# 빌드 스테이지
FROM node:20-slim as builder
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

# 실행 스테이지
FROM node:20-slim
WORKDIR /var/app
COPY package*.json ./
RUN npm install --only=production
COPY --from=builder /app/dist ./dist

EXPOSE 3000
CMD [ "node", "dist/main.js" ]
CMD ["node", "dist/main.js"]
27 changes: 27 additions & 0 deletions BE/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
version: '3'
services:
api:
build:
context: .
dockerfile: Dockerfile
ports:
- '3000:3000'
environment:
- NODE_ENV=production
- REDIS_HOST=redis
- REDIS_PORT=6379
depends_on:
- redis
restart: unless-stopped

redis:
image: redis:latest
ports:
- '6379:6379'
volumes:
- redis_data:/data
command: redis-server --appendonly yes
restart: unless-stopped

volumes:
redis_data:
Loading

0 comments on commit 8622129

Please sign in to comment.