Skip to content

Commit 41a9861

Browse files
committed
feat: try out multi-stages workflow
1 parent e809d7a commit 41a9861

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed
File renamed without changes.
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
name: Generate Swagger UI for API specs of data models
2+
3+
env:
4+
JSON_SCHEMA_PATH: ./example_building_automation/schemas
5+
OUTPUT_ROOT: ./example_building_automation
6+
7+
on:
8+
push:
9+
# Adjust branches as needed
10+
branches:
11+
- main
12+
13+
jobs:
14+
set-matrix:
15+
runs-on: ubuntu-latest
16+
concurrency:
17+
group: gh-pages-deployment
18+
cancel-in-progress: false
19+
outputs:
20+
matrix: ${{ steps.build-matrix.outputs.matrix }}
21+
steps:
22+
- name: Checkout repository
23+
uses: actions/checkout@v2
24+
25+
- name: Find API spec files and build matrix
26+
id: build-matrix
27+
run: |
28+
# Find all YAML files under any "api_docs" directory
29+
specs=$(find . -type f -path "*/api_docs/*.yaml")
30+
echo "Found spec files:"
31+
echo "$specs"
32+
33+
# Build a JSON matrix: for each spec, the output directory is under the same api_docs folder.
34+
matrix="[]"
35+
for spec in $specs; do
36+
# Remove the .yaml extension from the file name
37+
base=$(basename "$spec" .yaml)
38+
# Get the directory of the spec file
39+
spec_dir=$(dirname "$spec")
40+
# For each spec, the generated output will reside in: <spec_dir>/swagger-ui/<basename>
41+
output_dir="${spec_dir}/swagger-ui/${base}"
42+
newEntry="{\"spec\":\"${spec}\", \"output\":\"${output_dir}\"}"
43+
if [ "$matrix" = "[]" ]; then
44+
matrix="[$newEntry]"
45+
else
46+
matrix="${matrix%]}"
47+
matrix+=",${newEntry}]"
48+
fi
49+
done
50+
51+
echo "Matrix JSON: $matrix"
52+
# Set the matrix output (using the GITHUB_OUTPUT mechanism)
53+
echo "matrix=$matrix" >> $GITHUB_OUTPUT
54+
55+
generate-swagger-ui:
56+
needs: set-matrix
57+
runs-on: ubuntu-latest
58+
strategy:
59+
# Use the dynamically generated matrix.
60+
# Each job will process one API spec file.
61+
matrix:
62+
include: ${{ fromJson(needs.set-matrix.outputs.matrix) }}
63+
steps:
64+
- name: Checkout repository
65+
uses: actions/checkout@v4
66+
67+
- name: Create output folder if not present
68+
run: mkdir -p "${{ matrix.output }}"
69+
70+
- name: Generate Swagger UI for ${{ matrix.spec }}
71+
uses: Legion2/swagger-ui-action@v1
72+
with:
73+
# The file to process from the matrix.
74+
spec-file: ${{ matrix.spec }}
75+
# The output directory, computed in the matrix (e.g. swagger-ui/ActuatorFiware)
76+
output: ${{ matrix.output }}
77+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
78+
79+
- name: Upload artifact for ${{ matrix.folder }}
80+
uses: actions/upload-artifact@v3
81+
with:
82+
# Each artifact is named after the folder, ensuring uniqueness
83+
name: ${{ matrix.output }}
84+
path: deploy/${{ matrix.output }}
85+
86+
- name: Copy schemas folder if available
87+
run: |
88+
# For a spec like "./module/api_docs/my-api.yaml", the related schemas folder is assumed to be at "./module/schemas"
89+
parent_dir=$(dirname "$(dirname "${{ matrix.spec }}")")
90+
schemas_dir="$parent_dir/schemas"
91+
if [ -d "$schemas_dir" ]; then
92+
echo "Found schemas directory: $schemas_dir"
93+
# Copy the entire schemas folder into the output folder (as a subfolder named "schemas")
94+
mkdir -p "${{ matrix.output }}/schemas"
95+
cp -r "$schemas_dir/"* "${{ matrix.output }}/schemas/"
96+
echo "Copied schemas from $schemas_dir to ${{ matrix.output }}/schemas/"
97+
else
98+
echo "No schemas folder found at $parent_dir/schemas"
99+
fi
100+
101+
# - name: Deploy to GitHub Pages
102+
# uses: peaceiris/actions-gh-pages@v4
103+
# with:
104+
# github_token: ${{ secrets.GITHUB_TOKEN }}
105+
# # Change the publish_dir to the folder you want served.
106+
# # Here we deploy the directory containing the generated swagger-ui (and now also the schemas folder, if available).
107+
# publish_dir: ${{ matrix.output }}
108+
# destination_dir: ${{ matrix.output }}
109+
110+
deploy-gh-pages:
111+
runs-on: ubuntu-latest
112+
needs: generate-swagger-ui
113+
steps:
114+
- name: Checkout repository
115+
uses: actions/checkout@v4
116+
117+
- name: Download all UI artifacts
118+
uses: actions/download-artifact@v3
119+
with:
120+
# This downloads all artifacts (each will be placed in its own folder
121+
# under the "deploy" directory)
122+
path: deploy
123+
124+
# (Optional) In case you want to include JSON data as well:
125+
- name: Copy JSON data
126+
run: |
127+
mkdir -p deploy/data
128+
cp -R data/*.json $OUTPUT_ROOT/swagger-ui
129+
cp -R $JSON_SCHEMA_PATH/*.json $OUTPUT_ROOT/schemas/
130+
131+
- name: Deploy to GitHub Pages
132+
uses: peaceiris/actions-gh-pages@v3
133+
with:
134+
github_token: ${{ secrets.GITHUB_TOKEN }}
135+
publish_dir: ./deploy

0 commit comments

Comments
 (0)