From 901c2e75af3596b1288de108a76ae1a08cb10b47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rcio=20Vin=C3=ADcius=20Sena?= Date: Mon, 3 Jun 2024 11:14:29 -0300 Subject: [PATCH] Setup Digital Ocean (#436) * init setup digital ocean * split script * add echo secret_key * add echo secret_key * update droplet_ip * Add DigitalOcean droplet to known_hosts --- .github/workflows/deploy.yml | 36 +++++++++++++++++++++++++++++ docker-api/Dockerfile | 2 +- docker-compose.dev.yml | 44 ++++++++++++++++++++++++++++++++++++ docker-compose.yml | 21 ++--------------- package.json | 2 +- start.sh | 2 +- 6 files changed, 85 insertions(+), 22 deletions(-) create mode 100644 .github/workflows/deploy.yml create mode 100644 docker-compose.dev.yml diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..683a325 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,36 @@ +name: Deploy to DigitalOcean + +on: + push: + branches: + - config/digital-ocean + +jobs: + deploy: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Setup SSH and known hosts + uses: webfactory/ssh-agent@v0.5.3 + with: + ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} + + - name: Add DigitalOcean droplet to known_hosts + run: ssh-keyscan ${{ secrets.DROPLET_IP }} >> ~/.ssh/known_hosts + + - name: Copy project files to the server + run: rsync -avz --exclude '/node_modules/' --exclude '/.git/' ./ root@${{ secrets.DROPLET_IP }}:/root/abibliadigital/ + + - name: Build container + run: ssh root@${{ secrets.DROPLET_IP }} "cd /root/abibliadigital/ && docker-compose up -d --build" + + - name: Environment setup + run: | + ssh root@${{ secrets.DROPLET_IP }} "echo 'MONGODB_URI=${{ secrets.MONGODB_URI }}' >> /root/abibliadigital/.env" + ssh root@${{ secrets.DROPLET_IP }} "echo 'SECRET_KEY=${{ secrets.SECRET_KEY }}' >> /root/abibliadigital/.env" + ssh root@${{ secrets.DROPLET_IP }} "echo 'SENDGRID_KEY=${{ secrets.SENDGRID_KEY }}' >> /root/abibliadigital/.env" + + - name: Restart the application + run: ssh root@${{ secrets.DROPLET_IP }} "cd /root/abibliadigital/ && docker-compose down && docker-compose up -d" \ No newline at end of file diff --git a/docker-api/Dockerfile b/docker-api/Dockerfile index f3b666d..c71b0ee 100644 --- a/docker-api/Dockerfile +++ b/docker-api/Dockerfile @@ -1,5 +1,5 @@ # specify the node base image with your desired version node: -FROM node:10.14.0 +FROM node:16.20.1 # replace this with your application's default port EXPOSE 4201 EXPOSE 49153 diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml new file mode 100644 index 0000000..49fd582 --- /dev/null +++ b/docker-compose.dev.yml @@ -0,0 +1,44 @@ +version: '3' + +services: + abibliadigital: + build: ./docker-api + command: [ "bash", "/usr/src/api/start.sh" ] + ports: + - "3000:3000" + expose: + - "3000" + container_name: abibliadigital + environment: + - MONGODB_URI=mongodb://abibliadigital-mongo/abibliadigital + - NODE_ENV=development + - SECRET_KEY=19B2AFFC3B287A4E41FA7C1158A8A + - REDIS_URL=redis://abibliadigital-redis + volumes: + - ../abibliadigital:/usr/src/api + depends_on: + - abibliadigital-mongo + - abibliadigital-redis + + abibliadigital-mongo: + build: ./docker-mongo + container_name: abibliadigital-mongo + ports: + - "27017:27017" + expose: + - "27017" + volumes: + - ../mongo-abibliadigital/db/data:/data/db + - ../mongo-abibliadigital/db/log:/var/log/mongodb + + abibliadigital-redis: + image: "redis:alpine" + container_name: abibliadigital-redis + command: redis-server + ports: + - "6379:6379" + expose: + - "6379" + restart: always + volumes: + - ../redis-data:/var/lib/redis diff --git a/docker-compose.yml b/docker-compose.yml index 49fd582..cc0bab6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,36 +1,19 @@ version: '3' services: - abibliadigital: + abibliadigital-app: build: ./docker-api command: [ "bash", "/usr/src/api/start.sh" ] ports: - "3000:3000" expose: - "3000" - container_name: abibliadigital - environment: - - MONGODB_URI=mongodb://abibliadigital-mongo/abibliadigital - - NODE_ENV=development - - SECRET_KEY=19B2AFFC3B287A4E41FA7C1158A8A - - REDIS_URL=redis://abibliadigital-redis + container_name: abibliadigital-app volumes: - ../abibliadigital:/usr/src/api depends_on: - - abibliadigital-mongo - abibliadigital-redis - abibliadigital-mongo: - build: ./docker-mongo - container_name: abibliadigital-mongo - ports: - - "27017:27017" - expose: - - "27017" - volumes: - - ../mongo-abibliadigital/db/data:/data/db - - ../mongo-abibliadigital/db/log:/var/log/mongodb - abibliadigital-redis: image: "redis:alpine" container_name: abibliadigital-redis diff --git a/package.json b/package.json index df971e9..7f232bd 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "license": "MIT", "private": false, "engines": { - "node": ">=10.14.0" + "node": ">=16.20.1" }, "scripts": { "dev": "nodemon --exec babel-node app.js", diff --git a/start.sh b/start.sh index 5e6188e..8544fc1 100644 --- a/start.sh +++ b/start.sh @@ -1,2 +1,2 @@ yarn install -yarn dev +yarn start