Skip to content

Commit f6e6e3f

Browse files
committed
chore(migration): update publish workflow (#2072)
1 parent 1eaa3bd commit f6e6e3f

File tree

13 files changed

+374
-301
lines changed

13 files changed

+374
-301
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
name: "Posthog.com auto upgrade"
2+
description: "Create main repo PR with new posthog-js version"
3+
4+
inputs:
5+
package_name:
6+
description: "package name to upgrade"
7+
required: true
8+
package_version:
9+
description: "package version to upgrade to"
10+
required: true
11+
github_bot_token:
12+
description: "github bot token"
13+
required: true
14+
15+
runs:
16+
using: "composite"
17+
steps:
18+
- name: Check out main repo
19+
uses: actions/checkout@v2
20+
with:
21+
repository: "PostHog/posthog.com"
22+
token: ${{ inputs.github_bot_token }}
23+
24+
- name: Set up Node.js
25+
uses: actions/setup-node@v3
26+
with:
27+
node-version: 18
28+
cache: yarn
29+
30+
- name: Install new posthog-js version in posthog.com repo
31+
shell: bash
32+
id: yarn-upgrade
33+
run: |
34+
OUTGOING_VERSION=$(jq '.dependencies["${{ inputs.package_name }}"]' package.json -r)
35+
echo "outgoing-version=$OUTGOING_VERSION" >> "$GITHUB_OUTPUT"
36+
for i in $(seq 1 $RETRY_TIMES); do
37+
# Retry loop because of npm being _eventually_ consistent
38+
if yarn upgrade ${{ inputs.package_name }}@${{ inputs.package_version }}; then
39+
break
40+
else
41+
[ $i -ne $RETRY_TIMES ] && sleep $RETRY_WAIT_SECONDS || false
42+
fi
43+
done
44+
env:
45+
RETRY_TIMES: 20
46+
RETRY_WAIT_SECONDS: 5
47+
48+
- name: Create posthog.com repo pull request
49+
id: com-repo-pr
50+
uses: peter-evans/create-pull-request@v3
51+
with:
52+
token: ${{ inputs.github_bot_token }}
53+
commit-message: "chore(deps): Update ${{ inputs.package_name }} to ${{ inputs.package_version }}"
54+
branch: ${{ replace(replace(inputs.package_name, '@', ''), '/', '-') }}-${{ inputs.package_version }}
55+
delete-branch: true
56+
labels: automerge
57+
title: "chore(deps): Update ${{ inputs.package_name }} to ${{ inputs.package_version }}"
58+
body: |
59+
## Changes
60+
61+
${{ inputs.package_name }} version ${{ inputs.package_version }} has been released. This updates PostHog to use it.
62+
63+
https://github.com/PostHog/posthog-js/compare/${{ inputs.package_name }}@${{ steps.yarn-upgrade.outputs.outgoing-version }}...${{ inputs.package_name }}@${{ inputs.package_version }} • [GitHub releases](https://github.com/PostHog/posthog-js/releases) • [npm releases](https://www.npmjs.com/package/${{ inputs.package_name }}?activeTab=version)
64+
65+
- name: Output pull request result
66+
shell: bash
67+
run: |
68+
echo "PostHog pull request for ${{ inputs.package_name }} version ${{ inputs.package_version }} ready: ${{ steps.com-repo-pr.outputs.pull-request-url }}"
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
name: "Posthog auto upgrade"
2+
description: "Upgrade a package in main posthog repository"
3+
4+
inputs:
5+
package_name:
6+
description: "package name to upgrade"
7+
required: true
8+
package_version:
9+
description: "package version to upgrade to"
10+
required: true
11+
deployer_app_id:
12+
description: "deployer app id"
13+
required: true
14+
deployer_app_private_key:
15+
description: "deployer app private key"
16+
required: true
17+
github_bot_token:
18+
description: "github bot token"
19+
required: true
20+
21+
runs:
22+
using: "composite"
23+
steps:
24+
- name: Check out main repo
25+
uses: actions/checkout@v2
26+
with:
27+
repository: "PostHog/posthog"
28+
token: ${{ inputs.github_bot_token }}
29+
30+
- uses: pnpm/action-setup@v4
31+
32+
- name: Set up Node.js
33+
uses: actions/setup-node@v3
34+
with:
35+
node-version: 18
36+
cache: pnpm
37+
38+
- name: Install new package version in main repo
39+
id: pnpm-upgrade
40+
shell: bash
41+
run: |
42+
OUTGOING_VERSION=$(jq '.dependencies["${{ inputs.package_name }}"]' package.json -r)
43+
echo "outgoing-version=$OUTGOING_VERSION" >> "$GITHUB_OUTPUT"
44+
for i in $(seq 1 $RETRY_TIMES); do
45+
# Retry loop because of npm being _eventually_ consistent
46+
if pnpm --filter=@posthog/frontend upgrade ${{ inputs.package_name }}@${{ inputs.package_version }}; then
47+
break
48+
else
49+
[ $i -ne $RETRY_TIMES ] && sleep $RETRY_WAIT_SECONDS || false
50+
fi
51+
done
52+
env:
53+
RETRY_TIMES: 20
54+
RETRY_WAIT_SECONDS: 5
55+
56+
- name: Create main repo pull request
57+
id: main-repo-pr
58+
uses: peter-evans/create-pull-request@v3
59+
with:
60+
token: ${{ inputs.github_bot_token }}
61+
commit-message: "chore(deps): Update ${{ inputs.package_name }} to ${{ inputs.package_version }}"
62+
branch: ${{ replace(replace(inputs.package_name, '@', ''), '/', '-') }}-${{ inputs.package_version }}
63+
delete-branch: true
64+
labels: automerge
65+
title: "chore(deps): Update ${{ inputs.package_name }} to ${{ inputs.package_version }}"
66+
body: |
67+
## Changes
68+
69+
${{ inputs.package_name }} version ${{ inputs.package_version }} has been released. This updates PostHog to use it.
70+
71+
https://github.com/PostHog/posthog-js/compare/${{ inputs.package_name }}@${{ steps.pnpm-upgrade.outputs.outgoing-version }}...${{ inputs.package_name }}@${{ inputs.package_version }} • [GitHub releases](https://github.com/PostHog/posthog-js/releases) • [npm releases](https://www.npmjs.com/package/${{ inputs.package_name }}?activeTab=version)
72+
73+
- name: Output pull request result
74+
shell: bash
75+
run: |
76+
echo "PostHog pull request for ${{ inputs.package_name }} version ${{ inputs.package_version }} ready: ${{ steps.main-repo-pr.outputs.pull-request-url }}"
77+
78+
- name: get deployer token
79+
id: deployer
80+
uses: getsentry/action-github-app-token@v2
81+
with:
82+
app_id: ${{ inputs.deployer_app_id }}
83+
private_key: ${{ inputs.deployer_app_private_key }}
84+
85+
- name: Stamp PR
86+
shell: bash
87+
run: |
88+
# unbelievably github has a race condition where if you commit and
89+
# approve too quickly on a PR with "auto-merge" enabled it can miss
90+
# the new commit in the merge commit (but it looks like the PR has the change)
91+
# Sleep 5 should work
92+
sleep 5
93+
pull_number=${{ steps.main-repo-pr.outputs.pull-request-number }}
94+
curl -L \
95+
-X POST \
96+
-H "Accept: application/vnd.github+json" \
97+
-H "Authorization: Bearer ${{ steps.deployer.outputs.token }}" \
98+
-H "X-GitHub-Api-Version: 2022-11-28" \
99+
https://api.github.com/repos/posthog/posthog/pulls/${pull_number}/reviews \
100+
-d '{"body":"${{ inputs.package_name }} auto approved.","event":"APPROVE","comments":[]}'

.github/actions/release/action.yaml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
name: "Release Workflow"
2+
description: "Publish package to NPM and create a GitHub release"
3+
4+
inputs:
5+
package_path:
6+
description: "Package path to publish"
7+
required: true
8+
package_name:
9+
description: "Package name to publish"
10+
required: true
11+
package_version:
12+
description: "Package version to publish"
13+
required: true
14+
npm_token:
15+
description: "NPM token to publish package"
16+
required: true
17+
18+
runs:
19+
using: "composite"
20+
steps:
21+
- name: Checkout the repository
22+
uses: actions/checkout@v4
23+
24+
- name: Setup workspace
25+
uses: ./.github/actions/setup
26+
with:
27+
build: true
28+
29+
- name: Tag repository with package_name and package_version
30+
shell: bash
31+
run: |
32+
git tag -a "${{ inputs.package_name }}@${{ inputs.package_version }}" -m "${{ inputs.package_name }}@${{ inputs.package_version }}"
33+
34+
- name: Publish package to NPM
35+
shell: bash
36+
run: |
37+
pnpm publish --access public
38+
env:
39+
DATABASE_URL: "postgres://postgres:postgres@localhost:${{ job.services.postgres.ports[5432] }}/postgres"
40+
REDIS_URL: "redis://localhost"
41+
NODE_AUTH_TOKEN: ${{ inputs.npm_token }}
42+
43+
- name: Push tag to GitHub
44+
shell: bash
45+
run: |
46+
git push origin "${{ inputs.package_name }}@${{ inputs.package_version }}"
47+
48+
- name: Create GitHub release
49+
working-directory: ${{ inputs.package_path }}
50+
shell: bash
51+
env:
52+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
53+
run: |
54+
# read from the first until the second header in the changelog file
55+
# this assumes the formatting of the file
56+
# and that this workflow is always running for the most recent entry in the file
57+
LAST_CHANGELOG_ENTRY=$(awk -v defText="see CHANGELOG.md" '/^## /{if (flag) exit; flag=1} flag && /^##$/{exit} flag; END{if (!flag) print defText}' CHANGELOG.md)
58+
# the action we used to use was archived, and made it really difficult to create a release with a body
59+
# because the LAST_CHANGELOG_ENTRY contains bash special characters so passing it between steps
60+
# was a pain.
61+
# we can use the github cli to create a release with a body
62+
# all as part of one step
63+
gh api \
64+
--method POST \
65+
-H "Accept: application/vnd.github+json" \
66+
-H "X-GitHub-Api-Version: 2022-11-28" \
67+
/repos/posthog/posthog-js/releases \
68+
-f tag_name="${{ inputs.package_name }}@${{ inputs.package_version }}" \
69+
-f target_commitish='main' \
70+
-f name="${{ inputs.package_name }}@${{ inputs.package_version }}" \
71+
-f body="$LAST_CHANGELOG_ENTRY" \
72+
-F draft=false \
73+
-F prerelease=false \
74+
-F generate_release_notes=false

.github/actions/setup/action.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ inputs:
66
description: "Node.js version to use"
77
required: false
88
default: "20"
9+
install:
10+
description: "Whether to install dependencies"
11+
required: false
12+
default: "true"
13+
build:
14+
description: "Whether to build the project"
15+
required: false
16+
default: "true"
917

1018
runs:
1119
using: "composite"
@@ -33,5 +41,11 @@ runs:
3341
restore-keys: ${{ runner.os }}-pnpm-
3442

3543
- name: Install package.json dependencies with pnpm
44+
if: ${{ inputs.install == 'true' }}
3645
shell: bash
3746
run: pnpm install --frozen-lockfile
47+
48+
- name: Build project with pnpm
49+
if: ${{ inputs.build == 'true' }}
50+
shell: bash
51+
run: pnpm build

0 commit comments

Comments
 (0)