Skip to content

Commit 828ed18

Browse files
authored
feat: simplify env var usage (#26)
* chore: update development script to set NODE_ENV for improved environment handling * feat: add development environment support for configuration management * feat: add environment configuration files for development and production * chore: simplify development script by removing NODE_ENV setting * feat: enhance environment loading by adding dynamic file selection and logging * feat: add NODE_ENV settings and environment config files for production and development * refactor: remove unused development environment function and related configuration * chore: update docker-compose configuration * chore: update production environment variables for development server * chore: update .gitignore to include .env.development.example and adjust environment variable exclusions * chore: remove .env.development file * chore: add example development environment configuration file * chore: standardize environment configuration by replacing .env.local with .env * chore: consolidate environment configuration by removing .env.development.example and updating .env.example * chore: clean up environment configuration by removing commented-out sections
1 parent 1b41653 commit 828ed18

File tree

11 files changed

+92
-42
lines changed

11 files changed

+92
-42
lines changed

.env.example

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
1-
DISCORD_TOKEN="" # Your bot token
2-
CLIENT_ID="" # Your bot's application ID
1+
# Local Environment Variables (Secrets)
2+
# Copy this file to .env and fill in your actual values
3+
# .env is gitignored and should NEVER be committed
34

4-
SERVER_ID= # Discord Server ID where the bot will operate
5-
MODERATORS_ROLE_IDS= # Comma separated list of role IDs that are Moderators(Mods, Admins, etc)
5+
# Discord Bot Token & Application ID (REQUIRED)
6+
# Get this from: https://discord.com/developers/applications
7+
DISCORD_TOKEN=your-bot-token-here
8+
CLIENT_ID=your-bot-application-id
69

7-
REPEL_LOG_CHANNEL_ID= # Channel ID where the bot will log repel actions
8-
REPEL_ROLE_ID= # Role ID assigned to users who can use the repel command
9-
GUIDES_CHANNEL_ID="" # The ID of the channel where guides will be posted
10+
# Override any public config values for local testing
11+
12+
# Discord Server ID (your dev server)
13+
SERVER_ID=your-server-id
14+
15+
# Channel IDs (from your dev server)
16+
GUIDES_CHANNEL_ID=your-guide-channel-id
17+
REPEL_LOG_CHANNEL_ID=your-repel-log-channel-id
18+
19+
# Role IDs (from your dev server)
20+
REPEL_ROLE_ID=your-repel-role-id
21+
MODERATORS_ROLE_IDS=your-moderator-role-id
22+
23+
# Other
24+
GUIDES_TRACKER_PATH=guides-tracker.json

.env.production

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Production Environment Configuration
2+
# Public values - safe to commit to repository
3+
# These are Discord IDs that are publicly visible anyway
4+
5+
# Discord Server ID (your dev server)
6+
SERVER_ID=434487340535382016
7+
8+
# Channel IDs (from your dev server)
9+
GUIDES_CHANNEL_ID=1429492053825290371
10+
REPEL_LOG_CHANNEL_ID=1403558160144531589
11+
12+
# Role IDs (from your dev server)
13+
REPEL_ROLE_ID=1002411741776461844
14+
MODERATORS_ROLE_IDS=849481536654803004
15+
16+
# Other
17+
GUIDES_TRACKER_PATH=/app/data/guides-tracker.json
18+
19+
# Note: DISCORD_TOKEN & CLIENT_ID should be in .env.local (not committed)

.github/workflows/deploy.yml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,19 @@ jobs:
3838
export NODE_VERSION=$(cat .nvmrc | sed 's/v//')
3939
echo "Using Node version: $NODE_VERSION"
4040
41-
# Create .env.local file with secrets
42-
cat > .env.local << EOF
41+
# Create .env file with secrets
42+
# Public config comes from .env.production (committed to repo)
43+
# NODE_ENV=production is set in docker-compose.yml
44+
cat > .env << EOF
4345
DISCORD_TOKEN=${{ secrets.DISCORD_TOKEN }}
4446
CLIENT_ID=${{ secrets.CLIENT_ID }}
45-
GUIDES_CHANNEL_ID=${{ secrets.GUIDES_CHANNEL_ID }}
46-
SERVER_ID=${{ secrets.SERVER_ID }}
47-
REPEL_LOG_CHANNEL_ID=${{ secrets.REPEL_LOG_CHANNEL_ID }}
48-
REPEL_ROLE_ID=${{ secrets.REPEL_ROLE_ID }}
49-
MODERATORS_ROLE_IDS=${{ secrets.MODERATORS_ROLE_IDS }}
5047
EOF
5148
5249
# Stop any existing containers
5350
docker compose down || true
5451
5552
# Build and start production container with profile
53+
# NODE_ENV=production is explicitly set in docker-compose.yml bot-prod service
5654
docker compose --profile prod up -d --build
5755
5856
# Check status

.gitignore

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@ yarn-debug.log*
1212
yarn-error.log*
1313

1414
# Env files
15-
!.env.example
1615
.env
17-
.env.local
18-
.env.*.local
16+
.env.*
17+
18+
# Public config (committed to repo)
19+
!.env.production
20+
!.env.example
1921

2022
# guides tracker
21-
guides-tracker.json
23+
guides-tracker.json
24+
25+
# Docker
26+
docker-compose.yml

DOCKER.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ This document explains how to run the webdev-bot using Docker.
66

77
- Docker installed (version 20.10 or higher)
88
- Docker Compose installed (version 2.0 or higher)
9-
- `.env.local` file with required environment variables
9+
- `.env` file with required environment variables
1010

1111
## Node Version Management
1212

1313
The Docker setup uses `.nvmrc` as the single source of truth for the Node.js version. The Dockerfile automatically reads this version at build time via the `NODE_VERSION` build argument. No need to manually sync versions between `.nvmrc` and Docker files.
1414

1515
## Environment Variables
1616

17-
Before running the bot, create a `.env.local` file in the project root with the following variables:
17+
Before running the bot, create a `.env` file in the project root with the following variables:
1818

1919
```env
2020
DISCORD_TOKEN=your_discord_bot_token
@@ -104,7 +104,7 @@ Run the production container manually (after building with the NODE_VERSION arg)
104104
```bash
105105
docker run -d \
106106
--name webdev-bot \
107-
--env-file .env.local \
107+
--env-file .env \
108108
--restart unless-stopped \
109109
webdev-bot:latest
110110
```
@@ -114,7 +114,7 @@ Run the development container manually (after building with the NODE_VERSION arg
114114
```bash
115115
docker run -it \
116116
--name webdev-bot-dev \
117-
--env-file .env.local \
117+
--env-file .env \
118118
-v $(pwd)/src:/app/src:ro \
119119
webdev-bot:dev
120120
```
@@ -183,7 +183,7 @@ docker compose build --no-cache
183183

184184
## Best Practices
185185

186-
1. **Never commit `.env.local`** - Keep your secrets secure
186+
1. **Never commit `.env`** - Keep your secrets secure
187187
2. **Use production profile for deployment** - Smaller, more secure images
188188
3. **Keep development profile for local testing** - Faster iteration with hot reload
189189
4. **Node version is managed in `.nvmrc`** - Update `.nvmrc` to change Node version for Docker

Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ COPY --from=deps /app/node_modules ./node_modules
4141
COPY --from=build /app/dist ./dist
4242
COPY package.json ./
4343

44+
# Copy environment config file (public, non-secret)
45+
COPY .env.production ./
46+
4447
# Create data directory and set permissions for node user
4548
RUN mkdir -p /app/data && chown -R node:node /app/data
4649

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ A comprehensive Discord bot designed specifically for the Web Dev Discord server
5252
pnpm install
5353
```
5454

55-
3. Create a `.env.local` file based on `.env.example` and fill in the required environment variables:
55+
3. Create a `.env` file based on `.env.example` and fill in the required environment variables:
5656
```bash
57-
cp .env.example .env.local
58-
# Edit .env.local to add your Discord bot token and other configurations
57+
cp .env.example .env
58+
# Edit .env to add your Discord bot token and other configurations
5959
```
6060

6161
4. Build and start the bot:

docker-compose.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ services:
99
container_name: webdev-bot-prod
1010
restart: unless-stopped
1111
env_file:
12-
- .env.local
12+
- .env
1313
environment:
14-
- GUIDES_TRACKER_PATH=/app/data/guides-tracker.json
14+
- NODE_ENV=production
1515
volumes:
16+
# Mount environment config file
17+
- ./.env.production:/app/.env.production:ro
1618
# Persist guides tracker data
1719
- guides-data:/app/data
1820
profiles:
@@ -28,9 +30,9 @@ services:
2830
container_name: webdev-bot-dev
2931
restart: unless-stopped
3032
env_file:
31-
- .env.local
33+
- .env
3234
environment:
33-
- GUIDES_TRACKER_PATH=/app/data/guides-tracker.json
35+
- NODE_ENV=development
3436
volumes:
3537
# Mount source code for hot reload
3638
- ./src:/app/src:ro
@@ -40,6 +42,8 @@ services:
4042
# Mount package files (in case dependencies change)
4143
- ./package.json:/app/package.json:ro
4244
- ./pnpm-lock.yaml:/app/pnpm-lock.yaml:ro
45+
# Mount environment config files
46+
- ./.env.production:/app/.env.production:ro
4347
# Persist guides tracker data
4448
- guides-data:/app/data
4549
profiles:

docs/GUIDE_SYNC.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ The bot automatically synchronizes guide markdown files from `src/commands/guide
44

55
## Setup
66

7-
Add to your `.env.local` file:
7+
Add to your `.env` file:
88
```
99
GUIDES_CHANNEL_ID=1234567890123456789
1010
```

src/env.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function requireEnv(key: string): string {
88
const value = process.env[key];
99
if (!value) {
1010
console.error(`❌ Required environment variable ${key} is not set`);
11-
console.error('Please check your .env.local file or CI/CD configuration');
11+
console.error('Please check your .env file or CI/CD configuration');
1212
process.exit(1);
1313
}
1414
return value;
@@ -33,13 +33,6 @@ export const config = {
3333
channelId: requireEnv('GUIDES_CHANNEL_ID'),
3434
trackerPath: optionalEnv('GUIDES_TRACKER_PATH'),
3535
},
36-
// Add more config sections as needed:
37-
// database: {
38-
// url: requireEnv('DATABASE_URL'),
39-
// },
40-
// api: {
41-
// openaiKey: optionalEnv('OPENAI_API_KEY'),
42-
// },
4336
};
4437

4538
export type Config = typeof config;

0 commit comments

Comments
 (0)