Skip to content

Commit 59f7145

Browse files
feat: Attempt to use environments for posthog-js deployment (#2757)
* feat: Attempt to use environments for posthog-js deployment * refactor: Use Github app By using the Github app we can skip the requirement for a CodeQL review * fix: Forcefully check `main` By checking main we guarantee we'll see the most recent commits rather than seeing the one from the action HEAD * fix: Avoid failing when changesets is not found No need to fail, we'll not proceed anyway * fix: Move `main` check to global config * refactor: Move to approver token It doesn't make sense to call it deployer because it approves a PR * refactor: move to releaser token
1 parent 6f5db7c commit 59f7145

File tree

4 files changed

+392
-354
lines changed

4 files changed

+392
-354
lines changed

.github/workflows/label-version-bump.yml

Lines changed: 0 additions & 61 deletions
This file was deleted.
Lines changed: 138 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -1,146 +1,146 @@
1-
name: "PostHog Upgrade"
1+
name: 'PostHog Upgrade'
22

33
on:
4-
workflow_dispatch:
5-
inputs:
6-
package_name:
7-
type: choice
8-
description: "Package name to upgrade"
9-
required: true
10-
options:
11-
- posthog-js
12-
package_version:
13-
description: "Package version to upgrade to"
14-
required: true
15-
type: string
4+
workflow_dispatch:
5+
inputs:
6+
package_name:
7+
type: choice
8+
description: 'Package name to upgrade'
9+
required: true
10+
options:
11+
- posthog-js
12+
package_version:
13+
description: 'Package version to upgrade to'
14+
required: true
15+
type: string
1616

1717
permissions:
18-
actions: write
19-
contents: read
18+
actions: write
19+
contents: read
2020

2121
jobs:
22-
posthog-upgrade:
23-
name: Upgrade PostHog Package
24-
runs-on: ubuntu-latest
25-
26-
steps:
27-
- name: Check out main repo
28-
uses: actions/checkout@v4
29-
with:
30-
repository: "PostHog/posthog"
31-
token: ${{ secrets.POSTHOG_BOT_PAT }}
32-
33-
- uses: pnpm/action-setup@41ff72655975bd51cab0327fa583b6e92b6d3061 # pin v4.2.0
34-
35-
- name: Set up Node.js
36-
uses: actions/setup-node@v3
37-
with:
38-
node-version: 20
39-
cache: pnpm
40-
41-
- name: Install new package version in main repo
42-
id: pnpm-upgrade
43-
shell: bash
44-
run: |
45-
OUTGOING_VERSION=$(jq '.dependencies["${{ github.event.inputs.package_name }}"]' package.json -r)
46-
echo "outgoing-version=$OUTGOING_VERSION" >> "$GITHUB_OUTPUT"
47-
for i in $(seq 1 $RETRY_TIMES); do
48-
# Retry loop because of npm being _eventually_ consistent
49-
if pnpm -r upgrade ${{ github.event.inputs.package_name }}@${{ github.event.inputs.package_version }}; then
50-
break
51-
else
52-
[ $i -ne $RETRY_TIMES ] && sleep $RETRY_WAIT_SECONDS || false
53-
fi
54-
done
55-
env:
56-
RETRY_TIMES: 20
57-
RETRY_WAIT_SECONDS: 5
58-
59-
- name: Install new package version in hedgebox-dummy
60-
shell: bash
61-
run: |
62-
if [ -d "hedgebox-dummy" ]; then
63-
cd hedgebox-dummy
64-
for i in $(seq 1 $RETRY_TIMES); do
65-
if pnpm upgrade ${{ github.event.inputs.package_name }}@${{ github.event.inputs.package_version }}; then
66-
break
22+
posthog-upgrade:
23+
name: Upgrade PostHog Package
24+
runs-on: ubuntu-latest
25+
26+
steps:
27+
- name: Check out main repo
28+
uses: actions/checkout@v4
29+
with:
30+
repository: 'PostHog/posthog'
31+
token: ${{ secrets.POSTHOG_BOT_PAT }}
32+
33+
- uses: pnpm/action-setup@41ff72655975bd51cab0327fa583b6e92b6d3061 # pin v4.2.0
34+
35+
- name: Set up Node.js
36+
uses: actions/setup-node@v3
37+
with:
38+
node-version: 20
39+
cache: pnpm
40+
41+
- name: Install new package version in main repo
42+
id: pnpm-upgrade
43+
shell: bash
44+
run: |
45+
OUTGOING_VERSION=$(jq '.dependencies["${{ github.event.inputs.package_name }}"]' package.json -r)
46+
echo "outgoing-version=$OUTGOING_VERSION" >> "$GITHUB_OUTPUT"
47+
for i in $(seq 1 $RETRY_TIMES); do
48+
# Retry loop because of npm being _eventually_ consistent
49+
if pnpm -r upgrade ${{ github.event.inputs.package_name }}@${{ github.event.inputs.package_version }}; then
50+
break
51+
else
52+
[ $i -ne $RETRY_TIMES ] && sleep $RETRY_WAIT_SECONDS || false
53+
fi
54+
done
55+
env:
56+
RETRY_TIMES: 20
57+
RETRY_WAIT_SECONDS: 5
58+
59+
- name: Install new package version in hedgebox-dummy
60+
shell: bash
61+
run: |
62+
if [ -d "hedgebox-dummy" ]; then
63+
cd hedgebox-dummy
64+
for i in $(seq 1 $RETRY_TIMES); do
65+
if pnpm upgrade ${{ github.event.inputs.package_name }}@${{ github.event.inputs.package_version }}; then
66+
break
67+
else
68+
[ $i -ne $RETRY_TIMES ] && sleep $RETRY_WAIT_SECONDS || false
69+
fi
70+
done
6771
else
68-
[ $i -ne $RETRY_TIMES ] && sleep $RETRY_WAIT_SECONDS || false
72+
echo "hedgebox-dummy folder not found, skipping"
6973
fi
70-
done
71-
else
72-
echo "hedgebox-dummy folder not found, skipping"
73-
fi
74-
env:
75-
RETRY_TIMES: 20
76-
RETRY_WAIT_SECONDS: 5
77-
78-
- name: Generate branch name
79-
id: generate-branch-name
80-
shell: bash
81-
run: |
82-
PACKAGE_NAME_SANITIZED=$(echo "${{ github.event.inputs.package_name }}" | sed 's/@//g' | sed 's/\//-/g')
83-
echo "branch_name=${PACKAGE_NAME_SANITIZED}-${{ github.event.inputs.package_version }}" >> "$GITHUB_OUTPUT"
84-
85-
- name: Create main repo pull request
86-
id: main-repo-pr
87-
uses: peter-evans/create-pull-request@18f7dc018cc2cd597073088f7c7591b9d1c02672 #v3.14.0
88-
with:
89-
token: ${{ secrets.POSTHOG_BOT_PAT }}
90-
commit-message: "chore(deps): Update ${{ github.event.inputs.package_name }} to ${{ github.event.inputs.package_version }}"
91-
branch: ${{ steps.generate-branch-name.outputs.branch_name }}
92-
delete-branch: true
93-
labels: automerge
94-
title: "chore(deps): Update ${{ github.event.inputs.package_name }} to ${{ github.event.inputs.package_version }}"
95-
body: |
96-
## Changes
97-
98-
${{ github.event.inputs.package_name }} version ${{ github.event.inputs.package_version }} has been released. This updates PostHog to use it.
99-
100-
https://github.com/PostHog/posthog-js/compare/${{ github.event.inputs.package_name }}@${{ steps.pnpm-upgrade.outputs.outgoing-version }}...${{ github.event.inputs.package_name }}@${{ github.event.inputs.package_version }} • [GitHub releases](https://github.com/PostHog/posthog-js/releases) • [npm releases](https://www.npmjs.com/package/${{ github.event.inputs.package_name }}?activeTab=version)
101-
102-
- name: Output pull request result
103-
shell: bash
104-
run: |
105-
echo "PostHog pull request for ${{ github.event.inputs.package_name }} version ${{ github.event.inputs.package_version }} ready: ${{ steps.main-repo-pr.outputs.pull-request-url }}"
106-
107-
- name: Get deployer token
108-
id: deployer
109-
uses: getsentry/action-github-app-token@97c9e23528286821f97fba885c1b1123284b29cc # v2
110-
with:
111-
app_id: ${{ secrets.GH_APP_POSTHOG_DEPLOYER_APP_ID }}
112-
private_key: ${{ secrets.GH_APP_POSTHOG_DEPLOYER_PRIVATE_KEY }}
113-
114-
- name: Stamp PR
115-
shell: bash
116-
run: |
117-
# unbelievably github has a race condition where if you commit and
118-
# approve too quickly on a PR with "auto-merge" enabled it can miss
119-
# the new commit in the merge commit (but it looks like the PR has the change)
120-
# Sleep 5 should work
121-
sleep 5
122-
pull_number=${{ steps.main-repo-pr.outputs.pull-request-number }}
123-
curl -L \
124-
-X POST \
125-
-H "Accept: application/vnd.github+json" \
126-
-H "Authorization: Bearer ${{ steps.deployer.outputs.token }}" \
127-
-H "X-GitHub-Api-Version: 2022-11-28" \
128-
https://api.github.com/repos/posthog/posthog/pulls/${pull_number}/reviews \
129-
-d '{"body":"${{ github.event.inputs.package_name }} auto approved.","event":"APPROVE","comments":[]}'
130-
131-
# https://us.posthog.com/project/11213/functions/019ae9c0-03ee-0000-2f32-971f64be8ffe
132-
- name: Send failure event to PostHog
133-
if: ${{ failure() }}
134-
uses: PostHog/posthog-github-action@v0.1
135-
with:
136-
posthog-token: "${{ secrets.POSTHOG_PROJECT_API_KEY }}"
137-
event: "posthog-js-github-release-workflow-failure"
138-
properties: >-
139-
{
140-
"commitSha": "${{ github.sha }}",
141-
"jobStatus": "${{ job.status }}",
142-
"ref": "${{ github.ref }}",
143-
"repository": "Posthog/posthog",
144-
"packageName": "${{ github.event.inputs.package_name }}",
145-
"packageVersion": "${{ github.event.inputs.package_version }}"
146-
}
74+
env:
75+
RETRY_TIMES: 20
76+
RETRY_WAIT_SECONDS: 5
77+
78+
- name: Generate branch name
79+
id: generate-branch-name
80+
shell: bash
81+
run: |
82+
PACKAGE_NAME_SANITIZED=$(echo "${{ github.event.inputs.package_name }}" | sed 's/@//g' | sed 's/\//-/g')
83+
echo "branch_name=${PACKAGE_NAME_SANITIZED}-${{ github.event.inputs.package_version }}" >> "$GITHUB_OUTPUT"
84+
85+
- name: Create main repo pull request
86+
id: main-repo-pr
87+
uses: peter-evans/create-pull-request@18f7dc018cc2cd597073088f7c7591b9d1c02672 #v3.14.0
88+
with:
89+
token: ${{ secrets.POSTHOG_BOT_PAT }}
90+
commit-message: 'chore(deps): Update ${{ github.event.inputs.package_name }} to ${{ github.event.inputs.package_version }}'
91+
branch: ${{ steps.generate-branch-name.outputs.branch_name }}
92+
delete-branch: true
93+
labels: automerge
94+
title: 'chore(deps): Update ${{ github.event.inputs.package_name }} to ${{ github.event.inputs.package_version }}'
95+
body: |
96+
## Changes
97+
98+
${{ github.event.inputs.package_name }} version ${{ github.event.inputs.package_version }} has been released. This updates PostHog to use it.
99+
100+
https://github.com/PostHog/posthog-js/compare/${{ github.event.inputs.package_name }}@${{ steps.pnpm-upgrade.outputs.outgoing-version }}...${{ github.event.inputs.package_name }}@${{ github.event.inputs.package_version }} • [GitHub releases](https://github.com/PostHog/posthog-js/releases) • [npm releases](https://www.npmjs.com/package/${{ github.event.inputs.package_name }}?activeTab=version)
101+
102+
- name: Output pull request result
103+
shell: bash
104+
run: |
105+
echo "PostHog pull request for ${{ github.event.inputs.package_name }} version ${{ github.event.inputs.package_version }} ready: ${{ steps.main-repo-pr.outputs.pull-request-url }}"
106+
107+
- name: Get approver token
108+
id: approver
109+
uses: getsentry/action-github-app-token@97c9e23528286821f97fba885c1b1123284b29cc # v2
110+
with:
111+
app_id: ${{ secrets.GH_APP_POSTHOG_APPROVER_APP_ID }}
112+
private_key: ${{ secrets.GH_APP_POSTHOG_APPROVER_PRIVATE_KEY }}
113+
114+
- name: Stamp PR
115+
shell: bash
116+
run: |
117+
# unbelievably github has a race condition where if you commit and
118+
# approve too quickly on a PR with "auto-merge" enabled it can miss
119+
# the new commit in the merge commit (but it looks like the PR has the change)
120+
# Sleep 5 should work
121+
sleep 5
122+
pull_number=${{ steps.main-repo-pr.outputs.pull-request-number }}
123+
curl -L \
124+
-X POST \
125+
-H "Accept: application/vnd.github+json" \
126+
-H "Authorization: Bearer ${{ steps.approver.outputs.token }}" \
127+
-H "X-GitHub-Api-Version: 2022-11-28" \
128+
https://api.github.com/repos/posthog/posthog/pulls/${pull_number}/reviews \
129+
-d '{"body":"${{ github.event.inputs.package_name }} auto approved.","event":"APPROVE","comments":[]}'
130+
131+
# https://us.posthog.com/project/11213/functions/019ae9c0-03ee-0000-2f32-971f64be8ffe
132+
- name: Send failure event to PostHog
133+
if: ${{ failure() }}
134+
uses: PostHog/posthog-github-action@v0.1
135+
with:
136+
posthog-token: '${{ secrets.POSTHOG_PROJECT_API_KEY }}'
137+
event: 'posthog-js-github-release-workflow-failure'
138+
properties: >-
139+
{
140+
"commitSha": "${{ github.sha }}",
141+
"jobStatus": "${{ job.status }}",
142+
"ref": "${{ github.ref }}",
143+
"repository": "Posthog/posthog",
144+
"packageName": "${{ github.event.inputs.package_name }}",
145+
"packageVersion": "${{ github.event.inputs.package_version }}"
146+
}

0 commit comments

Comments
 (0)