Skip to content

Commit

Permalink
feat: Improve local setup
Browse files Browse the repository at this point in the history
– use Docker for local setup
– update README
– update CI scripts
  • Loading branch information
letehaha committed Sep 14, 2024
1 parent a9324bc commit fbeddd1
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 49 deletions.
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*
4 changes: 2 additions & 2 deletions .github/actions/docker-build/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ runs:
id: docker-build
uses: docker/build-push-action@v2
with:
context: ./
file: ./Dockerfile
context: ../..
file: ./docker/prod/Dockerfile
push: ${{ inputs.push == 'true' }}
tags: ${{ inputs.docker-hub-username }}/budget-tracker-be:latest
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 --remove-orphans
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"]
17 changes: 10 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,23 @@
"dev": "cross-env NODE_ENV=development nodemon",
"prod": "cross-env NODE_ENV=production nodemon",
"migrate:dev": "cross-env NODE_ENV=development npx sequelize-cli db:migrate",
"migrate:dev:undo": "cross-env NODE_ENV=development npx sequelize-cli db:migrate:undo",
"migrate": "cross-env NODE_ENV=production npx sequelize-cli db:migrate",
"migrate-undo:dev": "cross-env NODE_ENV=development npx sequelize-cli db:migrate:undo",
"migrate-undo": "cross-env NODE_ENV=production npx sequelize-cli db:migrate:undo",
"seed": "npx sequelize-cli db:seed:all",
"seed-undo": "npx sequelize-cli db:seed:undo:all",
"migrate:undo": "cross-env NODE_ENV=production npx sequelize-cli db:migrate:undo",
"db:reset": "cross-env NODE_ENV=test npx sequelize-cli db:drop && npx sequelize-cli db:create && npx sequelize-cli db:migrate",
"pretest": "cross-env NODE_ENV=test npm run db:reset",
"test": "cross-env NODE_ENV=test npm run test:unit && npm run test:e2e",
"test:unit": "cross-env NODE_ENV=test jest -c jest.config.unit.ts --passWithNoTests --forceExit --detectOpenHandles",
"test:e2e": "cross-env NODE_ENV=test jest -c jest.config.e2e.ts --runInBand --passWithNoTests --forceExit --detectOpenHandles",
"lint": "eslint .",
"docker-build": "docker build . -t letehaha/budget-tracker-be",
"docker-push": "docker push letehaha/budget-tracker-be",
"docker-compose": "npm run docker-build && npm run docker-push",
"docker:dev": "docker compose --env-file .env.development -f ./docker/dev/docker-compose.yml up --build",
"docker:dev:ps": "docker compose --env-file .env.development -f ./docker/dev/docker-compose.yml ps",
"docker:dev:down": "docker compose --env-file .env.development -f ./docker/dev/docker-compose.yml down",
"docker:dev:destroy": "./docker/dev/docker-destroy.sh",
"docker:dev:run-in-container": "docker compose --env-file .env.development -f ./docker/dev/docker-compose.yml exec app",
"docker:prod:build": "docker build . -t letehaha/budget-tracker-be -f ./docker/prod/Dockerfile",
"docker:prod:push": "docker push letehaha/budget-tracker-be",
"docker:prod:build-push": "npm run docker-build && npm run docker-push",
"postinstall": "chmod +x ./post-install.sh && ./post-install.sh",
"prepare": "husky install"
},
Expand Down
13 changes: 12 additions & 1 deletion post-install.sh
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
git config blame.ignoreRevsFile .git-blame-ignore-revs
#!/bin/sh
set -e

# Check if we're in a git repository
if git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
echo "Running git commands..."
git config blame.ignoreRevsFile .git-blame-ignore-revs
else
echo "Not in a git repository, skipping git commands."
fi

chmod +x ./docker/dev/docker-destroy.sh
6 changes: 1 addition & 5 deletions src/migrations/1664386509637-exchange-rates.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ module.exports = {
const transaction = await queryInterface.sequelize.transaction();

try {
let data = {};

if (isTest) {
data.data = JSON.parse(fs.readFileSync('./src/tests/test-exchange-rates.json'));
}
let data = { data: JSON.parse(fs.readFileSync('./src/tests/test-exchange-rates.json')) };

const currencies = await queryInterface.sequelize.query('SELECT * FROM "Currencies"', {
type: QueryTypes.SELECT,
Expand Down

0 comments on commit fbeddd1

Please sign in to comment.