Skip to content

Commit

Permalink
build: use makefile for local setup, run migrations from container
Browse files Browse the repository at this point in the history
  • Loading branch information
Jameskmonger committed Aug 26, 2023
1 parent 87a7b7e commit 6ef770b
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 18 deletions.
41 changes: 36 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,45 @@
# =====

all: install build db server
rebuild: build server

# =====

install:
@echo "Setting up the project..."
yarn

build:
@echo "Building the docker images..."
docker compose -f docker-compose.yml build

db:
@echo "Setting up the database and running migrations..."
yarn dockerup-db

@read -p "Enter the DATABASE_URL for seeding: " DATABASE_URL; \
DATABASE_URL=$$DATABASE_URL yarn workspace @creature-chess/data prisma-migrate deploy
docker compose -f docker-compose.db.yml up -d postgres
docker compose run -e DATABASE_URL nodejs-builder yarn workspace @creature-chess/data prisma migrate deploy

server:
@echo "Running the game..."
yarn dockerup
docker compose -f docker-compose.yml up -d

down:
@echo "Stopping the game..."
docker compose -f docker-compose.yml down
@echo "Stopping the database..."
docker compose -f docker-compose.db.yml down

# =====

# This step is used to create a new migration.
# It creates a new nodejs-builder container, runs the migration command inside,
# then copies the migration files to the local machine.
add-migration:
@echo "Creating new migration"
read -p "Enter the migration name: " MIGRATION_NAME; \
docker compose -f docker-compose.db.yml up -d postgres; \
docker compose run -e DATABASE_URL nodejs-builder yarn workspace @creature-chess/data prisma migrate dev --name $$MIGRATION_NAME; \
CONTAINER_ID=$$(docker ps -aqf "ancestor=nodejs-builder" --latest); \
docker cp $$CONTAINER_ID:/code/modules/@creature-chess/data/prisma/migrations ./modules/@creature-chess/data/prisma/; \
docker stop $$CONTAINER_ID; \
docker rm $$CONTAINER_ID; \
yarn workspace @creature-chess/data prisma generate
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ See "Environment variables" above for info on how to store them.

### Setup

You can use the following `make` commands to set up the project:
To build and run the project, you can use `make`:

- `make install` - install dependencies
- `make db` - seed the database
- take the `DATABASE_URL` from `.env` and input it when asked.
- you may need to configure the connection string to use `localhost` instead of `postgres` as the host, depending on your setup.
- `make server` - build and run the docker containers
```shell
make
```

If you change the server, you can run `make rebuild` to rebuild and restart the server.

### Dev toolkit

Expand Down
18 changes: 15 additions & 3 deletions apps/nodejs-builder.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,27 @@ ADD modules/@shoki/networking/package.json ./modules/@shoki/networking/
ADD modules/@tools/battle-tester/package.json ./modules/@tools/battle-tester/

# Run the install now that we have the package.json and yarn.lock files

RUN yarn install --frozen-lockfile --network-timeout 1000000

# Finally, copy the rest of the source code into the image
# These steps happen after the install, so that we don't have to re-run the
# install every time one of these steps has a different result

ADD tsconfig.json ./
ADD modules/ ./modules/

# Copy and build the `@shoki` packages (excluding board-react)
ADD modules/@shoki/ ./modules/@shoki/
RUN yarn workspaces foreach --include "@shoki/*" --exclude "@shoki/board-react" run build

# Copy and build the @creature-chess/models
ADD modules/@creature-chess/models/ ./modules/@creature-chess/models/
RUN yarn workspace @creature-chess/models run build

# Copy and build the @creature-chess/data
ADD modules/@creature-chess/data/ ./modules/@creature-chess/data/
RUN yarn workspace @creature-chess/data prisma-generate
RUN yarn workspaces foreach --exclude "@creature-chess-app/*" run build
RUN yarn workspace @creature-chess/data run build

# Copy and build the remaining `@creature-chess` packages
ADD modules/@creature-chess/ ./modules/@creature-chess/
RUN yarn workspaces foreach --include "@creature-chess/*" --exclude "@creature-chess/models" --exclude "@creature-chess/data" run build
9 changes: 9 additions & 0 deletions docker-compose.db.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
version: "3.8"

networks:
creature-chess:
name: creature-chess
driver: bridge

volumes:
pgdata:
pgadmin:
Expand All @@ -22,6 +27,8 @@ services:
timeout: 5s
start_period: 10s
retries: 20
networks:
- creature-chess
# pgadmin, a local web UI for managing the DB
pgadmin:
image: dpage/pgadmin4
Expand All @@ -33,6 +40,8 @@ services:
- POSTGRES_PASSWORD
ports:
- 5050:80
networks:
- creature-chess
volumes:
- pgadmin:/root/.pgadmin
depends_on:
Expand Down
13 changes: 13 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
version: "3.8"

networks:
creature-chess:
name: creature-chess
external: true

services:
# this is the base image for all nodejs-based apps
nodejs-builder:
build:
context: .
dockerfile: apps/nodejs-builder.Dockerfile
image: nodejs-builder
networks:
- creature-chess
# this image builds the web-game app
# TODO (James) we could extend this in local dev to also watch for changes
web-game-builder:
Expand Down Expand Up @@ -67,6 +74,8 @@ services:
ports:
- 80:80
- 443:443
networks:
- creature-chess
depends_on:
web-game-builder:
condition: service_completed_successfully
Expand Down Expand Up @@ -95,6 +104,8 @@ services:
- APP_DIR=server-game
ports:
- "3000"
networks:
- creature-chess
restart: always
depends_on:
nodejs-builder:
Expand All @@ -116,6 +127,8 @@ services:
- APP_DIR=server-info
ports:
- "3000"
networks:
- creature-chess
restart: always
depends_on:
nodejs-builder:
Expand Down
4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
"start-server-info": "ts-node-dev apps/server-info/index.ts",
"start-web-menu": "yarn workspace @creature-chess-app/web-menu dev",
"test": "yarn workspaces foreach run test",
"dockerup-db": "docker-compose --env-file ./.env -f docker-compose.db.yml up -d",
"dockerup-db-live": "docker-compose --env-file ./.env.live -f docker-compose.db.yml up -d",
"dockerup": "docker-compose --env-file ./.env -f docker-compose.yml -f docker-compose.db.yml build && docker-compose --env-file ./.env -f docker-compose.yml -f docker-compose.db.yml up",
"dockerup-live": "docker-compose --env-file ./.env.live build && docker-compose --env-file ./.env.live up -d",
"storybook": "start-storybook -p 6006",
"build-storybook": "build-storybook -o ./output",
"lint": "eslint \"apps/**/*.ts\" \"modules/**/*.ts\" \"apps/**/*.tsx\" \"modules/**/*.tsx\"",
Expand Down

0 comments on commit 6ef770b

Please sign in to comment.