Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Improve local setup #129

Merged
merged 2 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
**/node_modules
node_modules
npm-debug.log
.env*
7 changes: 7 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@ API_LAYER_API_KEY=iVH7l3yBziOKwGSO7jYWYt1RDtb05oKf
APPLICATION_HOST=127.0.0.1
APPLICATION_PORT=8081
APPLICATION_JWT_SECRET=development
# for .env.test use docker/test/docker-compose db service name
APPLICATION_DB_HOST=127.0.0.1
APPLICATION_DB_USERNAME=
APPLICATION_DB_PASSWORD=
APPLICATION_DB_DATABASE=budget-tracker
APPLICATION_DB_PORT=5432
APPLICATION_DB_DIALECT=postgres
# for .env.test use docker/test/docker-compose redis service name
APPLICATION_REDIS_HOST=127.0.0.1

# Tests configurations
# e2e tests are running in parallel, so we need a strict amount of workers,
# so then we can dynamically create the same amount of DBs
JEST_WORKERS_AMOUNT=4
8 changes: 4 additions & 4 deletions .github/actions/docker-build/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ runs:
using: composite
steps:
- name: Login to Docker Hub
uses: docker/login-action@v1
uses: docker/login-action@v3
with:
username: ${{ inputs.docker-hub-username }}
password: ${{ inputs.docker-hub-access-token }}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
uses: docker/setup-buildx-action@v3

- name: Docker build
id: docker-build
uses: docker/build-push-action@v2
uses: docker/build-push-action@v6
with:
context: ./
file: ./Dockerfile
file: ./docker/prod/Dockerfile
push: ${{ inputs.push == 'true' }}
tags: ${{ inputs.docker-hub-username }}/budget-tracker-be:latest
33 changes: 8 additions & 25 deletions .github/workflows/check-source-code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
name: Prepare local deps
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- id: prepare-env
uses: ./.github/actions/prepare-local-env
- name: Install dependencies
Expand All @@ -23,7 +23,7 @@ jobs:
needs: prepare-dependencies
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- uses: ./.github/actions/prepare-local-env
- name: Lint source code
run: npm run lint
Expand All @@ -33,30 +33,11 @@ jobs:
needs: prepare-dependencies
runs-on: ubuntu-latest
environment: test
services:
postgres:
image: postgres:11.12-stretch
env:
POSTGRES_DB: budget-tracker_test
POSTGRES_USER: ${{ secrets.CI_POSTGRES_USER }}
POSTGRES_PASSWORD: ${{ secrets.CI_POSTGRES_PASSWORD }}
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
redis:
image: redis:latest
ports:
- 6379:6379

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- uses: ./.github/actions/prepare-local-env
- name: Make envfile
uses: SpicyPizza/create-envfile@v1
uses: SpicyPizza/create-envfile@v2
with:
envkey_APPLICATION_HOST: ${{ secrets.APPLICATION_HOST }}
envkey_APPLICATION_PORT: ${{ secrets.APPLICATION_PORT }}
Expand All @@ -68,6 +49,7 @@ jobs:
envkey_APPLICATION_DB_PORT: ${{ secrets.APPLICATION_DB_PORT }}
envkey_APPLICATION_DB_DIALECT: ${{ secrets.APPLICATION_DB_DIALECT }}
envkey_APPLICATION_REDIS_HOST: ${{ secrets.APPLICATION_REDIS_HOST }}
envkey_JEST_WORKERS_AMOUNT: ${{ secrets.JEST_WORKERS_AMOUNT }}
directory: ./
file_name: .env.test
- name: Unit and e2e testing
Expand All @@ -78,9 +60,9 @@ jobs:
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Make envfile
uses: SpicyPizza/create-envfile@v1
uses: SpicyPizza/create-envfile@v2
with:
envkey_APPLICATION_HOST: ${{ secrets.APPLICATION_HOST }}
envkey_APPLICATION_PORT: ${{ secrets.APPLICATION_PORT }}
Expand All @@ -92,6 +74,7 @@ jobs:
envkey_APPLICATION_DB_PORT: ${{ secrets.APPLICATION_DB_PORT }}
envkey_APPLICATION_DB_DIALECT: ${{ secrets.APPLICATION_DB_DIALECT }}
envkey_APPLICATION_REDIS_HOST: ${{ secrets.APPLICATION_REDIS_HOST }}
envkey_JEST_WORKERS_AMOUNT: ${{ secrets.JEST_WORKERS_AMOUNT }}
directory: ./
file_name: .env.production
- uses: ./.github/actions/docker-build
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/image-to-docker-hub.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ jobs:

