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 env files #368

Merged
merged 5 commits into from
Jan 7, 2025
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
26 changes: 17 additions & 9 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
NODE_ENV=development

# Next.js
NEXT_PUBLIC_SITE_URL=http://localhost:3000

POSTGRES_USER=dev
POSTGRES_PASSWORD=1234
POSTGRES_DB=dev
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_SCHEMA=public
POSTGRES_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}?schema=${POSTGRES_SCHEMA}
# Database
DATABASE_USER=dev
DATABASE_PASSWORD=1234
DATABASE_DB=dev
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_SCHEMA=public
# for prisma migration
DATABASE_URL=postgresql://${DATABASE_USER}:${DATABASE_PASSWORD}@${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_DB}?schema=${DATABASE_SCHEMA}

# Google OAuth
# https://console.cloud.google.com/apis/credentials
# Set values below
# AUTHORIZED JAVASCRIPT ORIGINS: http://localhost:3000
# AUTHORIZED REDIRECT URIS: http://localhost:3000/api/auth/callback/google
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
GOOGLE_CLIENT_ID=xxxx
GOOGLE_CLIENT_SECRET=xxxx

# NextAuth.js
NEXTAUTH_URL=${NEXT_PUBLIC_SITE_URL}
# https://next-auth.js.org/configuration/options#secret
# you must generate a new secret
# error: "ikm" must be at least one byte in length'
# $ openssl rand -base64 32
NEXTAUTH_SECRET=TKDdLVjf7cTyTs5oWVpv04senu6fia4RGQbYHRQIR5Q=

# OpenTelemetry
TRACE_EXPORTER_URL=
25 changes: 16 additions & 9 deletions .env.test
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
NODE_ENV=test

# Next.js
NEXT_PUBLIC_SITE_URL=http://localhost:3000

POSTGRES_USER=test
POSTGRES_PASSWORD=1234
POSTGRES_DB=test
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_SCHEMA=public
POSTGRES_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}?schema=${POSTGRES_SCHEMA}
# Database
DATABASE_USER=test
DATABASE_PASSWORD=1234
DATABASE_DB=test
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_SCHEMA=public
# for prisma migration
DATABASE_URL=postgresql://${DATABASE_USER}:${DATABASE_PASSWORD}@${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_DB}?schema=${DATABASE_SCHEMA}

# Google OAuth
# https://console.cloud.google.com/apis/credentials
# Set values below
# AUTHORIZED JAVASCRIPT ORIGINS: http://localhost:3000
# AUTHORIZED REDIRECT URIS: http://localhost:3000/api/auth/callback/google
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
GOOGLE_CLIENT_ID=dummy
GOOGLE_CLIENT_SECRET=dummy

