-
Notifications
You must be signed in to change notification settings - Fork 3
92 lines (80 loc) · 2.85 KB
/
openapi_build.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
name: Build OpenAPI JSON
on:
workflow_call:
inputs:
image:
description: "The image to build to OpenAPI JSON (dependencies included, i.e. FastAPI.)."
required: true
type: string
example_env_file_path:
description: "FastAPI must start with an environment set. Path to a .env with dummy vars."
required: true
type: string
output_path:
description: "If specified, the output dir is uploaded to the `artifact` key."
required: false
type: string
jobs:
build_openapi_json:
name: Build OpenAPI JSON
runs-on: ubuntu-latest
container:
image: ${{ inputs.image }}
options: --user root
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Create .env file
env:
EXAMPLE_DOTENV: ${{ inputs.example_env_file_path }}
run: |
echo "Checking if ${EXAMPLE_DOTENV} exists"
if [ -f ${EXAMPLE_DOTENV} ]; then
# Get a8m/envsubst (required for default vals syntax ${VAR:-default})
echo "Downloading envsubst"
curl -L https://github.com/a8m/envsubst/releases/download/v1.2.0/envsubst-`uname -s`-`uname -m` -o envsubst
if [ $? -ne 0 ]; then
echo "Failed to download envsubst"
exit 1
fi
chmod +x envsubst
echo "Substituting variables from ${EXAMPLE_DOTENV} --> .env"
./envsubst < "${EXAMPLE_DOTENV}" > .env
else
echo "${EXAMPLE_DOTENV} not found, creating empty .env"
touch .env
fi
- name: Build OpenAPI JSON
env:
DOTENV_FILE: .env
run: |
python - <<EOF
"""Get OpenAPI config from FastAPI and write to file."""
import os
import json
from pathlib import Path
from fastapi.openapi.utils import get_openapi
from dotenv import load_dotenv
load_dotenv(Path(os.getenv("DOTENV_FILE")))
from app.main import api # noqa: E402
with open(Path("docs/openapi.json"), "w") as f:
openapi = get_openapi(
title=api.title,
version=api.version,
openapi_version=api.openapi_version,
description=api.description,
routes=api.routes,
tags=api.openapi_tags,
servers=api.servers,
terms_of_service=api.terms_of_service,
contact=api.contact,
license_info=api.license_info,
)
json.dump(openapi, f, separators=(",", ":"))
EOF
- name: Upload Artifact Dir
if: ${{ inputs.output_path }}
uses: actions/upload-artifact@v4
with:
path: ${{ inputs.output_path }}
name: openapi