posthog-deploy-integration

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

PostHog Deploy Integration

PostHog 部署集成

Overview

概述

Deploy PostHog-powered applications to popular platforms with proper secrets management.
将基于PostHog的应用部署至主流平台,并实现合规的密钥管理。

Prerequisites

前提条件

  • PostHog API keys for production environment
  • Platform CLI installed (vercel, fly, or gcloud)
  • Application code ready for deployment
  • Environment variables documented
  • 生产环境的PostHog API密钥
  • 已安装对应平台的CLI(vercel、fly或gcloud)
  • 应用代码已准备好部署
  • 环境变量已完成文档记录

Vercel Deployment

Vercel 部署

Environment Setup

环境配置

bash
undefined
bash
undefined

Add PostHog secrets to Vercel

Add PostHog secrets to Vercel

vercel secrets add posthog_api_key sk_live_*** vercel secrets add posthog_webhook_secret whsec_***
vercel secrets add posthog_api_key sk_live_*** vercel secrets add posthog_webhook_secret whsec_***

Link to project

Link to project

vercel link
vercel link

Deploy preview

Deploy preview

vercel
vercel

Deploy production

Deploy production

vercel --prod
undefined
vercel --prod
undefined

vercel.json Configuration

vercel.json 配置

json
{
  "env": {
    "POSTHOG_API_KEY": "@posthog_api_key"
  },
  "functions": {
    "api/**/*.ts": {
      "maxDuration": 30
    }
  }
}
json
{
  "env": {
    "POSTHOG_API_KEY": "@posthog_api_key"
  },
  "functions": {
    "api/**/*.ts": {
      "maxDuration": 30
    }
  }
}

Fly.io Deployment

Fly.io 部署

fly.toml

fly.toml

toml
app = "my-posthog-app"
primary_region = "iad"

[env]
  NODE_ENV = "production"

[http_service]
  internal_port = 3000
  force_https = true
  auto_stop_machines = true
  auto_start_machines = true
toml
app = "my-posthog-app"
primary_region = "iad"

[env]
  NODE_ENV = "production"

[http_service]
  internal_port = 3000
  force_https = true
  auto_stop_machines = true
  auto_start_machines = true

Secrets

密钥配置

bash
undefined
bash
undefined

Set PostHog secrets

Set PostHog secrets

fly secrets set POSTHOG_API_KEY=sk_live_*** fly secrets set POSTHOG_WEBHOOK_SECRET=whsec_***
fly secrets set POSTHOG_API_KEY=sk_live_*** fly secrets set POSTHOG_WEBHOOK_SECRET=whsec_***

Deploy

Deploy

fly deploy
undefined
fly deploy
undefined

Google Cloud Run

Google Cloud Run 部署

Dockerfile

Dockerfile

dockerfile
FROM node:20-slim
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
CMD ["npm", "start"]
dockerfile
FROM node:20-slim
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
CMD ["npm", "start"]

Deploy Script

部署脚本

bash
#!/bin/bash
bash
#!/bin/bash

deploy-cloud-run.sh

deploy-cloud-run.sh

PROJECT_ID="${GOOGLE_CLOUD_PROJECT}" SERVICE_NAME="posthog-service" REGION="us-central1"
PROJECT_ID="${GOOGLE_CLOUD_PROJECT}" SERVICE_NAME="posthog-service" REGION="us-central1"

Build and push image

Build and push image

gcloud builds submit --tag gcr.io/$PROJECT_ID/$SERVICE_NAME
gcloud builds submit --tag gcr.io/$PROJECT_ID/$SERVICE_NAME

Deploy to Cloud Run

Deploy to Cloud Run

gcloud run deploy $SERVICE_NAME
--image gcr.io/$PROJECT_ID/$SERVICE_NAME
--region $REGION
--platform managed
--allow-unauthenticated
--set-secrets=POSTHOG_API_KEY=posthog-api-key:latest
undefined
gcloud run deploy $SERVICE_NAME
--image gcr.io/$PROJECT_ID/$SERVICE_NAME
--region $REGION
--platform managed
--allow-unauthenticated
--set-secrets=POSTHOG_API_KEY=posthog-api-key:latest
undefined

