Skip to content

Commit 9a2fb71

Browse files
committed
change after-ci to use script to set deployment
1 parent 45ed0ec commit 9a2fb71

File tree

2 files changed

+56
-19
lines changed

2 files changed

+56
-19
lines changed

.github/set_docs_preview_url.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import os
2+
import re
3+
4+
import httpx
5+
6+
DEPLOY_OUTPUT = os.environ['DEPLOY_OUTPUT']
7+
GITHUB_TOKEN = os.environ['GITHUB_TOKEN']
8+
REPOSITORY = os.environ['REPOSITORY']
9+
ENVIRONMENT = os.environ['ENVIRONMENT']
10+
11+
m = re.search(r'https://(\S+)\.workers\.dev', DEPLOY_OUTPUT)
12+
assert m, f'Could not find worker URL in {DEPLOY_OUTPUT!r}'
13+
14+
worker_name = m.group(1)
15+
m = re.search(r'Current Version ID: ([^-]+)', DEPLOY_OUTPUT)
16+
assert m, f'Could not find version ID in {DEPLOY_OUTPUT!r}'
17+
18+
version_id = m.group(1)
19+
preview_url = f'https://{version_id}-{worker_name}.workers.dev'
20+
21+
gh_headers = {
22+
'Accept': 'application/vnd.github+json',
23+
'Authorization': f'Bearer {GITHUB_TOKEN}',
24+
'X-GitHub-Api-Version': '2022-11-28',
25+
}
26+
27+
deployment_url = f'https://api.github.com/repos/{REPOSITORY}/deployments'
28+
deployment_data = {
29+
'ref': '9616d25acd9d73e36b8e57032fc6bec7e9bb42f1',
30+
'task': 'docs preview',
31+
'environment': ENVIRONMENT,
32+
}
33+
r = httpx.post(deployment_url, headers=gh_headers, json=deployment_data)
34+
print(f'POST {deployment_url}: {r.status_code} {r.text}')
35+
r.raise_for_status()
36+
deployment_id = r.json()['id']
37+
38+
status_url = f'https://api.github.com/repos/{REPOSITORY}/deployments/{deployment_id}/statuses'
39+
status_data = {
40+
'environment': ENVIRONMENT,
41+
'environment_url': preview_url,
42+
'state': 'success',
43+
}
44+
r = httpx.post(status_url, headers=gh_headers, json=status_data)
45+
print(f'POST {status_url}: {r.status_code} {r.text}')
46+
r.raise_for_status()

.github/workflows/after-ci.yml

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ on:
77

88
permissions:
99
statuses: write
10+
deployments: write
1011

1112
jobs:
1213
smokeshow:
@@ -52,10 +53,13 @@ jobs:
5253
runs-on: ubuntu-latest
5354
environment:
5455
name: deploy-preview
55-
url: ${{ steps.get_preview_url.outputs.preview_url }}
5656

5757
steps:
5858
- uses: actions/checkout@v4
59+
- uses: astral-sh/setup-uv@v5
60+
with:
61+
enable-cache: true
62+
python-version: '3.12'
5963

6064
- uses: dawidd6/action-download-artifact@v6
6165
with:
@@ -66,8 +70,6 @@ jobs:
6670
allow_forks: true
6771
workflow_conclusion: completed
6872

69-
- run: ls -lha
70-
7173
- uses: cloudflare/wrangler-action@v3
7274
id: deploy
7375
with:
@@ -80,21 +82,10 @@ jobs:
8082
--var GIT_BRANCH:${{ github.event.workflow_run.head_branch }}
8183
8284
# work around for https://github.com/cloudflare/wrangler-action/issues/349
83-
- name: get preview URL
84-
id: get_preview_url
85-
shell: python
86-
run: |
87-
import os
88-
import re
89-
deploy_output = os.environ['DEPLOY_OUTPUT']
90-
m = re.search(r'https://(\S+)\.workers\.dev', deploy_output)
91-
assert m, f'Could not find worker URL in {deploy_output!r}'
92-
worker_name = m.group(1)
93-
m = re.search(r'Current Version ID: ([^-]+)', deploy_output)
94-
assert m, f'Could not find version ID in {deploy_output!r}'
95-
version_id = m.group(1)
96-
preview_url = f'https://{version_id}-{worker_name}.workers.dev'
97-
with open(os.environ['GITHUB_OUTPUT'], 'w') as f:
98-
f.write(f'preview_url={preview_url}\n')
85+
- name: Set preview URL
86+
run: uv run --with httpx .github/set_docs_preview_url.py
9987
env:
10088
DEPLOY_OUTPUT: ${{ steps.deploy.outputs.command-output }}
89+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
90+
REPOSITORY: ${{ github.repository }}
91+
ENVIRONMENT: deploy-preview

0 commit comments

Comments
 (0)