From 8152dde54f3fd374c822b0dd852b9d4de86d650d Mon Sep 17 00:00:00 2001 From: aster <137767097+aster-void@users.noreply.github.com> Date: Thu, 6 Mar 2025 14:38:12 +0900 Subject: [PATCH 01/10] fix flake --- server/prisma.nix | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/server/prisma.nix b/server/prisma.nix index bdb062f5..63cb161f 100644 --- a/server/prisma.nix +++ b/server/prisma.nix @@ -6,13 +6,13 @@ (prisma-utils.lib.prisma-factory { inherit pkgs; - prisma-fmt-hash = "sha256-atD5GZfmeU86mF1V6flAshxg4fFR2ews7EwaJWZZzbc="; - query-engine-hash = "sha256-8FTZaKmQCf9lrDQvkF5yWPeZ7TSVfFjTbjdbWWEHgq4="; - libquery-engine-hash = "sha256-USIdaum87ekGY6F6DaL/tKH0BAZvHBDK7zjmCLo//kM="; - schema-engine-hash = "sha256-k5MkxXViEqojbkkcW/4iBFNdfhb9PlMEF1M2dyhfOok="; + prisma-fmt-hash = "sha256-iZuomC/KaLF0fQy6RVHwk2qq4DRaG3xj+sWmtLofiMU="; + query-engine-hash = "sha256-Pl/YpYu326qqpbVfczM5RxB8iWXZlewG9vToqzSPIQo="; + libquery-engine-hash = "sha256-ETwMIJMjMgZmjH6QGD7GVwYYlyx9mo2ydEeunFViCjQ="; + schema-engine-hash = "sha256-rzzzPHOpUM3GJvkhU08lQ7rNspdq3RKxMRRW9YZtvhU="; }) .fromBunLock - ./bun.lock; + ../bun.lock; inherit (prisma) package; in { inherit (prisma) shellHook; From b164715014f3b8b4cca256188b563a480786f7ec Mon Sep 17 00:00:00 2001 From: aster <137767097+aster-void@users.noreply.github.com> Date: Thu, 6 Mar 2025 16:31:46 +0900 Subject: [PATCH 02/10] deployed --- .dockerignore | 1 + .env.sample | 1 + Dockerfile | 37 +++++++++++++++++++++++++++++++++++++ README.md | 4 ++-- flake.nix | 1 + fly.toml | 24 ++++++++++++++++++++++++ package.json | 4 ++-- server/.gitignore | 3 +-- server/Dockerfile | 13 ------------- server/package.json | 7 +++++-- server/tsconfig.json | 1 + 11 files changed, 75 insertions(+), 21 deletions(-) create mode 100644 .dockerignore create mode 100644 .env.sample create mode 100644 Dockerfile create mode 100644 fly.toml delete mode 100644 server/Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..3c3629e6 --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +node_modules diff --git a/.env.sample b/.env.sample new file mode 100644 index 00000000..1e593430 --- /dev/null +++ b/.env.sample @@ -0,0 +1 @@ +SQL_GENERATE_URL=postgres://sql-url diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..4d89933f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,37 @@ +# syntax = docker/dockerfile:1 + +ARG BUN_VERSION=1.2.2 +FROM oven/bun:${BUN_VERSION}-slim AS base +LABEL fly_launch_runtime="Bun/Prisma" +ENV NODE_ENV="production" + +# Throw-away build stage to reduce size of final image +FROM base AS build +# RUN apt-get update -qq && \ + # apt-get install --no-install-recommends -y build-essential openssl pkg-config + +WORKDIR /build +ARG SQL_GENERATE_URL +ENV DATABASE_URL=$SQL_GENERATE_URL +RUN test -n "${DATABASE_URL}" +COPY . . +RUN --mount=type=cache,target=~/.bun/install bun install --frozen-lockfile --ignore-scripts +RUN cd server; bun prisma generate --sql +RUN cd server; bun run :build + + +# Final stage for app image +FROM base AS runner +WORKDIR /srv + +# Install packages needed for deployment +RUN apt-get update -qq && \ + apt-get install --no-install-recommends -y openssl && \ + rm -rf /var/lib/apt/lists /var/cache/apt/archives + +# Copy built application +COPY --from=build /build/server/target/index.js /srv/index.js + +# Start the server by default, this can be overwritten at runtime +EXPOSE 3000 +CMD [ "bun", "run", "./index.js" ] diff --git a/README.md b/README.md index 598d7edb..d321fbe4 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,6 @@ make prepare-deploy-web` server: ```sh -make prepare-deploy-server -make deploy-server +# prisma がビルド時に DATABASE_URL を要求するため +bun --env-file=./.env deploy:server ``` diff --git a/flake.nix b/flake.nix index c5786b20..79e4ae47 100644 --- a/flake.nix +++ b/flake.nix @@ -39,6 +39,7 @@ packages = (with pkgs; [ nix # HACK: to fix the side effect of the hack below, installing two instances of nix + flyctl gnumake nodejs biome diff --git a/fly.toml b/fly.toml new file mode 100644 index 00000000..0c62208c --- /dev/null +++ b/fly.toml @@ -0,0 +1,24 @@ +# fly.toml app configuration file generated for coursemate on 2025-03-06T14:33:10+09:00 +# +# See https://fly.io/docs/reference/configuration/ for information about how to use this file. +# + +app = 'coursemate' +primary_region = 'nrt' + +[build] + +[deploy] + +[http_service] +internal_port = 3000 +force_https = true +auto_stop_machines = 'stop' +auto_start_machines = true +min_machines_running = 0 +processes = ['app'] + +[[vm]] +memory = '512mb' +cpu_kind = 'shared' +cpus = 1 diff --git a/package.json b/package.json index fbe2eb32..4bc82a81 100644 --- a/package.json +++ b/package.json @@ -28,9 +28,9 @@ "watch:server": "cd server; bun run dev", "seed": "cd server; bun prisma db seed", "prepare:deploy:web": "bun install && bun run build:web", - "prepare:deploy:server": "bun install", "deploy:web": "cd web; bun run start --port $PORT", - "deploy:server": "cd server; bun run src/index.ts", + "deploy:server": "fly deploy --build-arg SQL_GENERATE_URL=$SQL_GENERATE_URL", + "docker:server": "docker build . --build-arg SQL_GENERATE_URL=$SQL_GENERATE_URL", "dev-db": "make dev-db", "test": "make test", "spell": "bunx cspell --quiet ." diff --git a/server/.gitignore b/server/.gitignore index c0bced27..d264edb8 100644 --- a/server/.gitignore +++ b/server/.gitignore @@ -4,6 +4,5 @@ node_modules .env .env.dev -# deprecated, delete /dist if you have one -/dist +# deprecated, delete /dist and if you have one /target diff --git a/server/Dockerfile b/server/Dockerfile deleted file mode 100644 index 99b960c8..00000000 --- a/server/Dockerfile +++ /dev/null @@ -1,13 +0,0 @@ -FROM oven/bun:latest -WORKDIR /coursemate/dev/server - -COPY package.json package-lock.json ./ -RUN npm ci - -COPY ./prisma ./ -RUN npx prisma generate - -COPY . . - -RUN npm run build -CMD npx prisma db push && npm run serve diff --git a/server/package.json b/server/package.json index 32d9a02d..bce5c52f 100644 --- a/server/package.json +++ b/server/package.json @@ -6,8 +6,11 @@ "scripts": { "prepare": "bun --env-file=./.env.dev prisma generate --sql || (echo 'please set DATABASE_URL in server/.env.dev' && exit 1)", "dev": "bun --watch src/index.ts", - "build": "tsc", - "serve": "bun target/main.js" + "check": "tsc", + "clean": "rm target -r || true", + "build": "bun clean && bun check && bun run :build", + ":build": "bun build ./src/index.ts --target bun --outfile target/index.js --minify", + "serve": "bun run ./a.js" }, "prisma": { "seed": "bun src/seeds/seed-test.ts" diff --git a/server/tsconfig.json b/server/tsconfig.json index ac133f64..20df3ed0 100644 --- a/server/tsconfig.json +++ b/server/tsconfig.json @@ -1,6 +1,7 @@ { "compilerOptions": { "target": "es2016", + "noEmit": true, "module": "commonjs", "rootDir": "./src", "outDir": "./target", From 91a1d52fe7daa597f554d50d6e8a5cf452867c26 Mon Sep 17 00:00:00 2001 From: aster <137767097+aster-void@users.noreply.github.com> Date: Thu, 6 Mar 2025 17:24:18 +0900 Subject: [PATCH 03/10] fix all --- Dockerfile | 11 +++-------- README.md | 4 ++-- package.json | 3 ++- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/Dockerfile b/Dockerfile index 4d89933f..20e69ad8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,12 @@ # syntax = docker/dockerfile:1 ARG BUN_VERSION=1.2.2 -FROM oven/bun:${BUN_VERSION}-slim AS base +FROM oven/bun:${BUN_VERSION} AS base LABEL fly_launch_runtime="Bun/Prisma" ENV NODE_ENV="production" # Throw-away build stage to reduce size of final image FROM base AS build -# RUN apt-get update -qq && \ - # apt-get install --no-install-recommends -y build-essential openssl pkg-config WORKDIR /build ARG SQL_GENERATE_URL @@ -24,13 +22,10 @@ RUN cd server; bun run :build FROM base AS runner WORKDIR /srv -# Install packages needed for deployment -RUN apt-get update -qq && \ - apt-get install --no-install-recommends -y openssl && \ - rm -rf /var/lib/apt/lists /var/cache/apt/archives - # Copy built application COPY --from=build /build/server/target/index.js /srv/index.js +COPY --from=build /build/node_modules/.prisma/client /node_modules/.prisma/client +COPY --from=build /build/node_modules/@img /node_modules/@img # Start the server by default, this can be overwritten at runtime EXPOSE 3000 diff --git a/README.md b/README.md index d321fbe4..d3b8945e 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,6 @@ make prepare-deploy-web` server: ```sh -# prisma がビルド時に DATABASE_URL を要求するため -bun --env-file=./.env deploy:server +# prisma がビルド時に DATABASE_URL を要求するため、 root に .env を作って、 .env.sample に従って埋めよう。 +bun deploy:server ``` diff --git a/package.json b/package.json index 4bc82a81..53a312ad 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,8 @@ "seed": "cd server; bun prisma db seed", "prepare:deploy:web": "bun install && bun run build:web", "deploy:web": "cd web; bun run start --port $PORT", - "deploy:server": "fly deploy --build-arg SQL_GENERATE_URL=$SQL_GENERATE_URL", + "deploy:server": "bun run --env-file=.env :deploy:server", + ":deploy:server": "fly deploy --build-arg SQL_GENERATE_URL=$SQL_GENERATE_URL", "docker:server": "docker build . --build-arg SQL_GENERATE_URL=$SQL_GENERATE_URL", "dev-db": "make dev-db", "test": "make test", From 67480f661673dbc05cbdaf241a48e95e1f3acb21 Mon Sep 17 00:00:00 2001 From: aster <137767097+aster-void@users.noreply.github.com> Date: Thu, 6 Mar 2025 17:30:44 +0900 Subject: [PATCH 04/10] setup deploy github action --- .github/workflows/deploy.yml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 .github/workflows/deploy.yml diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 00000000..7a6a9107 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,18 @@ +name: Deploy +on: + push: + branches: + - main +jobs: + deploy: + name: Deploy Server to Fly.io + runs-on: ubuntu-latest + env: + SQL_GENERATE_URL: ${{ secrets.DATABASE_URL_FOR_PRISMA_SQL_GENERATION }} + steps: + - uses: actions/checkout@v4 + - uses: superfly/flyctl-actions/setup-flyctl@master + - uses: oven-sh/setup-bun@v2 + with: + bun-version: 1.2.2 + - run: bun deploy:server --access-token ${{ secrets.FLY_DEPLOY_TOKEN }} From 155c023098eef0fb0284463b664039de2255b3ed Mon Sep 17 00:00:00 2001 From: aster <137767097+aster-void@users.noreply.github.com> Date: Thu, 6 Mar 2025 17:32:09 +0900 Subject: [PATCH 05/10] fix: delete deploy-server-test --- .github/workflows/ci.yml | 8 -------- 1 file changed, 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 999f0788..0eaf48df 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -97,11 +97,3 @@ jobs: - uses: oven-sh/setup-bun@v2 - run: bun prepare:deploy:web - run: test `ls web/.next | wc -l` != 0 - - deploy-test-server: - name: Deploy Test (server) - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: oven-sh/setup-bun@v2 - - run: bun prepare:deploy:server From cfa93b4aee4a25fff91ec0c1714f9a9af9ebf062 Mon Sep 17 00:00:00 2001 From: aster <137767097+aster-void@users.noreply.github.com> Date: Thu, 6 Mar 2025 17:33:58 +0900 Subject: [PATCH 06/10] test: test deploy --- .github/workflows/deploy.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 7a6a9107..5ea98223 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -3,6 +3,8 @@ on: push: branches: - main + - deploy + jobs: deploy: name: Deploy Server to Fly.io From 97df71f55063228b42bb6b5f110235f5530ca65d Mon Sep 17 00:00:00 2001 From: aster <137767097+aster-void@users.noreply.github.com> Date: Thu, 6 Mar 2025 17:38:36 +0900 Subject: [PATCH 07/10] fix(action): command fly not found --- .github/workflows/deploy.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 5ea98223..3d95fe62 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -11,10 +11,8 @@ jobs: runs-on: ubuntu-latest env: SQL_GENERATE_URL: ${{ secrets.DATABASE_URL_FOR_PRISMA_SQL_GENERATION }} + steps: - uses: actions/checkout@v4 - uses: superfly/flyctl-actions/setup-flyctl@master - - uses: oven-sh/setup-bun@v2 - with: - bun-version: 1.2.2 - - run: bun deploy:server --access-token ${{ secrets.FLY_DEPLOY_TOKEN }} + - run: flyctl deploy --build-arg SQL_GENERATE_URL=$SQL_GENERATE_URL --access-token "${{ secrets.FLY_DEPLOY_TOKEN }}" From 0316c842b2703bf46c1522c2281bbbbfbfec4b01 Mon Sep 17 00:00:00 2001 From: aster <137767097+aster-void@users.noreply.github.com> Date: Thu, 6 Mar 2025 17:42:17 +0900 Subject: [PATCH 08/10] fix: prisma uses DIRECT_URL --- .dockerignore | 1 + Dockerfile | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.dockerignore b/.dockerignore index 3c3629e6..6a0d87ef 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1 +1,2 @@ node_modules +.env* diff --git a/Dockerfile b/Dockerfile index 20e69ad8..80b12fec 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,8 +10,9 @@ FROM base AS build WORKDIR /build ARG SQL_GENERATE_URL +RUN test -n "${SQL_GENERATE_URL}" ENV DATABASE_URL=$SQL_GENERATE_URL -RUN test -n "${DATABASE_URL}" +ENV DIRECT_URL=$SQL_GENERATE_URL COPY . . RUN --mount=type=cache,target=~/.bun/install bun install --frozen-lockfile --ignore-scripts RUN cd server; bun prisma generate --sql From 3d21c3afede9c73df4890657d7dd6514ee30a317 Mon Sep 17 00:00:00 2001 From: aster <137767097+aster-void@users.noreply.github.com> Date: Thu, 6 Mar 2025 17:46:47 +0900 Subject: [PATCH 09/10] stop test --- .github/workflows/deploy.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 3d95fe62..1cd25f63 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -3,7 +3,6 @@ on: push: branches: - main - - deploy jobs: deploy: From 7341f73e065e57615b52fbc942a973e81e510666 Mon Sep 17 00:00:00 2001 From: aster <137767097+aster-void@users.noreply.github.com> Date: Thu, 6 Mar 2025 17:49:31 +0900 Subject: [PATCH 10/10] fix: delete serve --- server/.gitignore | 3 +-- server/package.json | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/server/.gitignore b/server/.gitignore index d264edb8..f7361e03 100644 --- a/server/.gitignore +++ b/server/.gitignore @@ -2,7 +2,6 @@ node_modules # WARNING: DELETE THIS IF YOU FIND THIS .env - .env.dev -# deprecated, delete /dist and if you have one + /target diff --git a/server/package.json b/server/package.json index bce5c52f..e23f0407 100644 --- a/server/package.json +++ b/server/package.json @@ -9,8 +9,7 @@ "check": "tsc", "clean": "rm target -r || true", "build": "bun clean && bun check && bun run :build", - ":build": "bun build ./src/index.ts --target bun --outfile target/index.js --minify", - "serve": "bun run ./a.js" + ":build": "bun build ./src/index.ts --target bun --outfile target/index.js --minify" }, "prisma": { "seed": "bun src/seeds/seed-test.ts"