Environment Configuration Pattern

环境配置模式

typescript
// config/posthog.ts
interface PostHogConfig {
  apiKey: string;
  environment: 'development' | 'staging' | 'production';
  webhookSecret?: string;
}

export function getPostHogConfig(): PostHogConfig {
  const env = process.env.NODE_ENV || 'development';

  return {
    apiKey: process.env.POSTHOG_API_KEY!,
    environment: env as PostHogConfig['environment'],
    webhookSecret: process.env.POSTHOG_WEBHOOK_SECRET,
  };
}
typescript
// config/posthog.ts
interface PostHogConfig {
  apiKey: string;
  environment: 'development' | 'staging' | 'production';
  webhookSecret?: string;
}

export function getPostHogConfig(): PostHogConfig {
  const env = process.env.NODE_ENV || 'development';

  return {
    apiKey: process.env.POSTHOG_API_KEY!,
    environment: env as PostHogConfig['environment'],
    webhookSecret: process.env.POSTHOG_WEBHOOK_SECRET,
  };
}

Health Check Endpoint

健康检查端点

typescript
// api/health.ts
export async function GET() {
  const posthogStatus = await checkPostHogConnection();

  return Response.json({
    status: posthogStatus ? 'healthy' : 'degraded',
    services: {
      posthog: posthogStatus,
    },
    timestamp: new Date().toISOString(),
  });
}
typescript
// api/health.ts
export async function GET() {
  const posthogStatus = await checkPostHogConnection();

  return Response.json({
    status: posthogStatus ? 'healthy' : 'degraded',
    services: {
      posthog: posthogStatus,
    },
    timestamp: new Date().toISOString(),
  });
}

Instructions

操作步骤

Step 1: Choose Deployment Platform

步骤1:选择部署平台

Select the platform that best fits your infrastructure needs and follow the platform-specific guide below.
选择最符合您基础设施需求的平台,然后遵循下方对应平台的指南操作。

Step 2: Configure Secrets

步骤2:配置密钥

Store PostHog API keys securely using the platform's secrets management.
通过平台的密钥管理功能安全存储PostHog API密钥。

Step 3: Deploy Application

步骤3:部署应用

Use the platform CLI to deploy your application with PostHog integration.
使用平台CLI部署集成了PostHog的应用。

Step 4: Verify Health

步骤4:验证健康状态

Test the health check endpoint to confirm PostHog connectivity.
测试健康检查端点,确认与PostHog的连接状态。

Output

输出结果

  • Application deployed to production
  • PostHog secrets securely configured
  • Health check endpoint functional
  • Environment-specific configuration in place
  • 应用已部署至生产环境
  • PostHog密钥已完成安全配置
  • 健康检查端点可正常运行
  • 已配置好环境专属的设置

Error Handling

错误处理

IssueCauseSolution
Secret not foundMissing configurationAdd secret via platform CLI
Deploy timeoutLarge buildIncrease build timeout
Health check failsWrong API keyVerify environment variable
Cold start issuesNo warm-upConfigure minimum instances
问题原因解决方案
密钥未找到配置缺失通过平台CLI添加密钥
部署超时构建包过大延长构建超时时间
健康检查失败API密钥错误验证环境变量
冷启动问题未配置预热设置最小实例数

Examples

示例

Quick Deploy Script

快速部署脚本

bash
#!/bin/bash
bash
#!/bin/bash

Platform-agnostic deploy helper

Platform-agnostic deploy helper

case "$1" in vercel) vercel secrets add posthog_api_key "$POSTHOG_API_KEY" vercel --prod ;; fly) fly secrets set POSTHOG_API_KEY="$POSTHOG_API_KEY" fly deploy ;; esac
undefined
case "$1" in vercel) vercel secrets add posthog_api_key "$POSTHOG_API_KEY" vercel --prod ;; fly) fly secrets set POSTHOG_API_KEY="$POSTHOG_API_KEY" fly deploy ;; esac
undefined

Resources

资源链接

Next Steps

后续步骤

For webhook handling, see
posthog-webhooks-events
.
如需处理Webhook,请查看
posthog-webhooks-events