Skip to content

Commit

Permalink
chore: 배포 환경 DB 연동
Browse files Browse the repository at this point in the history
commit e8c187ff21161041d4a687e5f0706fedcf85fde8
Author: Seonghyeon0409 <[email protected]>
Date:   Wed Nov 6 17:59:40 2024 +0900

    test: CICD test

commit 456b492d3444ff4359221c00160907ef531f75d2
Author: Seonghyeon0409 <[email protected]>
Date:   Wed Nov 6 17:59:14 2024 +0900

    test: CICD test

commit 008cf6e
Author: Seonghyeon0409 <[email protected]>
Date:   Wed Nov 6 17:51:54 2024 +0900

    test: CICD test

commit 92a5076
Author: Seonghyeon0409 <[email protected]>
Date:   Wed Nov 6 17:45:30 2024 +0900

    test: CICD test

commit eecd5aa
Author: Seonghyeon0409 <[email protected]>
Date:   Wed Nov 6 17:43:04 2024 +0900

    test: CICD test

commit a58c7c9
Author: Seonghyeon0409 <[email protected]>
Date:   Wed Nov 6 17:35:07 2024 +0900

    cicd test

commit 5fe7385
Author: Seonghyeon0409 <[email protected]>
Date:   Wed Nov 6 17:31:57 2024 +0900

    test: CICD test

commit eb8fff6
Author: Seonghyeon0409 <[email protected]>
Date:   Wed Nov 6 17:23:15 2024 +0900

    test: CICD test

commit 51cdd89
Author: Seonghyeon0409 <[email protected]>
Date:   Wed Nov 6 17:12:48 2024 +0900

    test: CICD test

commit 17b7abd
Author: Seonghyeon0409 <[email protected]>
Date:   Wed Nov 6 17:08:26 2024 +0900

    test: CICD test

commit fdd834e
Author: Seonghyeon0409 <[email protected]>
Date:   Wed Nov 6 17:04:37 2024 +0900

    test: CICD test

commit 5b0673e
Author: Seonghyeon0409 <[email protected]>
Date:   Wed Nov 6 17:00:27 2024 +0900

    test: CICD test

commit cf28d17
Author: Seonghyeon0409 <[email protected]>
Date:   Wed Nov 6 16:51:37 2024 +0900

    test: CICD test

commit 6d3b6e5
Author: Seonghyeon0409 <[email protected]>
Date:   Wed Nov 6 16:47:43 2024 +0900

    test: CICD test

commit e22cc14
Author: Seonghyeon0409 <[email protected]>
Date:   Wed Nov 6 16:34:02 2024 +0900

    test: CICD test

commit 64b18d8
Author: Seonghyeon0409 <[email protected]>
Date:   Wed Nov 6 16:30:23 2024 +0900

    test: CICD test

commit 74549f1
Author: Seonghyeon0409 <[email protected]>
Date:   Wed Nov 6 16:24:48 2024 +0900

    test: CICD test

commit aa0bf8e
Author: Seonghyeon0409 <[email protected]>
Date:   Wed Nov 6 16:21:30 2024 +0900

    test: CICD test

commit 7204647
Author: Seonghyeon0409 <[email protected]>
Date:   Wed Nov 6 16:18:17 2024 +0900

    test: CICD test

commit a8c735c
Author: Seonghyeon0409 <[email protected]>
Date:   Wed Nov 6 16:12:46 2024 +0900

    test: CICD test
  • Loading branch information
SeongHyeon0409 authored and SeungGwan123 committed Nov 7, 2024
1 parent 040d2ca commit cf948a6
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 63 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ jobs:
docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/corinee-client -f ./dockerfile-client .
docker push ${{ secrets.DOCKERHUB_USERNAME }}/corinee-client
- name: make deploy environment
run: |
echo "${{secrets.DEPLOY_ENV}}" > .env
- name: Send files & deploy script
uses: appleboy/scp-action@master
Expand All @@ -37,11 +40,11 @@ jobs:
username: ${{ secrets.SSH_USERNAME }}
password: ${{ secrets.SSH_PASSWORD }}
port: ${{ secrets.SSH_PORT }}
source: ${{ secrets.DOCKER_IMAGE }}
source: ${{ secrets.DEPLOY_FILES }}
target: /corinee
overwrite: true

- name: Docker run
- name: Docker Container run
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SSH_HOST }}
Expand All @@ -54,8 +57,8 @@ jobs:
cd /corinee
docker-compose down
docker-compose up -d
rm -rf .env docker-compose.yml
- name: Generate Error Report
if: failure()
run: |
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@

#!.yarn/cache
.pnp.*
.env.*
.env
5 changes: 2 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ services:
context: .
dockerfile: dockerfile-server
image: seunggwan/corinee-server
env_file:
- .env
ports:
- "3000:3000"
# environment:
Expand All @@ -26,8 +28,5 @@ services:
networks:
- app-network

volumes:
db-data:

