Skip to content

Commit b268c39

Browse files
authored
✨ Add internal GitHub action to deploy docs previews (fastapi#1739)
* 📝 Update release notes * ✨ Add internal GitHub action to pull docs artifact * 🙈 Add archive.zip to gitignore
1 parent 4dd386b commit b268c39

File tree

7 files changed

+95
-2
lines changed

7 files changed

+95
-2
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM python:3.7
2+
3+
RUN pip install httpx "pydantic==1.5.1"
4+
5+
COPY ./app /app
6+
7+
CMD ["python", "/app/main.py"]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: "Deploy Artifact to Netlify"
2+
description: "Get artifact, possibly uploaded by a PR, useful to deploy docs previews"
3+
author: "Sebastián Ramírez <tiangolo@gmail.com>"
4+
inputs:
5+
token:
6+
description: 'Token for the repo. Can be passed in using {{ secrets.GITHUB_TOKEN }}'
7+
required: true
8+
name:
9+
description: 'Artifact name'
10+
required: true
11+
path:
12+
description: 'Where to store the artifact'
13+
required: true
14+
runs:
15+
using: 'docker'
16+
image: 'Dockerfile'
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import logging
2+
from datetime import datetime
3+
from pathlib import Path
4+
from typing import List, Optional
5+
6+
import httpx
7+
from pydantic import BaseModel, BaseSettings, SecretStr
8+
9+
github_api = "https://api.github.com"
10+
netlify_api = "https://api.netlify.com"
11+
12+
13+
class Settings(BaseSettings):
14+
input_name: str
15+
input_token: SecretStr
16+
input_path: str
17+
github_repository: str
18+
github_event_path: Path
19+
github_event_name: Optional[str] = None
20+
21+
22+
class Artifact(BaseModel):
23+
id: int
24+
node_id: str
25+
name: str
26+
size_in_bytes: int
27+
url: str
28+
archive_download_url: str
29+
expired: bool
30+
created_at: datetime
31+
updated_at: datetime
32+
33+
34+
class ArtifactResponse(BaseModel):
35+
total_count: int
36+
artifacts: List[Artifact]
37+
38+
39+
if __name__ == "__main__":
40+
logging.basicConfig(level=logging.INFO)
41+
settings = Settings()
42+
logging.info(f"Using config: {settings.json()}")
43+
github_headers = {
44+
"Authorization": f"token {settings.input_token.get_secret_value()}"
45+
}
46+
response = httpx.get(
47+
f"{github_api}/repos/{settings.github_repository}/actions/artifacts",
48+
headers=github_headers,
49+
)
50+
data = response.json()
51+
artifacts_response = ArtifactResponse.parse_obj(data)
52+
use_artifact: Optional[Artifact] = None
53+
for artifact in artifacts_response.artifacts:
54+
if artifact.name == settings.input_name:
55+
use_artifact = artifact
56+
break
57+
assert use_artifact
58+
file_response = httpx.get(
59+
use_artifact.archive_download_url, headers=github_headers, timeout=30
60+
)
61+
zip_file = Path(settings.input_path)
62+
zip_file.write_bytes(file_response.content)
63+
logging.info("Finished")

.github/workflows/preview-docs.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ jobs:
1313
deploy:
1414
runs-on: ubuntu-18.04
1515
steps:
16-
- uses: actions/download-artifact@v2
16+
- uses: ./.github/actions/get-artifact
1717
with:
18+
token: ${{ secrets.GITHUB_TOKEN }}
1819
name: ${{ github.event.inputs.name }}
19-
path: ./docs.zip
20+
path: ./archive.zip
2021
- name: Unzip docs
2122
run: bash ./scripts/unzip-docs.sh
2223
- name: Deploy to Netlify

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ env
1818
docs_build
1919
venv
2020
docs.zip
21+
archive.zip
2122

2223
# vim temporary files
2324
*~

docs/en/docs/release-notes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Latest changes
44

5+
* Add new GitHub Actions to preview docs from PRs. PR [#1738](https://github.com/tiangolo/fastapi/pull/1738).
56
* Add XML test coverage to support GitHub Actions. PR [#1737](https://github.com/tiangolo/fastapi/pull/1737).
67
* Update badges and remove Travis now that GitHub Actions is the main CI. PR [#1736](https://github.com/tiangolo/fastapi/pull/1736).
78
* Add GitHub Actions for CI, move from Travis. PR [#1735](https://github.com/tiangolo/fastapi/pull/1735).

scripts/unzip-docs.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ set -e
66
if [ -d ./site/ ]; then
77
rm -rf ./site/
88
fi
9+
unzip archive.zip
10+
# Double zipped by GitHub when downlading the archive
911
unzip docs.zip
12+
rm -rf archive.zip
13+
rm -rf docs.zip

0 commit comments

Comments
 (0)