steps:
- name: Check Out Repo
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Make envfile
uses: SpicyPizza/create-envfile@v1
uses: SpicyPizza/create-envfile@v2
with:
envkey_APPLICATION_HOST: ${{ secrets.APPLICATION_HOST }}
envkey_APPLICATION_PORT: ${{ secrets.APPLICATION_PORT }}
Expand All @@ -30,6 +30,7 @@ jobs:
envkey_APPLICATION_DB_PORT: ${{ secrets.APPLICATION_DB_PORT }}
envkey_APPLICATION_DB_DIALECT: ${{ secrets.APPLICATION_DB_DIALECT }}
envkey_APPLICATION_REDIS_HOST: ${{ secrets.APPLICATION_REDIS_HOST }}
envkey_JEST_WORKERS_AMOUNT: ${{ secrets.JEST_WORKERS_AMOUNT }}
directory: ./
file_name: .env.production

Expand Down
89 changes: 56 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
# budget-tracker-be

Budget tracker back-end
## First run instructions

### First run instructions
You need to make sure you have Docker installed. Current instruction is written
for the Docker v4.34.

Required stack (WIP): Postgres v11, Node version specified in `.nvmrc`
### 1. Install dependencies

1. Install dependencies
Use `npm ci` to install all the dependencies with correct versions.

```sh
npm i
```

2. Create corresponding env variables.
### 2. Create corresponding env variables.

Project uses different `.env` files for each environment: `.env.development`,
`.env.production`, `.env.test`.
Expand All @@ -25,38 +22,36 @@ variables.
since when tests are running, they're automatically filling and cleaning the DB,
so all your data from the DB used for development might be lost.

3. Run migrations
### 3. Start dev server

```sh
npm run migrate:dev
```
Run `npm run docker:dev` or `npm run docker:dev -- -d` to run it "in background".

