Skip to content

Commit

Permalink
Enhance backend and CI configurations; introduce environment management
Browse files Browse the repository at this point in the history
- Updated .gitignore to include environment files and example.
- Added dotenv and zod dependencies for environment variable management and validation.
- Refactored backend server setup to utilize environment variables.
- Introduced a new environment configuration module for better management of environment variables.
- Updated TypeScript configuration for improved module resolution.
- Enhanced CI workflows for better validation, testing, and release processes.
- Added example .env file for backend configuration.
  • Loading branch information
valon-loshaj committed Jan 3, 2025
1 parent 09c4de1 commit d52d1d3
Show file tree
Hide file tree
Showing 16 changed files with 348 additions and 103 deletions.
160 changes: 118 additions & 42 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,53 +1,129 @@
name: CI

# Trigger on PRs and pushes to main
on:
pull_request:
branches: [main]
push:
branches: [main]
paths-ignore:
- "**.md"
- ".github/*.md"
- "docs/**"
- "LICENSE"

# Prevent multiple concurrent runs of the same workflow
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

# Define permissions needed for the workflow
permissions:
contents: read
pull-requests: write
checks: write
id-token: write # Needed for codecov

jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/prepare
- run: pnpm build
- run: NODE_ENV=production node apps/backend/dist/index.js
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/prepare
- run: pnpm lint
lint_knip:
name: Lint Knip
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/prepare
- run: pnpm lint:knip || true # Allow warnings for now
prettier:
name: Prettier
validate:
name: Validate
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [20]
fail-fast: false

steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/prepare
- run: pnpm format --list-different
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup environment
uses: ./.github/actions/prepare

- name: Cache dependencies
uses: actions/cache@v4
with:
path: |
**/node_modules
~/.pnpm-store
key: ${{ runner.os }}-pnpm-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-
- name: Type check
run: pnpm tsc

- name: Lint
run: pnpm lint

- name: Check formatting
run: pnpm format --check

- name: Check dependencies (Knip)
run: pnpm lint:knip || true # Allow warnings for now

test:
name: Test
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [20]
fail-fast: false

steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/prepare
- run: pnpm run test --coverage
- uses: codecov/codecov-action@v3
type_check:
name: Type Check
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup environment
uses: ./.github/actions/prepare

- name: Cache dependencies
uses: actions/cache@v4
with:
path: |
**/node_modules
~/.pnpm-store
key: ${{ runner.os }}-pnpm-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-
- name: Run tests
run: pnpm test --coverage

- name: Upload coverage
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: true

build:
name: Build
runs-on: ubuntu-latest
needs: [validate, test]
strategy:
matrix:
node-version: [20]
fail-fast: false

steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/prepare
- run: pnpm tsc
- name: Checkout repository
uses: actions/checkout@v4

name: CI
- name: Setup environment
uses: ./.github/actions/prepare

on:
pull_request: ~
push:
branches:
- main
- name: Cache dependencies
uses: actions/cache@v4
with:
path: |
**/node_modules
~/.pnpm-store
key: ${{ runner.os }}-pnpm-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-
- name: Build applications
run: pnpm build

- name: Test backend build
run: NODE_ENV=production node apps/backend/dist/index.js
46 changes: 36 additions & 10 deletions .github/workflows/post-release.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,53 @@
name: Post Release

on:
release:
types:
- published

permissions:
contents: read
issues: write
pull-requests: write

# Prevent multiple post-release processes from running simultaneously
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: false

jobs:
post_release:
name: Post Release Tasks
runs-on: ubuntu-latest
environment: production

steps:
- uses: actions/checkout@v4
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- run: echo "npm_version=$(npm pkg get version | tr -d '"')" >> "$GITHUB_ENV"
- uses: apexskier/github-release-commenter@v1

- name: Setup environment
uses: ./.github/actions/prepare

- name: Get npm version
id: package-version
run: echo "npm_version=$(node -p "require('./package.json').version")" >> "$GITHUB_ENV"

