Skip to content

Commit 33b5418

Browse files
Pass PostHog key at build time (#92)
1 parent c73c344 commit 33b5418

File tree

11 files changed

+48
-34
lines changed

11 files changed

+48
-34
lines changed

.github/workflows/ghcr-publish.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ jobs:
7575
outputs: type=image,name=${{ env.REGISTRY_IMAGE }},push-by-digest=true,name-canonical=true,push=true
7676
build-args: |
7777
SOURCEBOT_VERSION=${{ github.ref_name }}
78+
POSTHOG_PAPIK=${{ secrets.POSTHOG_PAPIK }}
7879
7980
- name: Export digest
8081
run: |

Dockerfile

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ ENV NEXT_TELEMETRY_DISABLED=1
2626
# @see: https://phase.dev/blog/nextjs-public-runtime-variables/
2727
ARG NEXT_PUBLIC_SOURCEBOT_TELEMETRY_DISABLED=BAKED_NEXT_PUBLIC_SOURCEBOT_TELEMETRY_DISABLED
2828
ARG NEXT_PUBLIC_SOURCEBOT_VERSION=BAKED_NEXT_PUBLIC_SOURCEBOT_VERSION
29+
ENV NEXT_PUBLIC_POSTHOG_PAPIK=BAKED_NEXT_PUBLIC_POSTHOG_PAPIK
2930
# @note: leading "/" is required for the basePath property. @see: https://nextjs.org/docs/app/api-reference/next-config-js/basePath
3031
ARG NEXT_PUBLIC_DOMAIN_SUB_PATH=/BAKED_NEXT_PUBLIC_DOMAIN_SUB_PATH
3132
RUN yarn workspace @sourcebot/web build
@@ -61,9 +62,12 @@ ENV SOURCEBOT_LOG_LEVEL=info
6162
# will serve from http(s)://example.com/sb
6263
ENV DOMAIN_SUB_PATH=/
6364

64-
# @note: This is also set in .env
65-
ENV POSTHOG_KEY=phc_VFn4CkEGHRdlVyOOw8mfkoj1DKVoG6y1007EClvzAnS
66-
ENV NEXT_PUBLIC_POSTHOG_KEY=$POSTHOG_KEY
65+
# PAPIK = Project API Key
66+
# Note that this key does not need to be kept secret, so it's not
67+
# necessary to use Docker build secrets here.
68+
# @see: https://posthog.com/tutorials/api-capture-events#authenticating-with-the-project-api-key
69+
ARG POSTHOG_PAPIK=
70+
ENV POSTHOG_PAPIK=$POSTHOG_PAPIK
6771

6872
# Sourcebot collects anonymous usage data using [PostHog](https://posthog.com/). Uncomment this line to disable.
6973
# ENV SOURCEBOT_TELEMETRY_DISABLED=1

entrypoint.sh

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ set -e
33

44
echo -e "\e[34m[Info] Sourcebot version: $SOURCEBOT_VERSION\e[0m"
55

6+
# If we don't have a PostHog key, then we need to disable telemetry.
7+
if [ -z "$POSTHOG_PAPIK" ]; then
8+
echo -e "\e[33m[Warning] POSTHOG_PAPIK was not set. Setting SOURCEBOT_TELEMETRY_DISABLED.\e[0m"
9+
export SOURCEBOT_TELEMETRY_DISABLED=1
10+
fi
11+
612
# Issue a info message about telemetry
713
if [ ! -z "$SOURCEBOT_TELEMETRY_DISABLED" ]; then
814
echo -e "\e[34m[Info] Disabling telemetry since SOURCEBOT_TELEMETRY_DISABLED was set.\e[0m"
@@ -25,7 +31,7 @@ if [ ! -f "$FIRST_RUN_FILE" ]; then
2531
# (if telemetry is enabled)
2632
if [ -z "$SOURCEBOT_TELEMETRY_DISABLED" ]; then
2733
curl -L -s --header "Content-Type: application/json" -d '{
28-
"api_key": "'"$POSTHOG_KEY"'",
34+
"api_key": "'"$POSTHOG_PAPIK"'",
2935
"event": "install",
3036
"distinct_id": "'"$SOURCEBOT_INSTALL_ID"'",
3137
"properties": {
@@ -43,7 +49,7 @@ else
4349

4450
if [ -z "$SOURCEBOT_TELEMETRY_DISABLED" ]; then
4551
curl -L -s --header "Content-Type: application/json" -d '{
46-
"api_key": "'"$POSTHOG_KEY"'",
52+
"api_key": "'"$POSTHOG_PAPIK"'",
4753
"event": "upgrade",
4854
"distinct_id": "'"$SOURCEBOT_INSTALL_ID"'",
4955
"properties": {
@@ -83,12 +89,16 @@ echo -e "\e[34m[Info] Using config file at: '$CONFIG_PATH'.\e[0m"
8389
export NEXT_PUBLIC_SOURCEBOT_VERSION="$SOURCEBOT_VERSION"
8490
fi
8591

92+
# Always infer NEXT_PUBLIC_POSTHOG_PAPIK
93+
export NEXT_PUBLIC_POSTHOG_PAPIK="$POSTHOG_PAPIK"
94+
8695
# Iterate over all .js files in .next & public, making substitutions for the `BAKED_` sentinal values
8796
# with their actual desired runtime value.
8897
find /app/packages/web/public /app/packages/web/.next -type f -name "*.js" |
8998
while read file; do
9099
sed -i "s|BAKED_NEXT_PUBLIC_SOURCEBOT_TELEMETRY_DISABLED|${NEXT_PUBLIC_SOURCEBOT_TELEMETRY_DISABLED}|g" "$file"
91100
sed -i "s|BAKED_NEXT_PUBLIC_SOURCEBOT_VERSION|${NEXT_PUBLIC_SOURCEBOT_VERSION}|g" "$file"
101+
sed -i "s|BAKED_NEXT_PUBLIC_POSTHOG_PAPIK|${NEXT_PUBLIC_POSTHOG_PAPIK}|g" "$file"
92102
done
93103
}
94104

packages/backend/.env

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
11
POSTHOG_HOST=https://us.i.posthog.com
2-
3-
# @note: This is also set in the Dockerfile
4-
POSTHOG_KEY=phc_VFn4CkEGHRdlVyOOw8mfkoj1DKVoG6y1007EClvzAnS

packages/backend/src/environment.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import dotenv from 'dotenv';
22

3-
export const getEnv = (env: string | undefined, defaultValue = '') => {
3+
export const getEnv = (env: string | undefined, defaultValue?: string) => {
44
return env ?? defaultValue;
55
}
66

@@ -15,9 +15,9 @@ dotenv.config({
1515
path: './.env',
1616
});
1717

18-
export const SOURCEBOT_LOG_LEVEL = getEnv(process.env.SOURCEBOT_LOG_LEVEL, 'info');
19-
export const SOURCEBOT_TELEMETRY_DISABLED = getEnvBoolean(process.env.SOURCEBOT_TELEMETRY_DISABLED, false);
20-
export const SOURCEBOT_INSTALL_ID = getEnv(process.env.SOURCEBOT_INSTALL_ID, 'unknown');
21-
export const SOURCEBOT_VERSION = getEnv(process.env.SOURCEBOT_VERSION, 'unknown');
22-
export const POSTHOG_KEY = getEnv(process.env.POSTHOG_KEY);
18+
export const SOURCEBOT_LOG_LEVEL = getEnv(process.env.SOURCEBOT_LOG_LEVEL, 'info')!;
19+
export const SOURCEBOT_TELEMETRY_DISABLED = getEnvBoolean(process.env.SOURCEBOT_TELEMETRY_DISABLED, false)!;
20+
export const SOURCEBOT_INSTALL_ID = getEnv(process.env.SOURCEBOT_INSTALL_ID, 'unknown')!;
21+
export const SOURCEBOT_VERSION = getEnv(process.env.SOURCEBOT_VERSION, 'unknown')!;
22+
export const POSTHOG_PAPIK = getEnv(process.env.POSTHOG_PAPIK);
2323
export const POSTHOG_HOST = getEnv(process.env.POSTHOG_HOST);

packages/backend/src/posthog.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
import { PostHog } from 'posthog-node';
22
import { PosthogEvent, PosthogEventMap } from './posthogEvents.js';
3-
import { POSTHOG_HOST, POSTHOG_KEY, SOURCEBOT_INSTALL_ID, SOURCEBOT_TELEMETRY_DISABLED, SOURCEBOT_VERSION } from './environment.js';
3+
import { POSTHOG_HOST, POSTHOG_PAPIK, SOURCEBOT_INSTALL_ID, SOURCEBOT_TELEMETRY_DISABLED, SOURCEBOT_VERSION } from './environment.js';
44

5-
const posthog = new PostHog(
6-
POSTHOG_KEY,
7-
{
8-
host: POSTHOG_HOST,
9-
}
10-
);
5+
let posthog: PostHog | undefined = undefined;
6+
7+
if (POSTHOG_PAPIK) {
8+
posthog = new PostHog(
9+
POSTHOG_PAPIK,
10+
{
11+
host: POSTHOG_HOST,
12+
}
13+
);
14+
}
1115

1216
export function captureEvent<E extends PosthogEvent>(event: E, properties: PosthogEventMap[E]) {
1317
if (SOURCEBOT_TELEMETRY_DISABLED) {
1418
return;
1519
}
1620

17-
posthog.capture({
21+
posthog?.capture({
1822
distinctId: SOURCEBOT_INSTALL_ID,
1923
event: event,
2024
properties: {
@@ -24,4 +28,4 @@ export function captureEvent<E extends PosthogEvent>(event: E, properties: Posth
2428
});
2529
}
2630

27-
await posthog.shutdown();
31+
await posthog?.shutdown();

packages/web/.env

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
NEXT_PUBLIC_POSTHOG_HOST=https://us.i.posthog.com
22
NEXT_PUBLIC_POSTHOG_ASSET_HOST=https://us-assets.i.posthog.com
33
NEXT_PUBLIC_POSTHOG_UI_HOST=https://us.posthog.com
4-
5-
# @note: This is also set in the Dockerfile.
6-
NEXT_PUBLIC_POSTHOG_KEY=phc_VFn4CkEGHRdlVyOOw8mfkoj1DKVoG6y1007EClvzAnS

packages/web/src/app/posthogProvider.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
'use client'
2-
import { NEXT_PUBLIC_POSTHOG_KEY, NEXT_PUBLIC_POSTHOG_UI_HOST, NEXT_PUBLIC_SOURCEBOT_TELEMETRY_DISABLED } from '@/lib/environment.client'
2+
import { NEXT_PUBLIC_POSTHOG_PAPIK, NEXT_PUBLIC_POSTHOG_UI_HOST, NEXT_PUBLIC_SOURCEBOT_TELEMETRY_DISABLED } from '@/lib/environment.client'
33
import posthog from 'posthog-js'
44
import { PostHogProvider } from 'posthog-js/react'
55
import { resolveServerPath } from './api/(client)/client'
6+
import { isDefined } from '@/lib/utils'
67

78
if (typeof window !== 'undefined') {
8-
if (!NEXT_PUBLIC_SOURCEBOT_TELEMETRY_DISABLED) {
9+
if (!NEXT_PUBLIC_SOURCEBOT_TELEMETRY_DISABLED && isDefined(NEXT_PUBLIC_POSTHOG_PAPIK)) {
910
// @see next.config.mjs for path rewrites to the "/ingest" route.
1011
const posthogHostPath = resolveServerPath('/ingest');
1112

12-
posthog.init(NEXT_PUBLIC_POSTHOG_KEY!, {
13+
posthog.init(NEXT_PUBLIC_POSTHOG_PAPIK, {
1314
api_host: posthogHostPath,
1415
ui_host: NEXT_PUBLIC_POSTHOG_UI_HOST,
1516
person_profiles: 'identified_only',

packages/web/src/lib/environment.client.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import 'client-only';
22

33
import { getEnv, getEnvBoolean } from "./utils";
44

5-
export const NEXT_PUBLIC_POSTHOG_KEY = getEnv(process.env.NEXT_PUBLIC_POSTHOG_KEY);
5+
export const NEXT_PUBLIC_POSTHOG_PAPIK = getEnv(process.env.NEXT_PUBLIC_POSTHOG_PAPIK);
66
export const NEXT_PUBLIC_POSTHOG_HOST = getEnv(process.env.NEXT_PUBLIC_POSTHOG_HOST);
77
export const NEXT_PUBLIC_POSTHOG_UI_HOST = getEnv(process.env.NEXT_PUBLIC_POSTHOG_UI_HOST);
88
export const NEXT_PUBLIC_POSTHOG_ASSET_HOST = getEnv(process.env.NEXT_PUBLIC_POSTHOG_ASSET_HOST);
99
export const NEXT_PUBLIC_SOURCEBOT_TELEMETRY_DISABLED = getEnvBoolean(process.env.NEXT_PUBLIC_SOURCEBOT_TELEMETRY_DISABLED, false);
10-
export const NEXT_PUBLIC_SOURCEBOT_VERSION = getEnv(process.env.NEXT_PUBLIC_SOURCEBOT_VERSION, "unknown");
11-
export const NEXT_PUBLIC_DOMAIN_SUB_PATH = getEnv(process.env.NEXT_PUBLIC_DOMAIN_SUB_PATH, "");
10+
export const NEXT_PUBLIC_SOURCEBOT_VERSION = getEnv(process.env.NEXT_PUBLIC_SOURCEBOT_VERSION, "unknown")!;
11+
export const NEXT_PUBLIC_DOMAIN_SUB_PATH = getEnv(process.env.NEXT_PUBLIC_DOMAIN_SUB_PATH, "")!;

packages/web/src/lib/environment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'server-only';
22

33
import { getEnv, getEnvNumber } from "./utils";
44

5-
export const ZOEKT_WEBSERVER_URL = getEnv(process.env.ZOEKT_WEBSERVER_URL, "http://localhost:6070");
5+
export const ZOEKT_WEBSERVER_URL = getEnv(process.env.ZOEKT_WEBSERVER_URL, "http://localhost:6070")!;
66
export const SHARD_MAX_MATCH_COUNT = getEnvNumber(process.env.SHARD_MAX_MATCH_COUNT, 10000);
77
export const TOTAL_MAX_MATCH_COUNT = getEnvNumber(process.env.TOTAL_MAX_MATCH_COUNT, 100000);
88
export const NODE_ENV = process.env.NODE_ENV;

0 commit comments

Comments
 (0)