networks:
app-network:
3 changes: 3 additions & 0 deletions dockerfile-server
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ RUN yarn install
# 소스 파일 복사
COPY packages/server .

# 애플리케이션 빌드
RUN yarn build

# 포트 노출
EXPOSE 3000

Expand Down
5 changes: 2 additions & 3 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"start": "nest start",
"start:dev": "NODE_ENV=development nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "NODE_ENV=production node dist/main",
"start:prod": "NODE_ENV=production node dist/main.js",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
Expand All @@ -33,8 +33,7 @@
"rxjs": "^7.8.1",
"ssh2": "^1.16.0",
"tunnel-ssh": "^5.1.2",
"typeorm": "0.3.7",
"win-node-env": "^0.6.1"
"typeorm": "0.3.7"
},
"devDependencies": {
"@nestjs/cli": "^10.0.0",
Expand Down
53 changes: 16 additions & 37 deletions packages/server/src/configs/typeorm.config.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,23 @@
import { TypeOrmModuleOptions } from '@nestjs/typeorm';
import { config } from 'dotenv';
import { resolve } from 'path';
import { createTunnel } from 'tunnel-ssh';

const env = process.env.NODE_ENV || 'development';
const envFileName = `.env.${env}`;
const envPath = resolve(process.cwd(), envFileName);
config();

config({ path: envPath });
const env = process.env.NODE_ENV || 'development';

export default async function getTypeOrmConfig(): Promise<TypeOrmModuleOptions> {
if (env === 'production') {
console.log('배포 환경에서 데이터베이스에 직접 연결합니다.');

return {
type: 'mysql',
host: process.env.DB_HOST,
port: Number(process.env.DB_PORT),
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
entities: [__dirname + '/../**/*.entity.{js,ts}'],
synchronize: false,
};
} else {
await setupSshTunnel();
console.log('SSH 터널링 설정 완료');
console.log('데이터베이스 연결 시도 중...');

return {
type: process.env.DB_TYPE as 'mysql',
host: '127.0.0.1', // 로컬 IP
port: 3307, // 터널링된 로컬 포트
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
entities: [__dirname + '/../**/*.entity.{js,ts}'],
synchronize: env === 'development',
};
}
if(env === 'development') await setupSshTunnel();
return {
type: process.env.DB_TYPE as 'mysql',
host: process.env.DB_HOST,
port: Number(process.env.DB_PORT),
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
entities: [__dirname + '/../**/*.entity.{js,ts}'],
synchronize: false,
};
}

async function setupSshTunnel(): Promise<void> {
Expand All @@ -50,7 +29,7 @@ async function setupSshTunnel(): Promise<void> {
password: process.env.SSH_PASSWORD,
};

const port = 3307; // 로컬에서 사용할 포트
const port = process.env.DB_PORT; // 로컬에서 사용할 포트

tunnel(sshOptions, port)
.then(() => {
Expand All @@ -66,10 +45,10 @@ async function setupSshTunnel(): Promise<void> {
function tunnel(sshOptions, port, autoClose = true): Promise<void> {
return new Promise((resolve, reject) => {
const forwardOptions = {
srcAddr: '127.0.0.1',
srcAddr: process.env.DB_HOST,
srcPort: port,
dstAddr: process.env.SSH_DB_HOST, // 원격 DB 서버 IP
dstPort: 3306, // 원격 DB 포트
dstPort: Number(process.env.SSH_DB_TUNNUL_PORT), // 원격 DB 포트
};

const tunnelOptions = {
Expand Down
17 changes: 0 additions & 17 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7526,7 +7526,6 @@ __metadata:
tunnel-ssh: "npm:^5.1.2"
typeorm: "npm:0.3.7"
typescript: "npm:^5.1.3"
win-node-env: "npm:^0.6.1"
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -8758,22 +8757,6 @@ __metadata:
languageName: node
linkType: hard

"win-node-env@npm:^0.6.1":
version: 0.6.1
resolution: "win-node-env@npm:0.6.1"
bin:
CHAI_JEST_SNAPSHOT_UPDATE_ALL: bin/CHAI_JEST_SNAPSHOT_UPDATE_ALL.js
DEBUG: bin/DEBUG.js
ENV: bin/ENV.js
NODE_DEBUG: bin/NODE_DEBUG.js
NODE_ENV: bin/NODE_ENV.js
NODE_OPTIONS: bin/NODE_OPTIONS.js
PORT: bin/PORT.js
checksum: 10c0/95ac662805f5cf57a30afdd768d51f164115e39aae8bfad6a77793ae389854a00a001a0d864f02f568379a4e7ba1d667e4d82ec4924a8487d3caae644d24a106
conditions: os=win32
languageName: node
linkType: hard

"word-wrap@npm:^1.2.5":
version: 1.2.5
resolution: "word-wrap@npm:1.2.5"
Expand Down

0 comments on commit cf948a6

Please sign in to comment.