Skip to content

Commit f85048d

Browse files
committed
use comment for docs preview URL
1 parent deb623e commit f85048d

File tree

2 files changed

+37
-43
lines changed

2 files changed

+37
-43
lines changed

.github/set_docs_preview_url.py

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import json
21
import os
32
import re
43

@@ -7,8 +6,7 @@
76
DEPLOY_OUTPUT = os.environ['DEPLOY_OUTPUT']
87
GITHUB_TOKEN = os.environ['GITHUB_TOKEN']
98
REPOSITORY = os.environ['REPOSITORY']
10-
REF = os.environ['REF']
11-
ENVIRONMENT = 'deploy-preview'
9+
PULL_REQUEST_NUMBER = os.environ['PULL_REQUEST_NUMBER']
1210

1311
m = re.search(r'https://(\S+)\.workers\.dev', DEPLOY_OUTPUT)
1412
assert m, f'Could not find worker URL in {DEPLOY_OUTPUT!r}'
@@ -19,37 +17,44 @@
1917

2018
version_id = m.group(1)
2119
preview_url = f'https://{version_id}-{worker_name}.workers.dev'
20+
print('Docs preview URL:', preview_url)
2221

2322
gh_headers = {
2423
'Accept': 'application/vnd.github+json',
2524
'Authorization': f'Bearer {GITHUB_TOKEN}',
2625
'X-GitHub-Api-Version': '2022-11-28',
2726
}
2827

29-
deployment_url = f'https://api.github.com/repos/{REPOSITORY}/deployments'
30-
deployment_data = {
31-
'ref': REF,
32-
'task': f'docs preview {version_id}',
33-
'environment': ENVIRONMENT,
34-
'auto_merge': False,
35-
'required_contexts': [],
36-
'payload': json.dumps({
37-
'preview_url': preview_url,
38-
'worker_name': worker_name,
39-
'version_id': version_id,
40-
})
41-
}
42-
r = httpx.post(deployment_url, headers=gh_headers, json=deployment_data)
43-
print(f'POST {deployment_url}: {r.status_code}') # {r.text}
44-
r.raise_for_status()
45-
deployment_id = r.json()['id']
46-
47-
status_url = f'https://api.github.com/repos/{REPOSITORY}/deployments/{deployment_id}/statuses'
48-
status_data = {
49-
'environment': ENVIRONMENT,
50-
'environment_url': preview_url,
51-
'state': 'success',
52-
}
53-
r = httpx.post(status_url, headers=gh_headers, json=status_data)
54-
print(f'POST {status_url}: {r.status_code}') # {r.text}
28+
# now create or update a comment on the PR with the preview URL
29+
30+
comments_url = f'https://api.github.com/repos/{REPOSITORY}/issues/{PULL_REQUEST_NUMBER}/comments'
31+
r = httpx.get(comments_url, headers=gh_headers)
32+
print(f'GET {comments_url} {r.status_code}')
33+
comment_update_url = None
34+
35+
for comment in r.json():
36+
if comment['user']['login'] == 'github-actions[bot]' and comment['body'].startswith('## Docs Preview'):
37+
comment_update_url = comment['url']
38+
break
39+
40+
body = f"""\
41+
## Docs Preview
42+
43+
<table>
44+
<tr>
45+
<td><strong>Preview URL:</strong></td>
46+
<td><a href="{preview_url}">{preview_url}</a></td>
47+
</tr>
48+
</table>
49+
"""
50+
comment_data = {'body': body}
51+
52+
if comment_update_url:
53+
print('Updating existing comment...')
54+
r = httpx.patch(comment_update_url, headers=gh_headers, json=comment_data)
55+
else:
56+
print('Creating new comment...')
57+
r = httpx.post(comments_url, headers=gh_headers, json=comment_data)
58+
59+
print(f'{r.request.method} {comments_url} {r.status_code}')
5560
r.raise_for_status()

.github/workflows/after-ci.yml

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77

88
permissions:
99
statuses: write
10-
deployments: write
10+
pull-requests: write
1111

1212
jobs:
1313
smokeshow:
@@ -51,6 +51,7 @@ jobs:
5151

5252
deploy-docs-preview:
5353
runs-on: ubuntu-latest
54+
if: github.event.workflow_run.event == 'pull_request'
5455
environment:
5556
name: deploy-preview
5657

@@ -75,22 +76,10 @@ jobs:
7576
allow_forks: true
7677
workflow_conclusion: completed
7778

78-
- uses: cloudflare/wrangler-action@v3
79-
id: deploy
80-
with:
81-
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
82-
environment: previews
83-
workingDirectory: docs-site
84-
command: >
85-
deploy
86-
--var GIT_COMMIT_SHA:${{ github.event.workflow_run.head_sha }}
87-
--var GIT_BRANCH:${{ github.event.workflow_run.head_branch }}
88-
89-
# work around for https://github.com/cloudflare/wrangler-action/issues/349
9079
- name: Set preview URL
9180
run: uv run --with httpx .github/set_docs_preview_url.py
9281
env:
9382
DEPLOY_OUTPUT: ${{ steps.deploy.outputs.command-output }}
9483
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
9584
REPOSITORY: ${{ github.repository }}
96-
REF: ${{ github.event.workflow_run.head_sha }}
85+
PULL_REQUEST_NUMBER: ${{ github.event.workflow_run.pull_requests[0].number }}

0 commit comments

Comments
 (0)