# NextAuth.js
NEXTAUTH_URL=${NEXT_PUBLIC_SITE_URL}
# https://next-auth.js.org/configuration/options#secret
# you must generate a new secret
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
runs-on: ubuntu-latest
env:
# should store them to github.secrets
POSTGRES_URL: postgresql://dev:1234@172.17.0.1:5432/dev?schema=public
DATABASE_URL: postgresql://dev:1234@172.17.0.1:5432/dev?schema=public
NEXTAUTH_SECRET: UfxvOS6HetHOFkL44YTITYgc0DOuOlz5TBp3jkbnZ3w=
NEXT_PUBLIC_SITE_URL: http://localhost:3000
steps:
Expand All @@ -51,7 +51,7 @@ jobs:
docker build \
-t app \
-f Dockerfile \
--build-arg POSTGRES_URL=${{env.POSTGRES_URL}} \
--build-arg DATABASE_URL=${{env.DATABASE_URL}} \
--build-arg NEXTAUTH_SECRET=${{env.NEXTAUTH_SECRET}} \
--build-arg NEXT_PUBLIC_SITE_URL=${{env.NEXT_PUBLIC_SITE_URL}} \
.
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/migration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ jobs:
- uses: ./.github/actions/setup-db
- run: pnpm db:deploy
env:
DATABASE_URL: ${{secrets.POSTGRES_URL}}
DATABASE_URL: ${{secrets.DATABASE_URL}}
17 changes: 11 additions & 6 deletions .internal/tests/common.test.mjs.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ exports[`common > should put files 1`] = `
"internal-tests-output-common/README.md",
"internal-tests-output-common/biome.json",
"internal-tests-output-common/compose.yml",
"internal-tests-output-common/env.ts",
"internal-tests-output-common/lefthook.yml",
"internal-tests-output-common/next.config.ts",
"internal-tests-output-common/otel-collector-config.yml",
Expand Down Expand Up @@ -81,7 +82,7 @@ exports[`common > should update .github/workflows/ci.yml 1`] = `
" runs-on: ubuntu-latest",
" env:",
" # should store them to github.secrets",
" POSTGRES_URL: postgresql://dev:1234@172.17.0.1:5432/dev?schema=public",
" DATABASE_URL: postgresql://dev:1234@172.17.0.1:5432/dev?schema=public",
" NEXTAUTH_SECRET: UfxvOS6HetHOFkL44YTITYgc0DOuOlz5TBp3jkbnZ3w=",
" NEXT_PUBLIC_SITE_URL: http://localhost:3000",
" steps:",
Expand All @@ -92,7 +93,7 @@ exports[`common > should update .github/workflows/ci.yml 1`] = `
" docker build \\\\",
" -t app \\\\",
" -f Dockerfile \\\\",
" --build-arg POSTGRES_URL=\${{env.POSTGRES_URL}} \\\\",
" --build-arg DATABASE_URL=\${{env.DATABASE_URL}} \\\\",
" --build-arg NEXTAUTH_SECRET=\${{env.NEXTAUTH_SECRET}} \\\\",
" --build-arg NEXT_PUBLIC_SITE_URL=\${{env.NEXT_PUBLIC_SITE_URL}} \\\\",
" .",
Expand Down Expand Up @@ -230,6 +231,7 @@ exports[`common > should update README.md 1`] = `
"## Links",
"",
"- [Database ER diagram](/prisma/ERD.md)",
"- [Web App Template](https://hiroppy.github.io/web-app-template/)",
""
]
`;
Expand All @@ -243,11 +245,11 @@ exports[`common > should update compose.yml 1`] = `
" db:",
" image: postgres:17",
" ports:",
" - \${POSTGRES_PORT:-5432}:5432",
" - \${DATABASE_PORT:-5432}:5432",
" environment:",
" - POSTGRES_USER=\${POSTGRES_USER}",
" - POSTGRES_PASSWORD=\${POSTGRES_PASSWORD}",
" - POSTGRES_DB=\${POSTGRES_DB}",
" - POSTGRES_USER=\${DATABASE_USER}",
" - POSTGRES_PASSWORD=\${DATABASE_PASSWORD}",
" - POSTGRES_DB=\${DATABASE_DB}",
" jaeger:",
" image: jaegertracing/all-in-one",
" ports:",
Expand Down Expand Up @@ -328,6 +330,9 @@ exports[`common > should update dependencies 1`] = `
exports[`common > should update next.config.ts 1`] = `
[
"import type { NextConfig } from \\"next\\";",
"import { config } from \\"./env\\";",
"",
"config();",
"",
"const nextConfig: NextConfig = {",
" images: {",
Expand Down
1 change: 1 addition & 0 deletions .internal/tests/no-docker.test.mjs.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ exports[`no-docker > should put files 1`] = `
"internal-tests-output-no-docker/README.md",
"internal-tests-output-no-docker/biome.json",
"internal-tests-output-no-docker/compose.yml",
"internal-tests-output-no-docker/env.ts",
"internal-tests-output-no-docker/lefthook.yml",
"internal-tests-output-no-docker/next.config.ts",
"internal-tests-output-no-docker/otel-collector-config.yml",
Expand Down
6 changes: 4 additions & 2 deletions .internal/tests/no-e2e.test.mjs.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ exports[`no-e2e > should put files 1`] = `
"internal-tests-output-no-e2e/README.md",
"internal-tests-output-no-e2e/biome.json",
"internal-tests-output-no-e2e/compose.yml",
"internal-tests-output-no-e2e/env.ts",
"internal-tests-output-no-e2e/lefthook.yml",
"internal-tests-output-no-e2e/next.config.ts",
"internal-tests-output-no-e2e/otel-collector-config.yml",
Expand Down Expand Up @@ -80,7 +81,7 @@ exports[`no-e2e > should update .github/workflows/ci.yml 1`] = `
" runs-on: ubuntu-latest",
" env:",
" # should store them to github.secrets",
" POSTGRES_URL: postgresql://dev:1234@172.17.0.1:5432/dev?schema=public",
" DATABASE_URL: postgresql://dev:1234@172.17.0.1:5432/dev?schema=public",
" NEXTAUTH_SECRET: UfxvOS6HetHOFkL44YTITYgc0DOuOlz5TBp3jkbnZ3w=",
" NEXT_PUBLIC_SITE_URL: http://localhost:3000",
" steps:",
Expand All @@ -91,7 +92,7 @@ exports[`no-e2e > should update .github/workflows/ci.yml 1`] = `
" docker build \\\\",
" -t app \\\\",
" -f Dockerfile \\\\",
" --build-arg POSTGRES_URL=\${{env.POSTGRES_URL}} \\\\",
" --build-arg DATABASE_URL=\${{env.DATABASE_URL}} \\\\",
" --build-arg NEXTAUTH_SECRET=\${{env.NEXTAUTH_SECRET}} \\\\",
" --build-arg NEXT_PUBLIC_SITE_URL=\${{env.NEXT_PUBLIC_SITE_URL}} \\\\",
" .",
Expand Down Expand Up @@ -198,6 +199,7 @@ exports[`no-e2e > should update README.md 1`] = `
"## Links",
"",
"- [Database ER diagram](/prisma/ERD.md)",
"- [Web App Template](https://hiroppy.github.io/web-app-template/)",
""
]
`;
Expand Down
13 changes: 9 additions & 4 deletions .internal/tests/no-otel.test.mjs.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ exports[`no-otel > should put files 1`] = `
"internal-tests-output-no-otel/README.md",
"internal-tests-output-no-otel/biome.json",
"internal-tests-output-no-otel/compose.yml",
"internal-tests-output-no-otel/env.ts",
"internal-tests-output-no-otel/lefthook.yml",
"internal-tests-output-no-otel/next.config.ts",
"internal-tests-output-no-otel/package.json",
Expand Down Expand Up @@ -102,6 +103,7 @@ exports[`no-otel > should update README.md 1`] = `
"## Links",
"",
"- [Database ER diagram](/prisma/ERD.md)",
"- [Web App Template](https://hiroppy.github.io/web-app-template/)",
""
]
`;
Expand All @@ -115,11 +117,11 @@ exports[`no-otel > should update compose.yml 1`] = `
" db:",
" image: postgres:17",
" ports:",
" - \${POSTGRES_PORT:-5432}:5432",
" - \${DATABASE_PORT:-5432}:5432",
" environment:",
" - POSTGRES_USER=\${POSTGRES_USER}",
" - POSTGRES_PASSWORD=\${POSTGRES_PASSWORD}",
" - POSTGRES_DB=\${POSTGRES_DB}",
" - POSTGRES_USER=\${DATABASE_USER}",
" - POSTGRES_PASSWORD=\${DATABASE_PASSWORD}",
" - POSTGRES_DB=\${DATABASE_DB}",
""
]
`;
Expand Down Expand Up @@ -166,6 +168,9 @@ exports[`no-otel > should update dependencies 1`] = `
exports[`no-otel > should update next.config.ts 1`] = `
[
"import type { NextConfig } from \\"next\\";",
"import { config } from \\"./env\\";",
"",
"config();",
"",
"const nextConfig: NextConfig = {",
" images: {",
Expand Down
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"prettier.requireConfig": false,
"cSpell.enabled": true,
"cSpell.words": ["biomejs", "otel", "otlp"],
"cSpell.words": ["dotenv", "biomejs", "otel", "otlp"],
"cSpell.allowCompoundWords": true,
"cSpell.ignorePaths": ["**/node_modules/**", "**/vscode-extension/**"]
}
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ FROM node:22.12.0-slim AS base

WORKDIR /app

ARG POSTGRES_URL=''
ARG DATABASE_URL=''
ARG NEXTAUTH_SECRET=''
ARG NEXT_PUBLIC_SITE_URL=''
ARG TRACE_EXPORTER_URL=''

ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
ENV POSTGRES_URL=$POSTGRES_URL
ENV DATABASE_URL=$DATABASE_URL
ENV NEXTAUTH_SECRET=$NEXTAUTH_SECRET
ENV NEXTAUTH_URL=$NEXT_PUBLIC_SITE_URL
ENV NEXT_PUBLIC_SITE_URL=$NEXT_PUBLIC_SITE_URL
Expand Down
8 changes: 4 additions & 4 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ services:
db:
image: postgres:17
ports:
- ${POSTGRES_PORT:-5432}:5432
- ${DATABASE_PORT:-5432}:5432
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
- POSTGRES_USER=${DATABASE_USER}
- POSTGRES_PASSWORD=${DATABASE_PASSWORD}
- POSTGRES_DB=${DATABASE_DB}
####### otel #######
jaeger:
image: jaegertracing/all-in-one
Expand Down
2 changes: 1 addition & 1 deletion e2e/globalSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { setupDB } from "../tests/db.setup";

export default async function globalSetup(config: FullConfig) {
const { down } = await setupDB({
port: Number(process.env.POSTGRES_PORT),
port: Number(process.env.DATABASE_PORT),
});

global.down = down;
Expand Down
48 changes: 48 additions & 0 deletions env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import dotenv from "dotenv";
import { z } from "zod";

export type Schema = z.infer<typeof schema>;

export const schema = z.object({
NODE_ENV: z
.union([
z.literal("development"),
z.literal("test"),
z.literal("production"),
])
.default("development"),

// for client and server
NEXT_PUBLIC_SITE_URL: z.string().url(),

// for server
DATABASE_USER: z.string().min(1),
DATABASE_PASSWORD: z.string().min(1),
DATABASE_DB: z.string().min(1),
DATABASE_HOST: z.string().min(1),
DATABASE_PORT: z.coerce.number().min(1),
DATABASE_SCHEMA: z.string().min(1),
DATABASE_URL: z.string().min(1),

GOOGLE_CLIENT_ID: z.string().min(1),
GOOGLE_CLIENT_SECRET: z.string().min(1),

NEXTAUTH_URL: z.string().min(1),
NEXTAUTH_SECRET: z.string().min(1),

TRACE_EXPORTER_URL: z.string().url().optional().or(z.literal("")),
});

export function config(file?: ".env" | ".env.test") {
if (file) {
dotenv.config({ path: file });
}

const res = schema.safeParse(process.env);

if (res.error) {
console.error("\x1b[31m%s\x1b[0m", "[Errors] environment variables");
console.error(JSON.stringify(res.error.errors, null, 2));
process.exit(1);
}
}
3 changes: 3 additions & 0 deletions next.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { NextConfig } from "next";
import { config } from "./env";

config();

const nextConfig: NextConfig = {
images: {
Expand Down
4 changes: 2 additions & 2 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineConfig, devices } from "@playwright/test";
import { config } from "dotenv";
import { config } from "./env";

config({ path: ".env.test" });
config(".env.test");

export default defineConfig({
testDir: "./e2e",
Expand Down
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

datasource db {
provider = "postgresql"
url = env("POSTGRES_URL")
url = env("DATABASE_URL")
}

generator client {
Expand Down
12 changes: 6 additions & 6 deletions src/app/_utils/db.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export function createDBUrl({
user = process.env.POSTGRES_USER,
password = process.env.POSTGRES_PASSWORD,
host = process.env.POSTGRES_HOST,
port = Number(process.env.POSTGRES_PORT),
db = process.env.POSTGRES_DB,
schema = process.env.POSTGRES_SCHEMA,
user = process.env.DATABASE_USER,
password = process.env.DATABASE_PASSWORD,
host = process.env.DATABASE_HOST,
port = Number(process.env.DATABASE_PORT),
db = process.env.DATABASE_DB,
schema = process.env.DATABASE_SCHEMA,
}: {
user?: string;
password?: string;
Expand Down
Loading
Loading