👷🏻 Show docs deployment status and preview URLs in comment (#910)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sebastián Ramírez · committed by GitHub · parent 142422a · Browse files · 3 files·+101 added−44 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+101 added−44 removed
- other:+101 added−44 removed
Provenance
- Commit
6c25973- Tree
263055e- Author
Sebastián Ramírez <tiangolo@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
142422a
3 files changed+101 added−44 removed
modified:.github/workflows/deploy-docs.yml+22 added-13 removed
8 unmodified lines910111213141516172 unmodified lines20212223242526272829303132333435363738394041424344454615 unmodified lines626364654647484950515253545556576667606869707172738 unmodified linespermissions: deployments: write issues: write pull-requests: write statuses: write
jobs: deploy-docs: runs-on: ubuntu-latest2 unmodified lines env: GITHUB_CONTEXT: ${{ toJson(github) }} run: echo "$GITHUB_CONTEXT" - uses: actions/checkout@v4 - name: Set up Python uses: actions/setup-python@v5 with: python-version: "3.11" - uses: actions/cache@v4 id: cache with: path: ${{ env.pythonLocation }} key: ${{ runner.os }}-python-github-actions-${{ env.pythonLocation }}-${{ hashFiles('requirements-github-actions.txt') }}-v01 - name: Install GitHub Actions dependencies if: steps.cache.outputs.cache-hit != 'true' run: pip install -r requirements-github-actions.txt - name: Deploy Docs Status Pending run: python ./scripts/deploy_docs_status.py env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} COMMIT_SHA: ${{ github.event.workflow_run.head_sha }} RUN_ID: ${{ github.run_id }}
- name: Clean site run: | rm -rf ./site mkdir ./site15 unmodified lines projectName: typertiangolo directory: './site' gitHubToken: ${{ secrets.GITHUB_TOKEN }} branch: ${{ ( github.event.workflow_run.head_repository.full_name == github.repository && github.event.workflow_run.head_branch == 'master' && 'main' ) || ( github.event.workflow_run.head_sha ) }} - name: Set up Python uses: actions/setup-python@v5 with: python-version: "3.11" - uses: actions/cache@v4 id: cache with: path: ${{ env.pythonLocation }} key: ${{ runner.os }}-python-github-actions-${{ env.pythonLocation }}-${{ hashFiles('requirements-github-actions.txt') }}-v01 - name: Install GitHub Actions dependencies if: steps.cache.outputs.cache-hit != 'true' run: pip install -r requirements-github-actions.txt - name: Comment Deploy if: steps.deploy.outputs.url != '' run: python ./scripts/comment_docs_deploy_url_in_pr.py run: python ./scripts/deploy_docs_status.py env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} DEPLOY_URL: ${{ steps.deploy.outputs.url }} COMMIT_SHA: ${{ github.event.workflow_run.head_sha }} RUN_ID: ${{ github.run_id }}deleted:scripts/comment_docs_deploy_url_in_pr.py-31 removed
1-import logging2-import sys3-4-from github import Github5-from pydantic import SecretStr6-from pydantic_settings import BaseSettings7-8-9-class Settings(BaseSettings):10- github_repository: str11- github_token: SecretStr12- deploy_url: str13- commit_sha: str14-15-16-if __name__ == "__main__":17- logging.basicConfig(level=logging.INFO)18- settings = Settings()19- logging.info(f"Using config: {settings.model_dump_json()}")20- g = Github(settings.github_token.get_secret_value())21- repo = g.get_repo(settings.github_repository)22- use_pr = next(23- (pr for pr in repo.get_pulls() if pr.head.sha == settings.commit_sha), None24- )25- if not use_pr:26- logging.error(f"No PR found for hash: {settings.commit_sha}")27- sys.exit(0)28- use_pr.as_issue().create_comment(29- f"📝 Docs preview for commit {settings.commit_sha} at: {settings.deploy_url}"30- )31- logging.info("Finished")added:scripts/deploy_docs_status.py+79 added
1+import logging2+import re3+4+from github import Github5+from pydantic import SecretStr6+from pydantic_settings import BaseSettings7+8+9+class Settings(BaseSettings):10+ github_repository: str11+ github_token: SecretStr12+ deploy_url: str | None = None13+ commit_sha: str14+ run_id: int15+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), None26+ )27+ if not use_pr:28+ logging.error(f"No PR found for hash: {settings.commit_sha}")29+ return30+ 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+ return42+ 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 match57+ 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()