Skip to content

Commit ad375dc

Browse files
👷🏻 Show docs deployment status and preview URLs in comment (#1054)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent a20a3a8 commit ad375dc

File tree

3 files changed

+101
-44
lines changed

3 files changed

+101
-44
lines changed

.github/workflows/deploy-docs.yml

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ permissions:
1010
deployments: write
1111
issues: write
1212
pull-requests: write
13+
statuses: write
1314

1415
jobs:
1516
deploy-docs:
@@ -20,6 +21,25 @@ jobs:
2021
GITHUB_CONTEXT: ${{ toJson(github) }}
2122
run: echo "$GITHUB_CONTEXT"
2223
- uses: actions/checkout@v4
24+
- name: Set up Python
25+
uses: actions/setup-python@v5
26+
with:
27+
python-version: "3.11"
28+
- uses: actions/cache@v4
29+
id: cache
30+
with:
31+
path: ${{ env.pythonLocation }}
32+
key: ${{ runner.os }}-python-github-actions-${{ env.pythonLocation }}-${{ hashFiles('requirements-github-actions.txt') }}-v01
33+
- name: Install GitHub Actions dependencies
34+
if: steps.cache.outputs.cache-hit != 'true'
35+
run: pip install -r requirements-github-actions.txt
36+
- name: Deploy Docs Status Pending
37+
run: python ./scripts/deploy_docs_status.py
38+
env:
39+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
40+
COMMIT_SHA: ${{ github.event.workflow_run.head_sha }}
41+
RUN_ID: ${{ github.run_id }}
42+
2343
- name: Clean site
2444
run: |
2545
rm -rf ./site
@@ -43,22 +63,11 @@ jobs:
4363
directory: './site'
4464
gitHubToken: ${{ secrets.GITHUB_TOKEN }}
4565
branch: ${{ ( github.event.workflow_run.head_repository.full_name == github.repository && github.event.workflow_run.head_branch == 'main' && 'main' ) || ( github.event.workflow_run.head_sha ) }}
46-
- name: Set up Python
47-
uses: actions/setup-python@v5
48-
with:
49-
python-version: "3.11"
50-
- uses: actions/cache@v4
51-
id: cache
52-
with:
53-
path: ${{ env.pythonLocation }}
54-
key: ${{ runner.os }}-python-github-actions-${{ env.pythonLocation }}-${{ hashFiles('requirements-github-actions.txt') }}-v01
55-
- name: Install GitHub Actions dependencies
56-
if: steps.cache.outputs.cache-hit != 'true'
57-
run: pip install -r requirements-github-actions.txt
5866
- name: Comment Deploy
5967
if: steps.deploy.outputs.url != ''
60-
run: python ./scripts/comment_docs_deploy_url_in_pr.py
68+
run: python ./scripts/deploy_docs_status.py
6169
env:
6270
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6371
DEPLOY_URL: ${{ steps.deploy.outputs.url }}
6472
COMMIT_SHA: ${{ github.event.workflow_run.head_sha }}
73+
RUN_ID: ${{ github.run_id }}

scripts/comment_docs_deploy_url_in_pr.py

Lines changed: 0 additions & 31 deletions
This file was deleted.

scripts/deploy_docs_status.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import logging
2+
import re
3+
4+
from github import Github
5+
from pydantic import SecretStr
6+
from pydantic_settings import BaseSettings
7+
8+
9+
class Settings(BaseSettings):
10+
github_repository: str
11+
github_token: SecretStr
12+
deploy_url: str | None = None
13+
commit_sha: str
14+
run_id: int
15+
16+
17+
def main():
18+
logging.basicConfig(level=logging.INFO)
19+
settings = Settings()
20+
21+
logging.info(f"Using config: {settings.model_dump_json()}")
22+
g = Github(settings.github_token.get_secret_value())
23+
repo = g.get_repo(settings.github_repository)
24+
use_pr = next(
25+
(pr for pr in repo.get_pulls() if pr.head.sha == settings.commit_sha), None
26+
)
27+
if not use_pr:
28+
logging.error(f"No PR found for hash: {settings.commit_sha}")
29+
return
30+
commits = list(use_pr.get_commits())
31+
current_commit = [c for c in commits if c.sha == settings.commit_sha][0]
32+
run_url = f"https://github.com/{settings.github_repository}/actions/runs/{settings.run_id}"
33+
if not settings.deploy_url:
34+
current_commit.create_status(
35+
state="pending",
36+
description="Deploy Docs",
37+
context="deploy-docs",
38+
target_url=run_url,
39+
)
40+
logging.info("No deploy URL available yet")
41+
return
42+
current_commit.create_status(
43+
state="success",
44+
description="Deploy Docs",
45+
context="deploy-docs",
46+
target_url=run_url,
47+
)
48+
49+
files = list(use_pr.get_files())
50+
docs_files = [f for f in files if f.filename.startswith("docs/")]
51+
52+
deploy_url = settings.deploy_url.rstrip("/")
53+
links: list[str] = []
54+
for f in docs_files:
55+
match = re.match(r"docs/(.*)", f.filename)
56+
assert match
57+
path = match.group(1)
58+
if path.endswith("index.md"):
59+
path = path.replace("index.md", "")
60+
else:
61+
path = path.replace(".md", "/")
62+
link = f"{deploy_url}/{path}"
63+
links.append(link)
64+
links.sort()
65+
66+
message = f"📝 Docs preview for commit {settings.commit_sha} at: {deploy_url}"
67+
68+
if links:
69+
message += "\n\n### Modified Pages\n\n"
70+
message += "\n".join([f"* {link}" for link in links])
71+
72+
print(message)
73+
use_pr.as_issue().create_comment(message)
74+
75+
logging.info("Finished")
76+
77+
78+
if __name__ == "__main__":
79+
main()

0 commit comments

Comments
 (0)