Thanks to visit codestin.com
Credit goes to deepwiki.com

Menu

Environment Variables and Configuration

Relevant source files

This document describes how Trigger.dev manages environment variables and configuration across the web application, workers, and deployment pipeline. It covers the centralized configuration system, validation approach, and the various categories of settings that control system behavior.

For information about local development setup, see Local Development. For deployment process details, see Deployment Process.


Configuration Architecture Overview

Trigger.dev uses a centralized, type-safe configuration system built on Zod schemas. All environment variables are validated at startup and exposed through a single env export rather than accessing process.env directly.

Architecture: Configuration System

Sources: apps/webapp/app/env.server.ts1-700 apps/webapp/app/utils/boolEnv.ts


Environment Variable Declaration and Validation

Zod Schema Definition

All environment variables are declared in a single Zod schema called EnvironmentSchema. This provides:

  1. Type safety: TypeScript types are automatically inferred from the schema
  2. Runtime validation: Invalid values cause startup failures with clear error messages
  3. Default values: Sensible defaults reduce configuration burden
  4. Transformations: Values can be parsed, coerced, and transformed

Sources: apps/webapp/app/env.server.ts28-700

Custom Validators

The system includes custom validation functions for complex requirements:

ValidatorPurposeExample Usage
isValidDatabaseUrlValidates PostgreSQL connection stringsDATABASE_URL, DIRECT_URL
isValidRegexValidates regex patterns for email filteringWHITELISTED_EMAILS, ADMIN_EMAILS
BoolEnvParses boolean environment variablesLOGIN_RATE_LIMITS_ENABLED

Sources: apps/webapp/app/env.server.ts3-4 apps/webapp/app/utils/boolEnv.ts

Discriminated Unions

Complex features use discriminated unions to ensure related variables are set together:

This ensures all GitHub app credentials are provided when the feature is enabled, or none are required when disabled.

Sources: apps/webapp/app/env.server.ts6-26


Core Configuration Categories

Database Configuration

PostgreSQL connection settings with support for read replicas and connection pooling:

VariableTypeDefaultDescription
DATABASE_URLstringrequiredPrimary database connection (pooled)
DIRECT_URLstringrequiredDirect connection for migrations
DATABASE_READ_REPLICA_URLstringoptionalRead replica for query offloading
DATABASE_CONNECTION_LIMITnumber10Maximum connections per pool
DATABASE_POOL_TIMEOUTnumber60Pool acquisition timeout (seconds)
DATABASE_CONNECTION_TIMEOUTnumber20Connection timeout (seconds)

Database Configuration Pattern:

Sources: apps/webapp/app/env.server.ts31-46 apps/webapp/app/db.server.ts

Redis Configuration

The system uses multiple Redis instances for different purposes, each with independent configuration:

Redis Instance Architecture:

Each Redis instance supports:

  • Primary host/port configuration
  • Optional read replica (reader host/port)
  • Username/password authentication
  • TLS enable/disable
  • Cluster mode enable/disable

Configuration Pattern with Fallbacks:

This pattern allows specialized Redis instances to fall back to the base Redis configuration if not explicitly set.

Sources: apps/webapp/app/env.server.ts95-237 apps/webapp/app/env.server.ts575-643

Authentication and Security

VariablePurpose
SESSION_SECRETSigns session cookies
MAGIC_LINK_SECRETSigns magic link tokens for passwordless auth
ENCRYPTION_KEYMust be exactly 32 bytes, used for sensitive data encryption
AUTH_GITHUB_CLIENT_IDGitHub OAuth app credentials
AUTH_GITHUB_CLIENT_SECRETGitHub OAuth app credentials

Sources: apps/webapp/app/env.server.ts47-75

Email and Notifications

The system supports multiple email transports:

TransportRequired Variables
resendRESEND_API_KEY
smtpSMTP_HOST, SMTP_PORT, SMTP_USER, SMTP_PASSWORD
aws-sesAWS credentials

Alerts use separate configuration (ALERT_EMAIL_TRANSPORT, ALERT_FROM_EMAIL, etc.) allowing different providers for system emails vs. user alerts.

Sources: apps/webapp/app/env.server.ts76-84 apps/webapp/app/env.server.ts398-442

Deployment Configuration

Deployment Registry Settings:

VariableDescription
DEPLOY_REGISTRY_HOSTContainer registry hostname (required)
DEPLOY_REGISTRY_USERNAMERegistry authentication (optional)
DEPLOY_REGISTRY_PASSWORDRegistry authentication (optional)
DEPLOY_REGISTRY_NAMESPACERegistry namespace (default: "trigger")
DEPLOY_REGISTRY_ECR_TAGSCSV of key=value tags for ECR
DEPLOY_REGISTRY_ECR_ASSUME_ROLE_ARNCross-account ECR access

The system supports separate v3 and v4 registry configuration, with v4 falling back to v3 values:

Build Settings:

VariableDefaultDescription
DEPOT_TOKEN-Depot.ai API token for remote builds
DEPOT_ORG_ID-Depot organization ID
DEPOT_REGION"us-east-1"Depot build region
DEPLOY_IMAGE_PLATFORM"linux/amd64"Target platform for images
DEPLOY_TIMEOUT_MS4800008 minutes - build timeout
DEPLOY_QUEUE_TIMEOUT_MS90000015 minutes - queue timeout

Sources: apps/webapp/app/env.server.ts264-319

Observability Configuration

OpenTelemetry Exporters:

Key OTEL Settings:

