✨ Add support for previewing docs in PRs from forks and enable MkDocs Insiders (#357)
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 ac0e1bb · Browse files · 8 files·+216 added−29 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+216 added−29 removed
- other:+216 added−29 removed
Provenance
- Commit
338d7e3- Tree
7bd9c08- Author
Sebastián Ramírez <tiangolo@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
ac0e1bb
8 files changed+216 added−29 removed
added:.github/actions/comment-docs-preview-in-pr/app/main.py+70 added
1+import logging2+import sys3+from pathlib import Path4+from typing import Optional5+6+import httpx7+from github import Github8+from github.PullRequest import PullRequest9+from pydantic import BaseModel, BaseSettings, SecretStr, ValidationError10+11+github_api = "https://api.github.com"12+13+14+class Settings(BaseSettings):15+ github_repository: str16+ github_event_path: Path17+ github_event_name: Optional[str] = None18+ input_token: SecretStr19+ input_deploy_url: str20+21+22+class PartialGithubEventHeadCommit(BaseModel):23+ id: str24+25+26+class PartialGithubEventWorkflowRun(BaseModel):27+ head_commit: PartialGithubEventHeadCommit28+29+30+class PartialGithubEvent(BaseModel):31+ workflow_run: PartialGithubEventWorkflowRun32+33+34+if __name__ == "__main__":35+ logging.basicConfig(level=logging.INFO)36+ settings = Settings()37+ logging.info(f"Using config: {settings.json()}")38+ g = Github(settings.input_token.get_secret_value())39+ repo = g.get_repo(settings.github_repository)40+ try:41+ event = PartialGithubEvent.parse_file(settings.github_event_path)42+ except ValidationError as e:43+ logging.error(f"Error parsing event file: {e.errors()}")44+ sys.exit(0)45+ use_pr: Optional[PullRequest] = None46+ for pr in repo.get_pulls():47+ if pr.head.sha == event.workflow_run.head_commit.id:48+ use_pr = pr49+ break50+ if not use_pr:51+ logging.error(52+ f"No PR found for hash: {event.workflow_run.head_commit.id}"53+ )54+ sys.exit(0)55+ github_headers = {56+ "Authorization": f"token {settings.input_token.get_secret_value()}"57+ }58+ url = f"{github_api}/repos/{settings.github_repository}/issues/{use_pr.number}/comments"59+ logging.info(f"Using comments URL: {url}")60+ response = httpx.post(61+ url,62+ headers=github_headers,63+ json={64+ "body": f"📝 Docs preview for commit {use_pr.head.sha} at: {settings.input_deploy_url}"65+ },66+ )67+ if not (200 <= response.status_code <= 300):68+ logging.error(f"Error posting comment: {response.text}")69+ sys.exit(1)70+ logging.info("Finished")added:.github/actions/comment-docs-preview-in-pr/action.yml+13 added
12345678910111213name: Comment Docs Preview in PRdescription: Comment with the docs URL preview in the PRauthor: Sebastián Ramírez <tiangolo@gmail.com>inputs: token: description: Token for the repo. Can be passed in using {{ secrets.GITHUB_TOKEN }} required: true deploy_url: description: The deployment URL to comment in the PR required: trueruns: using: docker image: Dockerfileadded:.github/actions/comment-docs-preview-in-pr/Dockerfile+7 added
1+FROM python:3.72+3+RUN pip install httpx "pydantic==1.5.1" pygithub4+5+COPY ./app /app6+7+CMD ["python", "/app/main.py"]added:.github/workflows/build-docs.yml+72 added
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172name: Build Docson: push: branches: - master pull_request: types: [opened, synchronize] workflow_dispatch: inputs: debug_enabled: description: 'Run the build with tmate debugging enabled (https://github.com/marketplace/actions/debugging-with-tmate)' required: false default: falsejobs: build-docs: runs-on: ubuntu-20.04 steps: - name: Dump GitHub context env: GITHUB_CONTEXT: ${{ toJson(github) }} run: echo "$GITHUB_CONTEXT" - uses: actions/checkout@v2 - name: Set up Python uses: actions/setup-python@v2 with: python-version: "3.7" # Allow debugging with tmate - name: Setup tmate session uses: mxschmitt/action-tmate@v3 if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.debug_enabled }} with: limit-access-to-actor: true - uses: actions/cache@v2 id: cache with: path: ${{ env.pythonLocation }} key: ${{ runner.os }}-python-${{ env.pythonLocation }}-${{ hashFiles('pyproject.toml') }}-docs - name: Install Flit if: steps.cache.outputs.cache-hit != 'true' run: python3.7 -m pip install flit - name: Install docs extras if: steps.cache.outputs.cache-hit != 'true' run: python3.7 -m flit install --extras doc - name: Install Material for MkDocs Insiders if: github.event.pull_request.head.repo.fork == false && steps.cache.outputs.cache-hit != 'true' run: python3.7 -m pip install git+https://${{ secrets.ACTIONS_TOKEN }}@github.com/squidfunk/mkdocs-material-insiders.git - uses: actions/cache@v2 with: key: mkdocs-cards-${{ github.ref }} path: .cache - name: Build Docs if: github.event.pull_request.head.repo.fork == true run: python3.7 -m mkdocs build - name: Build Docs with Insiders if: github.event.pull_request.head.repo.fork == false run: python3.7 -m mkdocs build --config-file mkdocs.insiders.yml - name: Zip docs run: bash ./scripts/zip-docs.sh - uses: actions/upload-artifact@v2 with: name: docs-zip path: ./docs.zip - name: Deploy to Netlify uses: nwtgck/actions-netlify@v1.1.5 with: publish-dir: './site' production-branch: master github-token: ${{ secrets.GITHUB_TOKEN }} enable-commit-comment: false env: NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }} NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}deleted:.github/workflows/deploy-docs.yml-29 removed
1234567891011121314151617181920212223242526272829name: Build and Deploy to Netlifyon: push: pull_request: types: [opened, synchronize]jobs: build-docs: runs-on: ubuntu-18.04 steps: - uses: actions/checkout@v2 - name: Set up Python uses: actions/setup-python@v1 with: python-version: "3.7" - name: Install Flit run: python3.7 -m pip install flit - name: Install docs extras run: python3.7 -m flit install --extras doc - name: Build MkDocs run: python3.7 -m mkdocs build - name: Deploy to Netlify uses: nwtgck/actions-netlify@v1.0.3 with: publish-dir: './site' production-branch: master github-token: ${{ secrets.GITHUB_TOKEN }} env: NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }} NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}added:.github/workflows/preview-docs.yml+41 added
1234567891011121314151617181920212223242526272829303132333435363738394041name: Preview Docson: workflow_run: workflows: - Build Docs types: - completed
jobs: preview-docs: runs-on: ubuntu-20.04 steps: - uses: actions/checkout@v2 - name: Download Artifact Docs uses: dawidd6/action-download-artifact@v2.9.0 with: github_token: ${{ secrets.GITHUB_TOKEN }} workflow: build-docs.yml run_id: ${{ github.event.workflow_run.id }} name: docs-zip - name: Unzip docs run: | rm -rf ./site unzip docs.zip rm -f docs.zip - name: Deploy to Netlify id: netlify uses: nwtgck/actions-netlify@v1.1.5 with: publish-dir: './site' production-deploy: false github-token: ${{ secrets.GITHUB_TOKEN }} enable-commit-comment: false env: NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }} NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }} - name: Comment Deploy uses: ./.github/actions/comment-docs-preview-in-pr with: token: ${{ secrets.GITHUB_TOKEN }} deploy_url: "${{ steps.netlify.outputs.deploy-url }}"added:scripts/zip-docs.sh+9 added
123456789#! /usr/bin/env bash
set -xset -e
if [ -f docs.zip ]; then rm -rf docs.zipfizip -r docs.zip ./siteadded:mkdocs.insiders.yml+4 added
1234INHERIT: mkdocs.ymlplugins: - search - social