]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
✨ Add internal GitHub action to deploy docs previews (#1739)
authorSebastián Ramírez <tiangolo@gmail.com>
Sun, 19 Jul 2020 20:11:28 +0000 (22:11 +0200)
committerGitHub <noreply@github.com>
Sun, 19 Jul 2020 20:11:28 +0000 (22:11 +0200)
* 📝 Update release notes

* ✨ Add internal GitHub action to pull docs artifact

* 🙈 Add archive.zip to gitignore

.github/actions/get-artifact/Dockerfile [new file with mode: 0644]
.github/actions/get-artifact/action.yml [new file with mode: 0644]
.github/actions/get-artifact/app/main.py [new file with mode: 0644]
.github/workflows/preview-docs.yml
.gitignore
docs/en/docs/release-notes.md
scripts/unzip-docs.sh

diff --git a/.github/actions/get-artifact/Dockerfile b/.github/actions/get-artifact/Dockerfile
new file mode 100644 (file)
index 0000000..1fc6a41
--- /dev/null
@@ -0,0 +1,7 @@
+FROM python:3.7
+
+RUN pip install httpx "pydantic==1.5.1"
+
+COPY ./app /app
+
+CMD ["python", "/app/main.py"]
diff --git a/.github/actions/get-artifact/action.yml b/.github/actions/get-artifact/action.yml
new file mode 100644 (file)
index 0000000..e19b7c9
--- /dev/null
@@ -0,0 +1,16 @@
+name: "Deploy Artifact to Netlify"
+description: "Get artifact, possibly uploaded by a PR, useful to deploy docs previews"
+author: "Sebastián Ramírez <tiangolo@gmail.com>"
+inputs:
+  token:
+    description: 'Token for the repo. Can be passed in using {{ secrets.GITHUB_TOKEN }}'
+    required: true
+  name:
+    description: 'Artifact name'
+    required: true
+  path:
+    description: 'Where to store the artifact'
+    required: true
+runs:
+  using: 'docker'
+  image: 'Dockerfile'
diff --git a/.github/actions/get-artifact/app/main.py b/.github/actions/get-artifact/app/main.py
new file mode 100644 (file)
index 0000000..2ae1ad9
--- /dev/null
@@ -0,0 +1,63 @@
+import logging
+from datetime import datetime
+from pathlib import Path
+from typing import List, Optional
+
+import httpx
+from pydantic import BaseModel, BaseSettings, SecretStr
+
+github_api = "https://api.github.com"
+netlify_api = "https://api.netlify.com"
+
+
+class Settings(BaseSettings):
+    input_name: str
+    input_token: SecretStr
+    input_path: str
+    github_repository: str
+    github_event_path: Path
+    github_event_name: Optional[str] = None
+
+
+class Artifact(BaseModel):
+    id: int
+    node_id: str
+    name: str
+    size_in_bytes: int
+    url: str
+    archive_download_url: str
+    expired: bool
+    created_at: datetime
+    updated_at: datetime
+
+
+class ArtifactResponse(BaseModel):
+    total_count: int
+    artifacts: List[Artifact]
+
+
+if __name__ == "__main__":
+    logging.basicConfig(level=logging.INFO)
+    settings = Settings()
+    logging.info(f"Using config: {settings.json()}")
+    github_headers = {
+        "Authorization": f"token {settings.input_token.get_secret_value()}"
+    }
+    response = httpx.get(
+        f"{github_api}/repos/{settings.github_repository}/actions/artifacts",
+        headers=github_headers,
+    )
+    data = response.json()
+    artifacts_response = ArtifactResponse.parse_obj(data)
+    use_artifact: Optional[Artifact] = None
+    for artifact in artifacts_response.artifacts:
+        if artifact.name == settings.input_name:
+            use_artifact = artifact
+            break
+    assert use_artifact
+    file_response = httpx.get(
+        use_artifact.archive_download_url, headers=github_headers, timeout=30
+    )
+    zip_file = Path(settings.input_path)
+    zip_file.write_bytes(file_response.content)
+    logging.info("Finished")
index 17a03b44842f1fd30c265b7e7e7cee5509e577f1..c46630c61c52859ff06f87f8c6e658c1398d1dce 100644 (file)
@@ -13,10 +13,11 @@ jobs:
   deploy:
     runs-on: ubuntu-18.04
     steps:
-      - uses: actions/download-artifact@v2
+      - uses: ./.github/actions/get-artifact
         with:
+          token: ${{ secrets.GITHUB_TOKEN }}
           name: ${{ github.event.inputs.name }}
-          path: ./docs.zip
+          path: ./archive.zip
       - name: Unzip docs
         run: bash ./scripts/unzip-docs.sh
       - name: Deploy to Netlify
index 186c432f3cdbe47aaae023f96edab3291632f974..a26bb5cd648e719a54d60b1eae8f138e0b9074be 100644 (file)
@@ -18,6 +18,7 @@ env
 docs_build
 venv
 docs.zip
+archive.zip
 
 # vim temporary files
 *~
index 8298c1a1845b87f51269b7bc59ff1c4bf0b6fcb1..1f8a25fe70129294dde3657966b684ae5f67fa7d 100644 (file)
@@ -2,6 +2,7 @@
 
 ## Latest changes
 
+* Add new GitHub Actions to preview docs from PRs. PR [#1738](https://github.com/tiangolo/fastapi/pull/1738).
 * Add XML test coverage to support GitHub Actions. PR [#1737](https://github.com/tiangolo/fastapi/pull/1737).
 * Update badges and remove Travis now that GitHub Actions is the main CI. PR [#1736](https://github.com/tiangolo/fastapi/pull/1736).
 * Add GitHub Actions for CI, move from Travis. PR [#1735](https://github.com/tiangolo/fastapi/pull/1735).
index f022b7deb89201b0ee955060fc480d753fa5b1f9..8e83741462cf2b70587693ec421257b89e9b80ec 100644 (file)
@@ -6,4 +6,8 @@ set -e
 if [ -d ./site/ ]; then
     rm -rf ./site/
 fi
+unzip archive.zip
+# Double zipped by GitHub when downlading the archive
 unzip docs.zip
+rm -rf archive.zip
+rm -rf docs.zip