-
Notifications
You must be signed in to change notification settings - Fork 129
239 lines (211 loc) · 10.8 KB
/
cd.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
name: 'CD'
on:
push:
branches:
- main
jobs:
check-package-version:
name: Check package version and detect an update
runs-on: ubuntu-20.04
outputs:
committed-version: ${{ steps.check-package-version.outputs.committed-version }}
published-version: ${{ steps.check-package-version.outputs.published-version }}
is-new-version: ${{ steps.check-package-version.outputs.is-new-version }}
steps:
- name: Checkout the repository
uses: actions/checkout@v2
- name: Check package version and detect an update
id: check-package-version
uses: PostHog/check-package-version@v2
release:
name: Publish release if new version
runs-on: ubuntu-20.04
needs: check-package-version
if: needs.check-package-version.outputs.is-new-version == 'true'
env:
COMMITTED_VERSION: ${{ needs.check-package-version.outputs.committed-version }}
PUBLISHED_VERSION: ${{ needs.check-package-version.outputs.published-version }}
steps:
- name: Checkout the repository
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.POSTHOG_BOT_GITHUB_TOKEN }}
- uses: pnpm/action-setup@v4
with:
version: 8.x.x
- name: Set up Node 18
uses: actions/setup-node@v4
with:
node-version: 18
registry-url: https://registry.npmjs.org
- name: Get pnpm cache directory path
id: pnpm-cache-dir
run: echo "PNPM_STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT
- uses: actions/cache@v3
id: pnpm-cache
with:
path: |
${{ steps.pnpm-cache-dir.outputs.PNPM_STORE_PATH }}
key: ${{ runner.os }}-pnpm-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-
- name: Install package.json dependencies with pnpm
run: pnpm install --frozen-lockfile
- name: Publish the package in the npm registry
run: npm publish --access public
env:
DATABASE_URL: 'postgres://postgres:postgres@localhost:${{ job.services.postgres.ports[5432] }}/postgres'
REDIS_URL: 'redis://localhost'
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Create GitHub release
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# read from the first until the second header in the changelog file
# this assumes the formatting of the file
# and that this workflow is always running for the most recent entry in the file
LAST_CHANGELOG_ENTRY=$(awk -v defText="see CHANGELOG.md" '/^## /{if (flag) exit; flag=1} flag && /^##$/{exit} flag; END{if (!flag) print defText}' CHANGELOG.md)
# the action we used to use was archived, and made it really difficult to create a release with a body
# because the LAST_CHANGELOG_ENTRY contains bash special characters so passing it between steps
# was a pain.
# we can use the github cli to create a release with a body
# all as part of one step
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/posthog/posthog-js/releases \
-f tag_name="v${{ env.COMMITTED_VERSION }}" \
-f target_commitish='main' \
-f name="${{ env.COMMITTED_VERSION }}" \
-f body="$LAST_CHANGELOG_ENTRY" \
-F draft=false \
-F prerelease=false \
-F generate_release_notes=false
create-posthog-main-repo-pull-request:
name: Create main repo PR with new posthog-js version
runs-on: ubuntu-20.04
needs: [check-package-version, release]
env:
COMMITTED_VERSION: ${{ needs.check-package-version.outputs.committed-version }}
PUBLISHED_VERSION: ${{ needs.check-package-version.outputs.published-version }}
steps:
- name: Check out main repo
uses: actions/checkout@v2
with:
repository: 'PostHog/posthog'
token: ${{ secrets.POSTHOG_BOT_GITHUB_TOKEN }}
- name: Install pnpm
uses: pnpm/action-setup@v4
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: 18
cache: pnpm
- name: Install new posthog-js version in main repo
id: pnpm-upgrade
run: |
OUTGOING_VERSION=$(jq '.dependencies["posthog-js"]' package.json -r)
echo "outgoing-version=$OUTGOING_VERSION" >> "$GITHUB_OUTPUT"
for i in $(seq 1 $RETRY_TIMES); do
# Retry loop because of npm being _eventually_ consistent
if pnpm upgrade posthog-js@${{ env.COMMITTED_VERSION }}; then
break
else
[ $i -ne $RETRY_TIMES ] && sleep $RETRY_WAIT_SECONDS || false
fi
done
env:
RETRY_TIMES: 20
RETRY_WAIT_SECONDS: 5
- name: Create main repo pull request
id: main-repo-pr
uses: peter-evans/create-pull-request@v3
with:
token: ${{ secrets.POSTHOG_BOT_GITHUB_TOKEN }}
commit-message: 'chore(deps): Update posthog-js to ${{ env.COMMITTED_VERSION }}'
branch: posthog-js-${{ env.COMMITTED_VERSION }}
delete-branch: true
labels: automerge
title: 'chore(deps): Update posthog-js to ${{ env.COMMITTED_VERSION }}'
body: |
## Changes
posthog-js version ${{ env.COMMITTED_VERSION }} has been released. This updates PostHog to use it.
https://github.com/PostHog/posthog-js/compare/v${{ steps.pnpm-upgrade.outputs.outgoing-version }}...v${{ env.COMMITTED_VERSION }} • [GitHub releases](https://github.com/PostHog/posthog-js/releases) • [npm releases](https://www.npmjs.com/package/posthog-js?activeTab=version)
- name: Output pull request result
run: |
echo "PostHog pull request for posthog-js version ${{ env.COMMITTED_VERSION }} ready: ${{ steps.main-repo-pr.outputs.pull-request-url }}"
- name: get deployer token
id: deployer
uses: getsentry/action-github-app-token@v2
with:
app_id: ${{ secrets.DEPLOYER_APP_ID }}
private_key: ${{ secrets.DEPLOYER_APP_PRIVATE_KEY }}
- name: Stamp PR
run: |
# unbelievably github has a race condition where if you commit and
# approve too quickly on a PR with "auto-merge" enabled it can miss
# the new commit in the merge commit (but it looks like the PR has the change)
# Sleep 5 should work
sleep 5
pull_number=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH")
pull_number=${{ steps.main-repo-pr.outputs.pull-request-number }}
curl -L \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ steps.deployer.outputs.token }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/posthog/posthog/pulls/${pull_number}/reviews \
-d '{"body":"PostHog JS auto approved.","event":"APPROVE","comments":[]}'
create-posthog-com-repo-pull-request:
name: Create posthog.com repo PR with new posthog-js version
runs-on: ubuntu-20.04
needs: [check-package-version, release]
env:
COMMITTED_VERSION: ${{ needs.check-package-version.outputs.committed-version }}
PUBLISHED_VERSION: ${{ needs.check-package-version.outputs.published-version }}
steps:
- name: Check out main repo
uses: actions/checkout@v2
with:
repository: 'PostHog/posthog.com'
token: ${{ secrets.POSTHOG_BOT_GITHUB_TOKEN }}
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: 18
cache: yarn
- name: Install new posthog-js version in posthog.com repo
id: yarn-upgrade
run: |
OUTGOING_VERSION=$(jq '.dependencies["posthog-js"]' package.json -r)
echo "outgoing-version=$OUTGOING_VERSION" >> "$GITHUB_OUTPUT"
for i in $(seq 1 $RETRY_TIMES); do
# Retry loop because of npm being _eventually_ consistent
if yarn upgrade posthog-js@${{ env.COMMITTED_VERSION }}; then
break
else
[ $i -ne $RETRY_TIMES ] && sleep $RETRY_WAIT_SECONDS || false
fi
done
env:
RETRY_TIMES: 20
RETRY_WAIT_SECONDS: 5
- name: Create posthog.com repo pull request
id: com-repo-pr
uses: peter-evans/create-pull-request@v3
with:
token: ${{ secrets.POSTHOG_BOT_GITHUB_TOKEN }}
commit-message: 'chore(deps): Update posthog-js to ${{ env.COMMITTED_VERSION }}'
branch: posthog-js-${{ env.COMMITTED_VERSION }}
delete-branch: true
labels: automerge
title: 'chore(deps): Update posthog-js to ${{ env.COMMITTED_VERSION }}'
body: |
## Changes
posthog-js version ${{ env.COMMITTED_VERSION }} has been released. This updates PostHog to use it.
https://github.com/PostHog/posthog-js/compare/v${{ steps.pnpm-upgrade.outputs.outgoing-version }}...v${{ env.COMMITTED_VERSION }} • [GitHub releases](https://github.com/PostHog/posthog-js/releases) • [npm releases](https://www.npmjs.com/package/posthog-js?activeTab=version)
- name: Output pull request result
run: |
echo "PostHog pull request for posthog-js version ${{ env.COMMITTED_VERSION }} ready: ${{ steps.com-repo-pr.outputs.pull-request-url }}"