From 7324d77e68737234dd027fc21211595eebe4a65c Mon Sep 17 00:00:00 2001 From: rishabh-malhotraa Date: Sun, 28 Apr 2024 17:57:57 +0530 Subject: [PATCH] Adding workflow to deploy to DigitalOcean --- .github/workflows/digital-ocean-prod.yaml | 72 +++++++++++++++++++++++ ecosystem.config.js | 30 ++++++++++ package.json | 2 +- server/package.json | 5 +- server/tsconfig.json | 2 +- 5 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/digital-ocean-prod.yaml create mode 100644 ecosystem.config.js diff --git a/.github/workflows/digital-ocean-prod.yaml b/.github/workflows/digital-ocean-prod.yaml new file mode 100644 index 0000000..08360e8 --- /dev/null +++ b/.github/workflows/digital-ocean-prod.yaml @@ -0,0 +1,72 @@ +name: CI/CD Digital Ocean PROD + +on: + push: + branches: [main] + +env: + REACT_APP_CLIENT_URL: ${{secrets.REACT_APP_CLIENT_URL}} + REACT_APP_SERVER_URL: ${{secrets.REACT_APP_SERVER_URL}} + +jobs: + build: + runs-on: ubuntu-latest + + strategy: + matrix: + node-version: [14.x] + + steps: + - name: 'Checkout Repository' + uses: actions/checkout@v2 + + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node-version }} + + - name: Install and Build Server + run: cd server && npm install && npm run build + + - name: Install and Build Client + run: cd client && npm install --force && npm run build + + - name: Upload to Server Digital Ocean + uses: appleboy/scp-action@master # This is the action + with: + host: ${{secrets.SSH_HOST_PROD}} # IP address of the server you wish to ssh into + key: ${{secrets.SSH_KEY}} # Private or public key of the server + username: ${{ secrets.SSH_USERNAME }} # User of the server you want to ssh into + passphrase: ${{ secrets.SSH_PASSPHRASE }} + source: 'server/dist' # Path to the directory you want to copy + target: '~/' # Path to the directory you want to copy to + + - name: Upload to Client Digital Ocean + uses: appleboy/scp-action@master # This is the action + with: + host: ${{secrets.SSH_HOST_PROD}} # IP address of the server you wish to ssh into + key: ${{secrets.SSH_KEY}} # Private or public key of the server + username: ${{ secrets.SSH_USERNAME }} # User of the server you want to ssh into + passphrase: ${{ secrets.SSH_PASSPHRASE }} + source: 'client/build' # Path to the directory you want to copy + target: '/var/www/html' # Path to the directory you want to copy to + + - name: Start Server + uses: appleboy/ssh-action@v0.1.10 # This is the action + with: + host: ${{secrets.SSH_HOST_PROD}} # IP address of the server you wish to ssh into + key: ${{secrets.SSH_KEY}} # Private or public key of the server + username: ${{ secrets.SSH_USERNAME }} # User of the server you want to ssh into + passphrase: ${{ secrets.SSH_PASSPHRASE }} + + script: | + echo $PATH + export NVM_DIR=~/.nvm + source ~/.nvm/nvm.sh + rm -rf ~/caucus + git clone https://github.com/Rishabh-malhotraa/caucus.git + mv ~/server/dist ~/caucus/server/ + cd ~/caucus/server && npm install --production && cd ~/ && rm -rf ./server + pm2 stop caucus-server caucus-crdt-server + pm2 start ~/env/ecosystem.config.js --env production + echo 'Deployment successful to digital ocean' diff --git a/ecosystem.config.js b/ecosystem.config.js new file mode 100644 index 0000000..03b18db --- /dev/null +++ b/ecosystem.config.js @@ -0,0 +1,30 @@ +// configuration for pm2 +// pm2 start ~/env/ecosystem.config.js --env production + +module.exports = { + apps: [ + { + name: 'caucus-server', + script: '/root/caucus/server/dist/index.js', + watch: true, + env: { + PROD : '', + CLIENT_URL: '', + SERVER_URL: '', + GOOGLE_KEY_CLIENTID: '', + GOOGLE_KEY_CLIENTSECRET: '', + GITHUB_KEY_CLIENTID: '', + GITHUB_KEY_CLIENTSECRET: '', + COOKIE_KEYS: '', + JDOODLE_CLIENTID: '', + JDOOLDE_CLIENTSECRET: '', + PORT: '', + }, + }, + { + name : 'caucus-crdt-server', + script: 'npx y-websocket', + watch: true, + } + ], +}; \ No newline at end of file diff --git a/package.json b/package.json index 912cffb..6ce32cc 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "scripts": { "format": "prettier --write \"src/**/*.ts\" \"src/**/*.tsx\"", "lint": "eslint src/**/*.ts src/**/*.tsx", - "install-client": "cd client && npm install", + "install-client": "cd client && npm install --force", "install-server": "cd server && npm install", "install-modules": "npm install && npm run install-client && npm run install-server", "client": "cd client && npm run start", diff --git a/server/package.json b/server/package.json index 68c5339..e61d076 100644 --- a/server/package.json +++ b/server/package.json @@ -13,10 +13,9 @@ "main": "build/index.js", "proxy": "https://caucus.netlify.app/", "scripts": { - "build-ts": "tsc", - "postinstall": "npm run build-ts", + "build": "tsc", "start": "npm run serve", - "serve": "node build/index.js", + "serve": "node dist/index.js", "dev": "nodemon --exec ts-node src/index.ts", "format": "prettier --write \"src/**/*.ts\"" }, diff --git a/server/tsconfig.json b/server/tsconfig.json index 7104b97..41cbb76 100644 --- a/server/tsconfig.json +++ b/server/tsconfig.json @@ -14,7 +14,7 @@ // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ // "sourceMap": true, /* Generates corresponding '.map' file. */ // "outFile": "./", /* Concatenate and emit output to single file. */ - "outDir": "./build" /* Redirect output structure to the directory. */, + "outDir": "./dist" /* Redirect output structure to the directory. */, // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ // "composite": true, /* Enable project compilation */ // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */