From 4f4067df854fcf7a07ab3599fd21bbc8bf0a8598 Mon Sep 17 00:00:00 2001 From: Aseer KT Date: Fri, 9 Aug 2024 11:15:24 +0530 Subject: [PATCH] feat: infra ec2 github actions --- .github/workflows/{cd.yml => ec2-cd.yml} | 25 ++++++++++++-- infra/ec2/Caddyfile | 5 +++ Makefile => infra/ec2/Makefile | 0 .../ec2/docker-compose.prod.yml | 20 +++++++++++ infra/ec2/install.sh | 33 +++++++++++++++++++ 5 files changed, 81 insertions(+), 2 deletions(-) rename .github/workflows/{cd.yml => ec2-cd.yml} (60%) create mode 100644 infra/ec2/Caddyfile rename Makefile => infra/ec2/Makefile (100%) rename docker-compose.prod.yml => infra/ec2/docker-compose.prod.yml (57%) create mode 100644 infra/ec2/install.sh diff --git a/.github/workflows/cd.yml b/.github/workflows/ec2-cd.yml similarity index 60% rename from .github/workflows/cd.yml rename to .github/workflows/ec2-cd.yml index 6e9f225..4579bb2 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/ec2-cd.yml @@ -7,7 +7,7 @@ on: jobs: build: - runs-on: self-hosted + runs-on: ubuntu-latest steps: - name: Checkout @@ -33,9 +33,30 @@ jobs: - name: Build packages run: pnpm build + - name: Upload build files + uses: actions/upload-artifact@v4 + with: + name: mchat_build + path: | + server + web/dist + infra/ec2 + + + deploy: + runs-on: self-hosted + needs: build + steps: + - name: Download math result for job 1 + uses: actions/download-artifact@v4 + with: + name: mchat_build + + - name: Move static files + run: mv web/dist /var/www/html + - name: Restart application run: pm2 restart mchat - \ No newline at end of file diff --git a/infra/ec2/Caddyfile b/infra/ec2/Caddyfile new file mode 100644 index 0000000..0f6603c --- /dev/null +++ b/infra/ec2/Caddyfile @@ -0,0 +1,5 @@ +mchat.aseerkt.com { + encode zstd gzip + file_server + reverse_proxy /api/* 127.0.0.1:5000 +} diff --git a/Makefile b/infra/ec2/Makefile similarity index 100% rename from Makefile rename to infra/ec2/Makefile diff --git a/docker-compose.prod.yml b/infra/ec2/docker-compose.prod.yml similarity index 57% rename from docker-compose.prod.yml rename to infra/ec2/docker-compose.prod.yml index d415b07..d0e98a1 100644 --- a/docker-compose.prod.yml +++ b/infra/ec2/docker-compose.prod.yml @@ -23,3 +23,23 @@ services: interval: 30s timeout: 10s retries: 5 + + caddy: + image: caddy:2.8.4-alpine + container_name: mchat_caddy + restart: unless-stopped + cap_add: + - NET_ADMIN + ports: + - '80:80' + - '443:443' + - '443:443/udp' + volumes: + - $PWD/Caddyfile:/etc/caddy/Caddyfile + - caddy_data:/data + - caddy_config:/config + +volumes: + caddy_data: + external: true + caddy_config: diff --git a/infra/ec2/install.sh b/infra/ec2/install.sh new file mode 100644 index 0000000..442f7ea --- /dev/null +++ b/infra/ec2/install.sh @@ -0,0 +1,33 @@ +# INSTALL NODE.JS + +# installs nvm (Node Version Manager) +curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash + +# download and install Node.js (you may need to restart the terminal) +nvm install 20 + +# verifies the right Node.js version is in the environment +node -v # should print `v20.16.0` + +# verifies the right npm version is in the environment +npm -v # should print `10.8.1` + +npm i -g pm2 + +# INSTALL DOCKER + +# Add Docker's official GPG key: +sudo apt-get update +sudo apt-get install ca-certificates curl +sudo install -m 0755 -d /etc/apt/keyrings +sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc +sudo chmod a+r /etc/apt/keyrings/docker.asc + +# Add the repository to Apt sources: +echo \ + "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \ + $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | + sudo tee /etc/apt/sources.list.d/docker.list >/dev/null +sudo apt-get update + +sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin