Skip to content

Commit 5850fcc

Browse files
authored
chore(ci): automate PostHog Python SDK upgrades (#870)
* chore(ci): automate PostHog Python SDK upgrades * chore(ci): update every PostHog SDK dependency
1 parent 8f3a568 commit 5850fcc

2 files changed

Lines changed: 175 additions & 0 deletions

File tree

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
name: "PostHog Upgrade"
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
package_name:
7+
type: choice
8+
description: "Package name to upgrade"
9+
required: true
10+
options:
11+
- posthoganalytics
12+
package_version:
13+
description: "Package version to upgrade to"
14+
required: true
15+
type: string
16+
17+
permissions:
18+
contents: read
19+
20+
jobs:
21+
posthog-upgrade:
22+
name: Upgrade PostHog package
23+
runs-on: ubuntu-latest
24+
timeout-minutes: 15
25+
26+
steps:
27+
- name: Get upgrader token
28+
id: upgrader
29+
uses: getsentry/action-github-app-token@5c1e90706fe007857338ac1bfbd7a4177db2f789 # v4.0.0
30+
with:
31+
app_id: ${{ secrets.GH_APP_POSTHOG_JS_UPGRADER_APP_ID }}
32+
private_key: ${{ secrets.GH_APP_POSTHOG_JS_UPGRADER_PRIVATE_KEY }}
33+
34+
- name: Check out main repo
35+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
36+
with:
37+
repository: "PostHog/posthog"
38+
token: ${{ steps.upgrader.outputs.token }}
39+
40+
- name: Install uv
41+
uses: astral-sh/setup-uv@37802adc94f370d6bfd71619e3f0bf239e1f3b78 # v7.6.0
42+
with:
43+
version: "0.11.28"
44+
45+
- name: Install new package version in main repo
46+
shell: bash
47+
env:
48+
RETRY_TIMES: 20
49+
RETRY_WAIT_SECONDS: 5
50+
PACKAGE_NAME: ${{ github.event.inputs.package_name }}
51+
PACKAGE_VERSION: ${{ github.event.inputs.package_version }}
52+
run: |
53+
for i in $(seq 1 "$RETRY_TIMES"); do
54+
# Retry because the PyPI index is eventually consistent.
55+
if uv add --no-sync --refresh-package "$PACKAGE_NAME" "$PACKAGE_NAME==$PACKAGE_VERSION" && \
56+
(cd services/llm-gateway && uv add --no-sync --refresh-package "$PACKAGE_NAME" "$PACKAGE_NAME>=$PACKAGE_VERSION"); then
57+
break
58+
else
59+
if [ "$i" -eq "$RETRY_TIMES" ]; then
60+
exit 1
61+
fi
62+
sleep "$RETRY_WAIT_SECONDS"
63+
fi
64+
done
65+
66+
sed -i "s/^posthoganalytics==.*/posthoganalytics==$PACKAGE_VERSION/" common/ingestion/acceptance_tests/requirements.txt
67+
sed -i "s/^posthog>=.*/posthog>=$PACKAGE_VERSION/" tools/infra-scripts/clitools/requirements.txt
68+
69+
expected_files=(
70+
pyproject.toml
71+
uv.lock
72+
services/llm-gateway/pyproject.toml
73+
services/llm-gateway/uv.lock
74+
common/ingestion/acceptance_tests/requirements.txt
75+
tools/infra-scripts/clitools/requirements.txt
76+
)
77+
for file in "${expected_files[@]}"; do
78+
if git diff --quiet -- "$file"; then
79+
echo "Failed to update $file to $PACKAGE_VERSION" >&2
80+
exit 1
81+
fi
82+
done
83+
84+
- name: Generate branch name
85+
id: generate-branch-name
86+
shell: bash
87+
env:
88+
PACKAGE_NAME: ${{ github.event.inputs.package_name }}
89+
PACKAGE_VERSION: ${{ github.event.inputs.package_version }}
90+
run: |
91+
echo "branch_name=${PACKAGE_NAME}-${PACKAGE_VERSION}" >> "$GITHUB_OUTPUT"
92+
93+
- name: Create main repo pull request
94+
id: main-repo-pr
95+
uses: peter-evans/create-pull-request@5f6978faf089d4d20b00c7766989d076bb2fc7f1 # v8.1.1
96+
env:
97+
HUSKY: "0"
98+
with:
99+
token: ${{ steps.upgrader.outputs.token }}
100+
# PostHog/posthog requires signed commits, so commit through the GitHub API.
101+
sign-commits: true
102+
commit-message: "chore(deps): update ${{ github.event.inputs.package_name }} to ${{ github.event.inputs.package_version }}"
103+
branch: ${{ steps.generate-branch-name.outputs.branch_name }}
104+
delete-branch: true
105+
title: "chore(deps): update ${{ github.event.inputs.package_name }} to ${{ github.event.inputs.package_version }}"
106+
body: |
107+
## Changes
108+
109+
PostHog Python SDK version ${{ github.event.inputs.package_version }} has been released. This updates the `posthoganalytics` and `posthog` dependency declarations to the same SDK version.
110+
111+
[GitHub releases](https://github.com/PostHog/posthog-python/releases) • [`posthoganalytics` on PyPI](https://pypi.org/project/posthoganalytics/#history) • [`posthog` on PyPI](https://pypi.org/project/posthog/#history)
112+
113+
- name: Output pull request result
114+
shell: bash
115+
env:
116+
PACKAGE_NAME: ${{ github.event.inputs.package_name }}
117+
PACKAGE_VERSION: ${{ github.event.inputs.package_version }}
118+
PR_URL: ${{ steps.main-repo-pr.outputs.pull-request-url }}
119+
run: |
120+
echo "PostHog pull request for $PACKAGE_NAME version $PACKAGE_VERSION ready: $PR_URL"
121+
122+
- name: Enable automerge
123+
env:
124+
GH_TOKEN: ${{ steps.upgrader.outputs.token }}
125+
PR_NUMBER: ${{ steps.main-repo-pr.outputs.pull-request-number }}
126+
run: |
127+
gh pr merge "$PR_NUMBER" --repo PostHog/posthog --auto --squash
128+
129+
- name: Assign reviewers
130+
env:
131+
GH_TOKEN: ${{ steps.upgrader.outputs.token }}
132+
PR_NUMBER: ${{ steps.main-repo-pr.outputs.pull-request-number }}
133+
run: |
134+
gh pr edit "$PR_NUMBER" --repo PostHog/posthog --add-reviewer PostHog/client-libraries-approvers --add-reviewer PostHog/team-client-libraries
135+
136+
# https://us.posthog.com/project/11213/functions/019ae9c0-03ee-0000-2f32-971f64be8ffe
137+
- name: Send failure event to PostHog
138+
if: ${{ failure() }}
139+
continue-on-error: true
140+
uses: PostHog/posthog-github-action@9a6a19d360820ab4d95ecc4a5a7564062c895f84 # v1.3.0
141+
with:
142+
posthog-token: "${{ secrets.POSTHOG_PROJECT_API_KEY }}"
143+
event: "posthog-sdk-github-release-workflow-failure"
144+
properties: >-
145+
{
146+
"commitSha": "${{ github.sha }}",
147+
"jobStatus": "${{ job.status }}",
148+
"ref": "${{ github.ref }}",
149+
"repository": "PostHog/posthog",
150+
"sdk": "posthog-python",
151+
"jobName": "posthog-upgrade",
152+
"packageName": "${{ github.event.inputs.package_name }}",
153+
"packageVersion": "${{ github.event.inputs.package_version }}",
154+
"actionUrl": "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}",
155+
"alertText": ":rotating_light: Failed to open the PostHog upgrade PR for posthoganalytics@${{ github.event.inputs.package_version }} :rotating_light:",
156+
"alertContext": "The posthog-upgrade workflow failed before completion."
157+
}

.github/workflows/release.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ jobs:
254254
runs-on: ubuntu-latest
255255
if: always() && needs.version-bump.outputs.commit-hash != ''
256256
permissions:
257+
actions: write
257258
contents: write
258259
id-token: write
259260
strategy:
@@ -395,6 +396,23 @@ jobs:
395396
--title "$TAG" \
396397
--notes-file release-notes.md
397398
399+
- name: Dispatch PostHog upgrade
400+
if: steps.detect.outputs.has-new-version == 'true' && matrix.package.name == 'posthoganalytics'
401+
env:
402+
PACKAGE_VERSION: ${{ steps.detect.outputs.version }}
403+
GITHUB_TOKEN: ${{ github.token }}
404+
run: |
405+
curl -f -sS -X POST \
406+
-H "Accept: application/vnd.github+json" \
407+
-H "Authorization: Bearer $GITHUB_TOKEN" \
408+
https://api.github.com/repos/posthog/posthog-python/actions/workflows/posthog-upgrade.yml/dispatches \
409+
-d "$(jq -n \
410+
--arg ref "main" \
411+
--arg package_name "posthoganalytics" \
412+
--arg package_version "$PACKAGE_VERSION" \
413+
'{ref: $ref, inputs: {package_name: $package_name, package_version: $package_version}}' \
414+
)"
415+
398416
# Notify in case of a failure
399417
- name: Send failure event to PostHog
400418
if: ${{ failure() }}

0 commit comments

Comments
 (0)