From fcfbb8e45c40e93e97866b866a9074610323cf01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Sun, 13 Feb 2022 17:52:15 +0100 Subject: [PATCH 1/4] =?UTF-8?q?=F0=9F=94=A7=20Add=20MkDocs=20insiders=20co?= =?UTF-8?q?nfig?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mkdocs.insiders.yml | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 mkdocs.insiders.yml diff --git a/mkdocs.insiders.yml b/mkdocs.insiders.yml new file mode 100644 index 0000000000..9f2775ff97 --- /dev/null +++ b/mkdocs.insiders.yml @@ -0,0 +1,4 @@ +INHERIT: mkdocs.yml +plugins: + - search + - social From d93456fba19f19c2ab2bd94dc3eda34011fe1606 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Sun, 13 Feb 2022 17:52:47 +0100 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=91=B7=20Update=20GitHub=20Actions=20?= =?UTF-8?q?to=20build=20and=20preview=20docs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build-docs.yml | 72 ++++++++++++++++++++++++++++++ .github/workflows/deploy-docs.yml | 29 ------------ .github/workflows/preview-docs.yml | 41 +++++++++++++++++ 3 files changed, 113 insertions(+), 29 deletions(-) create mode 100644 .github/workflows/build-docs.yml delete mode 100644 .github/workflows/deploy-docs.yml create mode 100644 .github/workflows/preview-docs.yml diff --git a/.github/workflows/build-docs.yml b/.github/workflows/build-docs.yml new file mode 100644 index 0000000000..1fd1bae68d --- /dev/null +++ b/.github/workflows/build-docs.yml @@ -0,0 +1,72 @@ +name: Build Docs +on: + 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: false +jobs: + 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 }} diff --git a/.github/workflows/deploy-docs.yml b/.github/workflows/deploy-docs.yml deleted file mode 100644 index f0d029040c..0000000000 --- a/.github/workflows/deploy-docs.yml +++ /dev/null @@ -1,29 +0,0 @@ -name: Build and Deploy to Netlify -on: - 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 }} diff --git a/.github/workflows/preview-docs.yml b/.github/workflows/preview-docs.yml new file mode 100644 index 0000000000..e335e81f91 --- /dev/null +++ b/.github/workflows/preview-docs.yml @@ -0,0 +1,41 @@ +name: Preview Docs +on: + 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 }}" From aa07148a0b8d669812c9c0d084a42b14be1cf9f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Sun, 13 Feb 2022 17:53:40 +0100 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=91=B7=20Add=20custom=20GitHub=20Acti?= =?UTF-8?q?on=20to=20comment=20preview=20docs=20in=20PRs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment-docs-preview-in-pr/Dockerfile | 7 ++ .../comment-docs-preview-in-pr/action.yml | 13 ++++ .../comment-docs-preview-in-pr/app/main.py | 70 +++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 .github/actions/comment-docs-preview-in-pr/Dockerfile create mode 100644 .github/actions/comment-docs-preview-in-pr/action.yml create mode 100644 .github/actions/comment-docs-preview-in-pr/app/main.py diff --git a/.github/actions/comment-docs-preview-in-pr/Dockerfile b/.github/actions/comment-docs-preview-in-pr/Dockerfile new file mode 100644 index 0000000000..4f20c5f10b --- /dev/null +++ b/.github/actions/comment-docs-preview-in-pr/Dockerfile @@ -0,0 +1,7 @@ +FROM python:3.7 + +RUN pip install httpx "pydantic==1.5.1" pygithub + +COPY ./app /app + +CMD ["python", "/app/main.py"] diff --git a/.github/actions/comment-docs-preview-in-pr/action.yml b/.github/actions/comment-docs-preview-in-pr/action.yml new file mode 100644 index 0000000000..0eb64402d2 --- /dev/null +++ b/.github/actions/comment-docs-preview-in-pr/action.yml @@ -0,0 +1,13 @@ +name: Comment Docs Preview in PR +description: Comment with the docs URL preview in the PR +author: Sebastián Ramírez +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: true +runs: + using: docker + image: Dockerfile diff --git a/.github/actions/comment-docs-preview-in-pr/app/main.py b/.github/actions/comment-docs-preview-in-pr/app/main.py new file mode 100644 index 0000000000..3b10e0ee08 --- /dev/null +++ b/.github/actions/comment-docs-preview-in-pr/app/main.py @@ -0,0 +1,70 @@ +import logging +import sys +from pathlib import Path +from typing import Optional + +import httpx +from github import Github +from github.PullRequest import PullRequest +from pydantic import BaseModel, BaseSettings, SecretStr, ValidationError + +github_api = "https://api.github.com" + + +class Settings(BaseSettings): + github_repository: str + github_event_path: Path + github_event_name: Optional[str] = None + input_token: SecretStr + input_deploy_url: str + + +class PartialGithubEventHeadCommit(BaseModel): + id: str + + +class PartialGithubEventWorkflowRun(BaseModel): + head_commit: PartialGithubEventHeadCommit + + +class PartialGithubEvent(BaseModel): + workflow_run: PartialGithubEventWorkflowRun + + +if __name__ == "__main__": + logging.basicConfig(level=logging.INFO) + settings = Settings() + logging.info(f"Using config: {settings.json()}") + g = Github(settings.input_token.get_secret_value()) + repo = g.get_repo(settings.github_repository) + try: + event = PartialGithubEvent.parse_file(settings.github_event_path) + except ValidationError as e: + logging.error(f"Error parsing event file: {e.errors()}") + sys.exit(0) + use_pr: Optional[PullRequest] = None + for pr in repo.get_pulls(): + if pr.head.sha == event.workflow_run.head_commit.id: + use_pr = pr + break + if not use_pr: + logging.error( + f"No PR found for hash: {event.workflow_run.head_commit.id}" + ) + sys.exit(0) + github_headers = { + "Authorization": f"token {settings.input_token.get_secret_value()}" + } + url = f"{github_api}/repos/{settings.github_repository}/issues/{use_pr.number}/comments" + logging.info(f"Using comments URL: {url}") + response = httpx.post( + url, + headers=github_headers, + json={ + "body": f"📝 Docs preview for commit {use_pr.head.sha} at: {settings.input_deploy_url}" + }, + ) + if not (200 <= response.status_code <= 300): + logging.error(f"Error posting comment: {response.text}") + sys.exit(1) + logging.info("Finished") From 9877001281f9436cf0c3aae02855601f1278b116 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Sun, 13 Feb 2022 17:57:07 +0100 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=94=A7=20Add=20zip-docs.sh=20script?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/zip-docs.sh | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 scripts/zip-docs.sh diff --git a/scripts/zip-docs.sh b/scripts/zip-docs.sh new file mode 100644 index 0000000000..f2b7ba3be3 --- /dev/null +++ b/scripts/zip-docs.sh @@ -0,0 +1,9 @@ +#! /usr/bin/env bash + +set -x +set -e + +if [ -f docs.zip ]; then + rm -rf docs.zip +fi +zip -r docs.zip ./site