If you have an error running this command, you probably need to install Postgres. Read [the guide below](#setup-postgres).
It will be working in HMR mode, so any changes to the codebase will be reflected.

If you encountered any errors during `npm run migrate:dev`, you can run
`npm run migrate-undo:dev` to undo migrations. If you still facing issues, you
can clear the DB using these two commands:
### 4. That's it ! 🎉🎉🎉

```sh
drop schema public cascade;
create schema public;
```
Now it should be accessible under the port that you defined in the `.env.development` file.

They will completely clean the DB and you will be able to run migrations again.
### Troubleshoting:

4. Start dev server
1. Sometimes when running `npm run docker:dev` it might stuck running migrations
due to DB connection issues. It's a very rare case, but if this happens,
_**simply run the command again**_.

```sh
npm run dev
```
### Useful command for local development:

### If you didn't work on it for long time
1. `npm run docker:dev:down` to stop containers. All the data will still be stored in the DB.
2. `npm run docker:dev:destroy` stops containers, and _**Completely destroys all the images, container and volumes**_. It means all the data will be erased from the DB. Useful when you want to test new migrations, or DB structure was damaged.
3. Use `docker:dev:run-in-container -- <some command>` to run any command inside running docker container. For example `docker:dev:run-in-container -- npm run migrate:dev` to run migrations and `docker:dev:run-in-container -- npm run migrate:dev:undo` to undo them.

1. Make sure Postres v11 is running
2. Run `nvm use`
3. That's it!
<hr>

### Setup Postgres
### If you don't want to use Docker

For whatever reason if you don't want not to use Docker, you still need to complete
first 2 steps described above, and then follow these instructions:

### 3. Setup Postgres

If you can access your user and you know how to create a DB, **you can ignore that section**.

Expand Down Expand Up @@ -92,8 +87,36 @@ CREATE DATABASE "budget-tracker";

7. That's it.

To install Redis (if you don't have one):
### 4. Install Redis (if you don't have one):

1. Install Redis via `brew install redis`
2. Then `brew services start redis`
3. You're done :)

### 5. Run migrations

```sh
npm run migrate:dev
```

If you have an error running this command, you probably need to install Postgres. Read [the guide below](#setup-postgres).

If you encountered any errors during `npm run migrate:dev`, you can run
`npm run migrate-undo:dev` to undo migrations. If you still facing issues, you
can clear the DB using these two commands:

```sh
drop schema public cascade;
create schema public;
```

They will completely clean the DB and you will be able to run migrations again.

### 6. Start dev server

```sh
npm run dev
```

### 7. That's it! 🎉🎉🎉

But better use Docker 🙈
17 changes: 17 additions & 0 deletions docker/dev/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM node:21.7.3
WORKDIR /app

# Copy the rest of the application
COPY . .

# Install dependencies
RUN chmod +x ./post-install.sh
RUN npm ci

ENV NODE_ENV=development

# Prepare and execute entrypoint script
RUN chmod +x /app/docker/dev/docker-entrypoint.sh
ENTRYPOINT ["/app/docker/dev/docker-entrypoint.sh"]

CMD ["/bin/sh", "-c", "npm run dev"]
31 changes: 31 additions & 0 deletions docker/dev/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
services:
app:
build:
context: ../..
dockerfile: docker/dev/Dockerfile
container_name: dev-budget-tracker-app
volumes: ['../../:/app', '/app/node_modules']
ports: ['${APPLICATION_PORT}:${APPLICATION_PORT}']
depends_on: ['db', 'redis']
env_file: ../../.env.development

db:
image: postgres:16
restart: always
container_name: dev-budget-tracker-db
volumes: ['db_data:/var/lib/postgresql/data']
environment:
- POSTGRES_USER=${APPLICATION_DB_USERNAME}
- POSTGRES_PASSWORD=${APPLICATION_DB_PASSWORD}
- POSTGRES_DB=${APPLICATION_DB_DATABASE}
ports: ['${APPLICATION_DB_PORT}:5432']

redis:
image: redis:6
container_name: dev-budget-tracker-redis
volumes: ['redis_data:/data']
ports: ['6379:6379']

volumes:
db_data:
redis_data:
6 changes: 6 additions & 0 deletions docker/dev/docker-destroy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

echo "Starting removing all dev container completely..."

npm run docker:dev -- -d
npm run docker:dev:down -- --rmi all --volumes
17 changes: 17 additions & 0 deletions docker/dev/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh
set -e

echo "Starting entrypoint script"

# Run migrations
echo "Running migrations..."
if npm run migrate:dev; then
echo "Migrations completed successfully"
else
echo "Migration failed. Exiting..."
exit 1
fi

# If we get here, migrations were successful
echo "Starting the application..."
exec "$@"
9 changes: 8 additions & 1 deletion Dockerfile → docker/prod/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
FROM node:21.7.3
WORKDIR /app

# Copy the rest of the application
COPY . .
RUN npm ci

ENV NODE_ENV=production

# Install dependencies
RUN chmod +x ./post-install.sh
RUN npm ci

CMD ["/bin/sh", "-c", "npm run prod"]
19 changes: 19 additions & 0 deletions docker/test/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM node:21.7.3

WORKDIR /app

# Copy package.json and package-lock.json files. This allows Docker to cache the
# npm dependencies as long as these files don't change.
COPY package*.json ./

# Install dependencies
COPY post-install.sh ./
COPY docker ./docker
RUN chmod +x ./post-install.sh
RUN npm ci

# Copy the rest of the application
COPY . .

# Run this command to keep container alive. Without it will be demounted right after deps installation
CMD ["tail", "-f", "/dev/null"]
Loading
Loading