Skip to content

Commit

Permalink
Merge pull request #14 from dmytro-komlyk/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
dmytro-komlyk authored May 29, 2024
2 parents 833aa1e + d220e9b commit 7720a8c
Show file tree
Hide file tree
Showing 20 changed files with 260 additions and 124 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/ssh-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
${{ env.REGISTRY }}/${{ github.repository_owner }}/${{ env.PROD_NAME }}-server:latest
build-args: |
BUILD_MODE=prod
SERVER_DOMAIN=${{ env.SERVER_DOMAIN }}
APP_DOMAIN=${{ env.SERVER_DOMAIN }}
ADMIN_DOMAIN=${{ env.ADMIN_DOMAIN }}
- name: 'Build Client'
Expand All @@ -59,8 +59,8 @@ jobs:
${{ env.REGISTRY }}/${{ github.repository_owner }}/${{ env.PROD_NAME }}-client:latest
build-args: |
BUILD_MODE=prod
SERVER_DOMAIN=${{ env.SERVER_DOMAIN }}
APP_DOMAIN=${{ env.APP_DOMAIN }}
NEXT_PUBLIC_SERVER_DOMAIN=${{ env.SERVER_DOMAIN }}
NEXT_PUBLIC_APP_DOMAIN=${{ env.APP_DOMAIN }}
- name: 'Build Admin'
if: contains(github.event.pull_request.labels.*.name, 'frontend-admin')
Expand All @@ -73,5 +73,5 @@ jobs:
${{ env.REGISTRY }}/${{ github.repository_owner }}/${{ env.PROD_NAME }}-admin:latest
build-args: |
BUILD_MODE=prod
SERVER_DOMAIN=${{ env.SERVER_DOMAIN }}
ADMIN_DOMAIN=${{ env.ADMIN_DOMAIN }}
NEXT_PUBLIC_SERVER_DOMAIN=${{ env.SERVER_DOMAIN }}
NEXT_PUBLIC_APP_DOMAIN=${{ env.ADMIN_DOMAIN }}
4 changes: 2 additions & 2 deletions .github/workflows/ssh-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: 'Run deploy on SSH'
if: github.event.pull_request.merged == true && github.event.pull_request.base.ref == ${{ env.BRANCH_NAME }}
if: github.event.pull_request.merged == true && github.event.pull_request.base.ref == ${{ env.BRANCH }}
uses: appleboy/ssh-action@v1.0.0
with:
host: ${{ vars.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
password: ${{ secrets.SSH_PASSWORD }}
port: ${{ vars.SSH_PORT }}
script: |
cd ${{ env.PROJECT_SSH_FOLDER_NAME }}
cd ${{ env.PROJECT_SSH_FOLDER }}
echo ${{ secrets.GITHUB_TOKEN }} | docker login ghcr.io -u ${{ github.repository_owner }} --password-stdin
if [[ "${{ contains(github.event.pull_request.labels.*.name, env.LABEL_SERVER) }}" == "true" ]]; then
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ node_modules
.turbo
.next
dist
out

# Uploads
apps/server/public
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
README.md
187 changes: 187 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,188 @@
# Next-Nest-js-CI-on-VPS-Boilerplate

## Overview

This repository provides a boilerplate for setting up a Next.js and Nest.js project with Continuous Integration (CI) on a Virtual Private Server (VPS). It includes configurations for various tools and libraries to help you quickly get started with a modern web development stack.

## Features

- Next.js for the frontend
- Nest.js for the backend
- TypeScript for type safety
- Docker for containerization
- CI/CD with GitHub Actions
- Monorepo structure with PNPM Workspaces
- Prisma ORM for database management
- TailwindCSS for styling
- NextAuth for authentication
- tRPC for type-safe APIs
- NextUI for UI components
- Next-Intl for internationalization

## Getting Started

### Prerequisites

Ensure you have the following installed:

- Node.js
- PNPM
- Docker

### Installation

1. **Clone the repository**

```bash
git clone https://github.com/dmytro-komlyk/Next-Nest-js-CI-on-VPS-Boilerplate.git
cd Next-Nest-js-CI-on-VPS-Boilerplate
```

2. **Install dependencies**

```bash
pnpm install
```

3. **Setup environment variables**

```bash
cp apps/admin/.env.local.example apps/admin/.env.local
cp apps/client/.env.local.example apps/client/.env.local
cp apps/server/.env.example apps/server/.env
```

### Running the Project

To start the development environment:

```bash
pnpm dev
```

This will concurrently run the Next.js and Nest.js applications.

### Building for Production

1. **To build the project for production:**

```bash
pnpm build
```

2. **To start the production build:**

```bash
pnpm start
```

### Docker development

To run the project using Docker, use the provided docker-compose files.

1. **Set up environment variables**

```bash
cp apps/admin/.env.docker.local.example apps/admin/.env.docker.local
cp apps/client/.env.docker.local.example apps/client/.env.docker.local
cp apps/server/.env.docker.local.example apps/server/.env.docker.local
```

2. **Build and run Docker containers**

```bash
docker compose -f docker-compose.local.yml up -d
```

## Deploy

### CI/CD Pipeline

This project uses GitHub Actions for continuous integration and continuous deployment. The configuration files are located in the .github/workflows directory.

1. 🔒 **Set up your secrets and variables in a GitHub repository**

```bash
SSH_PASSWORD
SSH_USERNAME
```

```bash
ADMIN_DOMAIN
APP_DOMAIN
SERVER_DOMAIN
BRANCH_DEPLOYMENT
PROD_NAME
SSH_FOLDER
SSH_HOST
SSH_PORT
```

2. 🏷️ **Set up labels in a GitHub repository**

```bash
backend
frontend-client
frontend-admin
```

### VPS Сonfiguration

1. **Connect to your VPS and run the following commands to install the required dependencies:**

```bash
sudo apt update
sudo apt install docker.io
sudo apt install docker-compose
```

2. **Create structure, copy files and set up environment variables:**

```markdown
PROD_NAME
.
├── client
│ └── .env.docker.prod #
├── admin
│ └── .env.docker.prod #
├── server
│ └── .env.docker.prod #
├── database
│ ├── conf
│ │ └── mongod.conf.orig
│ ├── seed
│ │ └── *.json
│ ├── Dockerfile
│ └── init-seed.sh
└── docker-compose.prod-ci.yml #
```

3. **Run Docker containers:**

```bash
docker login ghcr.io -u <github.repository_owner>
docker compose -f docker-compose.prod-ci.yml -p PROD_NAME pull
docker compose -f docker-compose.prod-ci.yml -p PROD_NAME up -d
```

## Folder Structure

```markdown
.
├── .github
│ └── workflows # CI/CD configurations
├── .vscode # VSCode settings
├── apps
│ ├── admin # Next.js app
│ ├── client # Next.js app
│ ├── database # MongoDB
│ └── server # Nest.js app
├── packages
│ └── eslint-config # Shared ESLint configuration
├── docker-compose*.yml # Docker configurations
├── pnpm-workspace.yaml # pnpm workspace configuration
├── README.md # Project documentation
└── ...
```
7 changes: 6 additions & 1 deletion apps/admin/.env.docker.local.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ APP_PORT=3002

NEXT_PUBLIC_BASE_URL=http://localhost:3001
NEXT_PUBLIC_SERVER_URL=http://localhost:3000
NEXT_PUBLIC_DOCKER_SERVICE_URL="http://server"
NEXT_PUBLIC_SERVER_API_URL=http://localhost:3000/api
NEXT_PUBLIC_SERVER_TRPC_URL=http://localhost:3000/api/trpc
NEXTAUTH_SECRET=

#Auth
NEXTAUTH_SECRET= #openssl rand -base64 32
NEXTAUTH_URL=http://localhost:3001/api/auth
NEXTAUTH_JWT_ACCESS_TOKEN_EXPIRATION='1d'
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=

# This was inserted by `prisma init`:
# Environment variables declared in this file are automatically made available to Prisma.
Expand Down
21 changes: 13 additions & 8 deletions apps/admin/.env.docker.prod.example
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
APP_PORT=3002
APP_DOMAIN=
SERVER_DOMAIN=

NEXT_PUBLIC_BASE_URL="https://${APP_DOMAIN}"
NEXT_PUBLIC_SERVER_URL="https://${SERVER_DOMAIN}"
NEXT_PUBLIC_SERVER_API_URL="https://${SERVER_DOMAIN}/api"
NEXT_PUBLIC_SERVER_TRPC_URL="https://${SERVER_DOMAIN}/api/trpc"
NEXTAUTH_SECRET=
NEXTAUTH_URL="https://${APP_DOMAIN}/api/auth"
NEXT_PUBLIC_APP_DOMAIN=
NEXT_PUBLIC_SERVER_DOMAIN=
NEXT_PUBLIC_BASE_URL="http://${NEXT_PUBLIC_APP_DOMAIN}"
NEXT_PUBLIC_SERVER_URL="http://${NEXT_PUBLIC_SERVER_DOMAIN}"
NEXT_PUBLIC_DOCKER_SERVICE_URL="https://server"
NEXT_PUBLIC_SERVER_API_URL="http://${NEXT_PUBLIC_SERVER_DOMAIN}/api"
NEXT_PUBLIC_SERVER_TRPC_URL="http://${NEXT_PUBLIC_SERVER_DOMAIN}/api/trpc"

#Auth
NEXTAUTH_SECRET= #openssl rand -base64 32
NEXTAUTH_URL="http://${NEXT_PUBLIC_APP_DOMAIN}/api/auth"
NEXTAUTH_JWT_ACCESS_TOKEN_EXPIRATION='1d'
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=

# This was inserted by `prisma init`:
# Environment variables declared in this file are automatically made available to Prisma.
Expand Down
4 changes: 2 additions & 2 deletions apps/admin/.env.local.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ NEXT_PUBLIC_SERVER_API_URL=http://localhost:3000/api
NEXT_PUBLIC_SERVER_TRPC_URL=http://localhost:3000/api/trpc

#Auth
NEXTAUTH_SECRET=
NEXTAUTH_SECRET= #openssl rand -base64 32
NEXTAUTH_URL=http://localhost:3002/api/auth
NEXTAUTH_JWT_ACCESS_TOKEN_EXPIRATION='1d'
GOOGLE_CLIENT_ID=
Expand All @@ -20,7 +20,7 @@ GOOGLE_CLIENT_SECRET=
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings
DATABASE_USERNAME=
DATABASE_PASSWORD=
DATABASE_HOST=127.0.0.1
DATABASE_HOST=localhost
DATABASE_PORT=27017
DATABASE_NAME=
DATABASE_URL="mongodb://${DATABASE_USERNAME}:${DATABASE_PASSWORD}@${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_NAME}?replicaSet=rs0&retryWrites=true&w=majority&directConnection=true&authSource=admin"
6 changes: 4 additions & 2 deletions apps/admin/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ RUN turbo prune --scope=${APP_NAME} --docker
FROM base AS builder
ARG APP_NAME=admin
ARG BUILD_MODE=local
ARG APP_DOMAIN
ARG NEXT_PUBLIC_APP_DOMAIN
ARG NEXT_PUBLIC_SERVER_DOMAIN

WORKDIR /app

Expand All @@ -37,7 +38,8 @@ COPY --from=pruner /app/tsconfig.json ./tsconfig.json
# First install the dependencies (as they change less often)
RUN pnpm install

ENV APP_DOMAIN=$APP_DOMAIN
ENV NEXT_PUBLIC_APP_DOMAIN=$NEXT_PUBLIC_APP_DOMAIN
ENV NEXT_PUBLIC_SERVER_DOMAIN=$NEXT_PUBLIC_SERVER_DOMAIN
ENV NEXT_TELEMETRY_DISABLED 1

COPY apps/${APP_NAME}/.env.docker.${BUILD_MODE}.example ./apps/${APP_NAME}/.env.local
Expand Down
10 changes: 5 additions & 5 deletions apps/client/.env.docker.prod.example
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
APP_PORT=3001
APP_DOMAIN=
SERVER_DOMAIN=

NEXT_PUBLIC_BASE_URL="https://${APP_DOMAIN}"
NEXT_PUBLIC_SERVER_URL="https://${SERVER_DOMAIN}"
NEXT_PUBLIC_SERVER_TRPC_URL="https://${SERVER_DOMAIN}/api/trpc"
NEXT_PUBLIC_APP_DOMAIN=
NEXT_PUBLIC_SERVER_DOMAIN=
NEXT_PUBLIC_BASE_URL="http://${NEXT_PUBLIC_APP_DOMAIN}"
NEXT_PUBLIC_SERVER_URL="http://${NEXT_PUBLIC_SERVER_DOMAIN}"
NEXT_PUBLIC_SERVER_TRPC_URL="http://${NEXT_PUBLIC_SERVER_DOMAIN}/api/trpc"

# This was inserted by `prisma init`:
# Environment variables declared in this file are automatically made available to Prisma.
Expand Down
2 changes: 1 addition & 1 deletion apps/client/.env.local.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ NEXT_PUBLIC_SERVER_TRPC_URL=http://localhost:3000/trpc
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings
DATABASE_USERNAME=
DATABASE_PASSWORD=
DATABASE_HOST=127.0.0.1
DATABASE_HOST=localhost
DATABASE_PORT=27017
DATABASE_NAME=
DATABASE_URL="mongodb://${DATABASE_USERNAME}:${DATABASE_PASSWORD}@${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_NAME}?replicaSet=rs0&retryWrites=true&w=majority&directConnection=true&authSource=admin"
8 changes: 4 additions & 4 deletions apps/client/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ COPY --from=pruner /app/tsconfig.json ./tsconfig.json
RUN pnpm install

ARG BUILD_MODE=local
ARG APP_DOMAIN
ARG SERVER_DOMAIN
ARG NEXT_PUBLIC_APP_DOMAIN
ARG NEXT_PUBLIC_SERVER_DOMAIN

ENV APP_DOMAIN=$APP_DOMAIN
ENV SERVER_DOMAIN=$SERVER_DOMAIN
ENV NEXT_PUBLIC_APP_DOMAIN=$NEXT_PUBLIC_APP_DOMAIN
ENV NEXT_PUBLIC_SERVER_DOMAIN=$NEXT_PUBLIC_SERVER_DOMAIN
ENV NEXT_TELEMETRY_DISABLED 1

COPY apps/${APP_NAME}/.env.docker.${BUILD_MODE}.example ./apps/${APP_NAME}/.env.local
Expand Down
8 changes: 4 additions & 4 deletions apps/server/.env.docker.prod.example
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
APP_PORT=3000
APP_DOMAIN=
SERVER_DOMAIN=
ADMIN_DOMAIN=
APP_API=api
APP_TRPC=trpc
APP_SWAGER=api-docs
APP_STATIC_ASSETS=public
APP_BASE_URL="https://${APP_DOMAIN}"
APP_ADMIN_URL="https://${SERVER_DOMAIN}"
APP_BASE_URL="http://${APP_DOMAIN}"
APP_ADMIN_URL="http://${ADMIN_DOMAIN}"

# This was inserted by `prisma init`:
# Environment variables declared in this file are automatically made available to Prisma.
Expand All @@ -17,6 +17,6 @@ APP_ADMIN_URL="https://${SERVER_DOMAIN}"
DATABASE_USERNAME=
DATABASE_PASSWORD=
DATABASE_HOST=mongo
DATABASE_PORT=27018
DATABASE_PORT=27017
DATABASE_NAME=
DATABASE_URL="mongodb://${DATABASE_USERNAME}:${DATABASE_PASSWORD}@${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_NAME}?replicaSet=rs0&retryWrites=true&w=majority&directConnection=true&authSource=admin"
Loading

0 comments on commit 7720a8c

Please sign in to comment.