- name: Comment on related issues and PRs
uses: apexskier/github-release-commenter@v1
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
comment-template: |
:tada: This is included in version {release_link} :tada:
The release is available on:
* [GitHub releases](https://github.com/valon-loshaj/side-quest/releases/tag/{release_tag})
* [npm package (@latest dist-tag)](https://www.npmjs.com/package/side-quest/v/${{ env.npm_version }})
Cheers! 📦🚀
name: Post Release

on:
release:
types:
- published
- name: Update release documentation
if: success()
run: |
echo "Release ${{ env.npm_version }} has been successfully published"
60 changes: 44 additions & 16 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,56 @@
name: Release

on:
push:
branches:
- main

# Prevent multiple releases from running simultaneously
concurrency:
group: ${{ github.workflow }}
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: false

permissions:
contents: write
id-token: write
issues: write
pull-requests: write
packages: write

jobs:
release:
name: Create Release
runs-on: ubuntu-latest
environment: production

steps:
- uses: actions/checkout@v4
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: main
- uses: ./.github/actions/prepare
- run: pnpm build
- env:
GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
uses: JoshuaKGoldberg/[email protected]

name: Release
- name: Setup environment
uses: ./.github/actions/prepare

on:
push:
branches:
- main
- name: Cache dependencies
uses: actions/cache@v4
with:
path: |
**/node_modules
~/.pnpm-store
key: ${{ runner.os }}-pnpm-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-
permissions:
contents: write
id-token: write
- name: Build packages
run: pnpm build

- name: Create Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
uses: JoshuaKGoldberg/[email protected]
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
npm-token: ${{ secrets.NPM_TOKEN }}
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
coverage/
lib/
node_modules/

# Environment files
.env
.env.local
.env.*.local

# Keep example files
!.env.example
13 changes: 13 additions & 0 deletions apps/backend/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Server Configuration
PORT=3000
NODE_ENV=development

# Database Configuration
DATABASE_URL="postgresql://user:password@localhost:5432/your_database"

# JWT Configuration
JWT_SECRET=your_jwt_secret
JWT_EXPIRES_IN=24h

# API Configuration
API_PREFIX=/api/v1
9 changes: 6 additions & 3 deletions apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
"start": "node dist/server.js"
},
"dependencies": {
"express": "^4.18.2"
"dotenv": "^16.4.7",
"express": "^4.18.2",
"zod": "^3.24.1"
},
"devDependencies": {
"@types/express": "^4.17.21",
"@types/node": "^20.11.0",
"@types/node": "^20.17.6",
"ts-node-dev": "^2.0.0",
"typescript": "^5.3.3"
}
},
"type": "module"
}
43 changes: 43 additions & 0 deletions apps/backend/src/config/environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { z } from "zod";
import dotenv from "dotenv";
import path from "path";
import { fileURLToPath } from "url";
import { dirname } from "path";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

// Load environment variables based on NODE_ENV
const loadEnvFile = () => {
const environment = process.env.NODE_ENV || "development";
const envPath = path.resolve(__dirname, `../../.env.${environment}`);
const defaultEnvPath = path.resolve(__dirname, "../../.env");

// First try to load environment-specific file, then fall back to default .env
dotenv.config({ path: envPath });
dotenv.config({ path: defaultEnvPath });
};

// Load environment variables before validation
loadEnvFile();

const envSchema = z.object({
PORT: z.string().transform(Number),
NODE_ENV: z.enum(["development", "production", "test"]),
DATABASE_URL: z.string().url(),
JWT_SECRET: z.string().min(1),
JWT_EXPIRES_IN: z.string().min(1),
API_PREFIX: z.string().startsWith("/"),
});

export const validateEnv = () => {
try {
const env = envSchema.parse(process.env);
return env;
} catch (error) {
console.error("❌ Invalid environment variables:", error);
process.exit(1);
}
};

export const env = validateEnv();
Loading

0 comments on commit d52d1d3

Please sign in to comment.