diff --git a/Dockerfile b/Dockerfile index 6615cfdf1..fb6cad03b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,23 +1,21 @@ # Use an official Node runtime as the base image FROM node:18 +# Add environment variable +ARG GAME_ENV=preprod +ENV GAME_ENV=$GAME_ENV + # Set the working directory in the container WORKDIR /usr/src/app - # Copy package.json and package-lock.json COPY package*.json ./ - # Install dependencies RUN npm install - # Copy the rest of the application code COPY . . - # Build the client-side application RUN npm run build-prod - # Expose the port the app runs on EXPOSE 3000 - # Define the command to run the app CMD ["npm", "run", "start:server"] \ No newline at end of file diff --git a/cloudbuild.yaml b/cloudbuild.yaml index b0ac5b7fb..fd22a7d09 100644 --- a/cloudbuild.yaml +++ b/cloudbuild.yaml @@ -1,12 +1,15 @@ steps: # Build the Docker image - name: 'gcr.io/cloud-builders/docker' - args: ['build', '-t', 'us-central1-docker.pkg.dev/$PROJECT_ID/openfrontio/game-server:${TAG_NAME}-${SHORT_SHA}', '.'] - + args: [ + 'build', + '-t', 'us-central1-docker.pkg.dev/$PROJECT_ID/openfrontio/game-server:${TAG_NAME}-${SHORT_SHA}', + '--build-arg', 'GAME_ENV=${_GAME_ENV}', + '.' + ] # Push the image to Artifact Registry - name: 'gcr.io/cloud-builders/docker' args: ['push', 'us-central1-docker.pkg.dev/$PROJECT_ID/openfrontio/game-server:${TAG_NAME}-${SHORT_SHA}'] - # Update the GCE instance with the new container image - name: 'gcr.io/cloud-builders/gcloud' args: @@ -17,14 +20,12 @@ steps: - '--container-image' - 'us-central1-docker.pkg.dev/$PROJECT_ID/openfrontio/game-server:${TAG_NAME}-${SHORT_SHA}' - '--zone=us-central1-a' - substitutions: _INSTANCE_NAME: 'openfrontio-dev-instance' + _GAME_ENV: 'preprod' # Default to preprod TAG_NAME: 'dev' - options: substitutionOption: 'ALLOW_LOOSE' logging: CLOUD_LOGGING_ONLY - images: - 'us-central1-docker.pkg.dev/$PROJECT_ID/openfrontio/game-server:${TAG_NAME}-${SHORT_SHA}' \ No newline at end of file diff --git a/package.json b/package.json index a5db6efc8..c136213c0 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "build-dev": "webpack --config webpack.config.js --mode development", "build-prod": "webpack --config webpack.config.js --mode production", "start:client": "webpack serve --open --node-env development", - "start:server": "GAME_ENV=prod node --loader ts-node/esm --experimental-specifier-resolution=node src/server/Server.ts", + "start:server": "node --loader ts-node/esm --experimental-specifier-resolution=node src/server/Server.ts", "start:server-dev": "GAME_ENV=dev node --loader ts-node/esm --experimental-specifier-resolution=node src/server/Server.ts", "dev": "GAME_ENV=dev concurrently \"npm run start:client\" \"npm run start:server-dev\"", "tunnel": "npm run build-prod && npm run start:server", @@ -94,4 +94,4 @@ "zod": "^3.23.8" }, "type": "module" -} +} \ No newline at end of file diff --git a/src/core/configuration/Config.ts b/src/core/configuration/Config.ts index d08524820..b9f3ff82d 100644 --- a/src/core/configuration/Config.ts +++ b/src/core/configuration/Config.ts @@ -3,6 +3,8 @@ import { Colord, colord } from "colord"; import { devConfig } from "./DevConfig"; import { defaultConfig } from "./DefaultConfig"; import { GameID } from "../Schemas"; +import { preprodConfig } from "./PreprodConfig"; +import { prodConfig } from "./ProdConfig"; export enum GameEnv { Dev, @@ -10,13 +12,18 @@ export enum GameEnv { } export function getConfig(): Config { - // TODO: 'prod' not found in prod env - if (process.env.GAME_ENV == 'dev') { - console.log('Using dev config') - return devConfig - } else { - console.log('Using prod config') - return defaultConfig + switch (process.env.GAME_ENV) { + case 'dev': + console.log('using dev config') + return devConfig + case 'preprod': + console.log('using preprod config') + return preprodConfig + case 'prod': + console.log('using prod config') + return prodConfig + default: + throw Error(`unsupported server configuration: ${process.env.GAME_ENV}`) } } @@ -25,6 +32,7 @@ export function getGameEnv(): GameEnv { } export interface Config { + discordBotSecret(): string theme(): Theme; percentageTilesOwnedToWin(): number turnIntervalMs(): number diff --git a/src/core/configuration/DefaultConfig.ts b/src/core/configuration/DefaultConfig.ts index 211b12bf4..8a2eccc34 100644 --- a/src/core/configuration/DefaultConfig.ts +++ b/src/core/configuration/DefaultConfig.ts @@ -6,7 +6,9 @@ import { pastelTheme } from "./PastelTheme"; -export class DefaultConfig implements Config { +export abstract class DefaultConfig implements Config { + abstract discordBotSecret(): string + difficultyModifier(difficulty: Difficulty): number { switch (difficulty) { case Difficulty.Easy: diff --git a/src/core/configuration/PreprodConfig.ts b/src/core/configuration/PreprodConfig.ts new file mode 100644 index 000000000..f7045fc18 --- /dev/null +++ b/src/core/configuration/PreprodConfig.ts @@ -0,0 +1,5 @@ +import { DefaultConfig } from "./DefaultConfig"; + +export const preprodConfig = new class extends DefaultConfig { + +} \ No newline at end of file diff --git a/src/core/configuration/ProdConfig.ts b/src/core/configuration/ProdConfig.ts new file mode 100644 index 000000000..23cbeaf22 --- /dev/null +++ b/src/core/configuration/ProdConfig.ts @@ -0,0 +1,5 @@ +import { DefaultConfig } from "./DefaultConfig"; + +export const prodConfig = new class extends DefaultConfig { + +} \ No newline at end of file diff --git a/update-deploy.sh b/update-deploy.sh index 40759413f..190316a6e 100755 --- a/update-deploy.sh +++ b/update-deploy.sh @@ -1,5 +1,4 @@ #!/bin/bash - # Check if the --env flag is provided if [[ "$1" != "--env" ]]; then echo "Usage: $0 --env [dev|prod]" @@ -19,20 +18,22 @@ fi if [[ "$ENV" == "dev" ]]; then INSTANCE_NAME="openfrontio-dev-instance" TAG="dev" + GAME_ENV="preprod" # Set game environment to preprod for dev echo "[DEV] Deploying to openfront.dev" else INSTANCE_NAME="openfrontio-instance" TAG="latest" + GAME_ENV="prod" # Set game environment to prod for prod echo "[PROD] Deploying to openfront.io" fi # Ensure you're authenticated with Google Cloud gcloud auth configure-docker us-central1-docker.pkg.dev -# Build the new Docker image -docker build -t openfrontio . +# Build the new Docker image with GAME_ENV build argument +docker build --build-arg GAME_ENV=$GAME_ENV -t openfrontio . -# Tag the new image (use a version number or 'latest') +# Tag the new image docker tag openfrontio us-central1-docker.pkg.dev/openfrontio/openfrontio/game-server:$TAG # Push the new image to Google Container Registry @@ -40,9 +41,7 @@ docker push us-central1-docker.pkg.dev/openfrontio/openfrontio/game-server:$TAG # Prune Docker system on the instance gcloud compute ssh $INSTANCE_NAME --zone us-central1-a --command 'docker system prune -f -a' - gcloud compute ssh $INSTANCE_NAME --zone us-central1-a --command 'docker kill $(docker ps -q)' - gcloud compute ssh $INSTANCE_NAME --zone us-central1-a --command 'docker rmi $(docker images -q) -f' # Update the GCE instance with the new container image