Skip to content

Commit 7f04332

Browse files
authored
Add deploy-docs-preview (#1498)
1 parent a4064f9 commit 7f04332

File tree

6 files changed

+113
-11
lines changed

6 files changed

+113
-11
lines changed

.github/set_docs_main_preview_url.py

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

.github/workflows/after-ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ jobs:
9292
GITHUB_EVENT_JSON: ${{ toJSON(github.event) }}
9393

9494
- name: Set preview URL
95-
run: uv run --with httpx .github/set_docs_preview_url.py
95+
run: uv run --with httpx .github/set_docs_pr_preview_url.py
9696
env:
9797
DEPLOY_OUTPUT: ${{ steps.deploy.outputs.command-output }}
9898
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/ci.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,48 @@ jobs:
311311
env:
312312
ALGOLIA_WRITE_API_KEY: ${{ secrets.ALGOLIA_WRITE_API_KEY }}
313313

314+
deploy-docs-preview:
315+
needs: [check]
316+
if: success() && github.ref == 'refs/heads/main'
317+
runs-on: ubuntu-latest
318+
environment:
319+
name: deploy-docs-preview
320+
321+
steps:
322+
- uses: actions/checkout@v4
323+
324+
- uses: actions/setup-node@v4
325+
- run: npm install
326+
working-directory: docs-site
327+
328+
- uses: astral-sh/setup-uv@v5
329+
with:
330+
enable-cache: true
331+
332+
- uses: actions/download-artifact@v4
333+
with:
334+
name: site
335+
path: site
336+
337+
- uses: cloudflare/wrangler-action@v3
338+
id: deploy
339+
with:
340+
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
341+
environment: previews
342+
workingDirectory: docs-site
343+
command: >
344+
deploy
345+
--var GIT_COMMIT_SHA:${{ github.sha }}
346+
--var GIT_BRANCH:main
347+
348+
- name: Set preview URL
349+
run: uv run --with httpx .github/set_docs_main_preview_url.py
350+
env:
351+
DEPLOY_OUTPUT: ${{ steps.deploy.outputs.command-output }}
352+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
353+
REPOSITORY: ${{ github.repository }}
354+
REF: ${{ github.event.workflow_run.head_sha }}
355+
314356
# TODO(Marcelo): We need to split this into two jobs: `build` and `release`.
315357
release:
316358
needs: [check]

docs-site/src/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,24 @@ async function getChangelog(kv: KVNamespace, commitSha: string): Promise<string>
6666

6767
interface Release {
6868
name: string
69+
tag_name: string
6970
body: string
7071
html_url: string
7172
}
7273

74+
const githubIcon = `<span class="twemoji"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12"></path></svg></span>`
75+
7376
function prepRelease(release: Release): string {
7477
const body = release.body
7578
.replace(/(#+)/g, (m) => `##${m}`)
7679
.replace(/https:\/\/github.com\/pydantic\/pydantic-ai\/pull\/(\d+)/g, (url, id) => `[#${id}](${url})`)
77-
.replace(/\*\*Full Changelog\*\*: (\S+)/, (_, url) => `[Compare diff](${url})`)
80+
.replace(/(\s)@([\w\-]+)/g, (_, s, u) => `${s}[@${u}](https://github.com/${u})`)
81+
.replace(/\*\*Full Changelog\*\*: (\S+)/, (_, url) => `[${githubIcon} Compare diff](${url}).`)
7882
return `
7983
### ${release.name}
8084
8185
${body}
8286
83-
[View on GitHub](${release.html_url})
87+
[${githubIcon} View ${release.tag_name} release](${release.html_url}).
8488
`
8589
}

docs/changelog.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
PydanticAI is still pre-version 1, so breaking changes will occur, however:
44

55
- We try to minimize them as much as possible.
6-
- We will use minor version bumps to signify breaking changes.
7-
- Wherever possible we'll deprecate old features so code continues to work with deprecation warnings when changing the public API.
8-
- We intend to release V1 in summer 2025, and then follow strict semantic versioning, e.g. no intentional breaking changes except in major versions.
6+
- We use minor version bumps to signify breaking changes.
7+
- Wherever possible we deprecate old features so code continues to work with deprecation warnings when changing the public API.
8+
- We intend to release V1 in summer 2025, and then follow strict semantic versioning, e.g. no intentional breaking changes except in minor or patch versions.
99

1010
## Breaking Changes
1111

@@ -27,11 +27,11 @@ See [#1484](https://github.com/pydantic/pydantic-ai/pull/1484) — `format_as_xm
2727
</div>
2828

2929
<script>
30-
async function getChangelog() {
31-
const r = await fetch('/changelog.html');
30+
fetch('/changelog.html').then(r => {
3231
if (r.ok) {
33-
document.getElementById('display-changelog').innerHTML = await r.text();
32+
r.text().then(t => {
33+
document.getElementById('display-changelog').innerHTML = t;
34+
});
3435
}
35-
}
36-
getChangelog();
36+
});
3737
</script>

0 commit comments

Comments
 (0)