CategoryVariablesPurpose
Dev OTELDEV_OTEL_EXPORTER_OTLP_ENDPOINTDevelopment worker telemetry endpoint
Batch ProcessingDEV_OTEL_BATCH_PROCESSING_ENABLEDEnable batch span/log export
Span LimitsTRIGGER_OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT (default: 1024)Maximum attributes per span
Log LimitsTRIGGER_OTEL_LOG_ATTRIBUTE_COUNT_LIMIT (default: 1024)Maximum attributes per log
Internal TracingINTERNAL_OTEL_TRACE_SAMPLING_RATE (default: "20")1 in 20 traces sampled

Sources: apps/webapp/app/env.server.ts343-393

Run Engine 2.0 Configuration

The Run Engine has extensive tuning parameters:

Core Run Engine Settings:

VariableDefaultDescription
RUN_ENGINE_WORKER_COUNT4Number of worker threads
RUN_ENGINE_TASKS_PER_WORKER10Concurrent tasks per worker
RUN_ENGINE_WORKER_CONCURRENCY_LIMIT10Max concurrent operations
RUN_ENGINE_WORKER_POLL_INTERVAL100Polling interval (ms)
RUN_ENGINE_TIMEOUT_EXECUTING3000005 minutes - execution timeout
RUN_ENGINE_PARENT_QUEUE_LIMIT1000Parent queue max size

Run Locking Configuration:

VariableDefaultDescription
RUN_ENGINE_RUN_LOCK_DURATION5000Lock duration (ms)
RUN_ENGINE_RUN_LOCK_MAX_RETRIES10Maximum lock acquisition attempts
RUN_ENGINE_RUN_LOCK_BASE_DELAY100Initial retry delay (ms)
RUN_ENGINE_RUN_LOCK_MAX_DELAY3000Maximum retry delay (ms)
RUN_ENGINE_RUN_LOCK_BACKOFF_MULTIPLIER1.8Exponential backoff factor

Sources: apps/webapp/app/env.server.ts520-643


Build-time vs Runtime Configuration

Configuration Stages:

Build-time Variables

These are embedded in the Docker image during build:

Sources: docker/Dockerfile96-103

Turbo Global Environment

Turbo tracks specific environment variables to invalidate build caches when they change:

Sources: turbo.json124-146

Runtime Validation

At server startup, env.server.ts validates all environment variables:

Invalid configurations cause the application to fail with descriptive error messages before accepting any requests.

Sources: apps/webapp/app/env.server.ts28-700


Configuration Patterns

Fallback Pattern

Many configuration options fall back to base values:

This reduces duplication when using a single Redis instance for multiple purposes.

Sources: apps/webapp/app/env.server.ts136-166

Coercion and Transformation

Numeric and boolean values are coerced from strings:

Sources: apps/webapp/app/env.server.ts37 apps/webapp/app/env.server.ts62

Conditional Defaults

Some defaults depend on other environment variables:

Sources: apps/webapp/app/env.server.ts133

Validation Refinements

Custom validation logic ensures correctness:

Sources: apps/webapp/app/env.server.ts49-54


Best Practices

Never Access process.env Directly

All application code imports env from env.server.ts:

Sources: .cursor/rules/webapp.mdc14-22

Service/Configuration Separation

Testable services receive configuration as constructor parameters rather than importing env directly:

This pattern allows tests to instantiate services with mock configuration without importing env.server.ts.

Sources: .cursor/rules/webapp.mdc18-22

Secrets Management

Sensitive values should never be committed to version control:

  1. Use .env files for local development (ignored by git)
  2. Use Docker secrets for container deployments
  3. Use environment variables in CI/CD pipelines

Sources: docker/Dockerfile72-73


Docker and Production Setup

Entrypoint Script

The Docker entrypoint validates the environment and runs migrations before starting the application:

Sources: docker/scripts/entrypoint.sh1-36

Multi-stage Build

The Dockerfile uses separate stages for dependencies, building, and running:

  1. pruner: Extracts only necessary package files
  2. dev-deps: Installs development dependencies
  3. production-deps: Installs only production dependencies
  4. builder: Builds the application
  5. runner: Final runtime image with minimal footprint

Sources: docker/Dockerfile1-116

Memory Configuration

The entrypoint allows configuring Node.js heap size:

Sources: docker/scripts/entrypoint.sh44-50


Configuration Reference Table

Critical Security Variables

VariableRequiredValidationPurpose
SESSION_SECRETYesStringSession cookie signing
MAGIC_LINK_SECRETYesStringMagic link token signing
ENCRYPTION_KEYYesExactly 32 bytesEncrypt sensitive data
PROVIDER_SECRETYesString (default: "provider-secret")Provider authentication
COORDINATOR_SECRETYesString (default: "coordinator-secret")Coordinator authentication
MANAGED_WORKER_SECRETYesString (default: "managed-secret")Worker authentication

Feature Flags

VariableDefaultDescription
V2_MARQS_ENABLED"0"Enable legacy MARQS queue system
MARQS_WORKER_ENABLED"0"Enable MARQS worker processing
EVENT_LOOP_MONITOR_ENABLED"1"Monitor event loop lag
RESOURCE_MONITOR_ENABLED"0"Monitor memory/CPU usage
DISABLE_COMPRESSION-Disable HTTP compression
DISABLE_SSE-Disable server-sent events

Sources: apps/webapp/app/env.server.ts264-665 apps/webapp/app/entry.server.tsx245-257