Skip to content

Commit 448dd83

Browse files
committed
chore(swagger): preview openapi doc at gh-paes
1 parent ed5f869 commit 448dd83

1 file changed

Lines changed: 164 additions & 0 deletions

File tree

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
name: OpenAPI generation
2+
3+
on:
4+
push:
5+
pull_request:
6+
jobs:
7+
openapi-generate:
8+
runs-on: ubuntu-latest
9+
env:
10+
APP_ENV: testing
11+
APP_DEBUG: true
12+
APP_KEY: base64:4vh0op/S1dAsXKQ2bbdCfWRyCI9r8NNIdPXyZWt9PX4=
13+
DEV_EMAIL_TO: smarcet@gmail.com
14+
APP_URL: http://localhost
15+
DB_CONNECTION: mysql
16+
DB_HOST: 127.0.0.1
17+
DB_PORT: 3306
18+
DB_DATABASE: idp_test
19+
DB_USERNAME: root
20+
DB_PASSWORD: 1qaz2wsx
21+
REDIS_HOST: 127.0.0.1
22+
REDIS_PORT: 6379
23+
REDIS_DB: 0
24+
REDIS_PASSWORD: 1qaz2wsx
25+
REDIS_DATABASES: 16
26+
SSL_ENABLED: false
27+
SESSION_DRIVER: redis
28+
PHP_VERSION: 8.3
29+
OTEL_SDK_DISABLED: true
30+
OTEL_SERVICE_ENABLED: false
31+
32+
services:
33+
mysql:
34+
image: mysql:8.0
35+
env:
36+
MYSQL_ROOT_PASSWORD: ${{env.DB_PASSWORD}}
37+
MYSQL_DATABASE: ${{env.DB_DATABASE}}
38+
ports:
39+
- 3306:3306
40+
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
41+
steps:
42+
- name: Create Redis
43+
uses: supercharge/redis-github-action@1.8.1
44+
with:
45+
redis-port: ${{env.REDIS_PORT}}
46+
redis-password: ${{env.REDIS_PASSWORD}}
47+
48+
- name: Check out repository code
49+
uses: actions/checkout@v4
50+
51+
- name: Change MYSQL sql_mode
52+
run: >
53+
docker exec mysql mysql -u root --password=${{ env.DB_PASSWORD }} -e "SET GLOBAL sql_mode = 'NO_ENGINE_SUBSTITUTION';"
54+
55+
- name: Install PHP
56+
uses: shivammathur/setup-php@v2
57+
with:
58+
php-version: ${{ env.PHP_VERSION }}
59+
extensions: pdo_mysql, mbstring, exif, pcntl, bcmath, sockets, gettext, apcu, redis, igbinary, memcached
60+
61+
- name: Install dependencies
62+
uses: "ramsey/composer-install@v3"
63+
env:
64+
COMPOSER_AUTH: '{"github-oauth": {"github.com": "${{ secrets.PAT }}"} }'
65+
66+
- name: Generate OpenAPI docs
67+
run: php artisan l5-swagger:generate
68+
69+
- name: Build Swagger UI preview
70+
run: |
71+
mkdir -p swagger-ui
72+
cp storage/api-docs/api-docs.json swagger-ui/api-docs.json
73+
cat > swagger-ui/index.html << 'HTMLEOF'
74+
<!doctype html>
75+
<html lang="en">
76+
<head>
77+
<meta charset="utf-8" />
78+
<title>OpenStackID API - Swagger UI</title>
79+
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist/swagger-ui.css" />
80+
</head>
81+
<body>
82+
<div id="swagger-ui"></div>
83+
<script src="https://unpkg.com/swagger-ui-dist/swagger-ui-bundle.js"></script>
84+
<script>
85+
window.onload = () => {
86+
window.ui = SwaggerUIBundle({
87+
url: 'api-docs.json',
88+
dom_id: '#swagger-ui'
89+
});
90+
};
91+
</script>
92+
</body>
93+
</html>
94+
HTMLEOF
95+
96+
- name: Upload Swagger UI artifact
97+
uses: actions/upload-artifact@v4
98+
with:
99+
name: swagger-ui
100+
path: swagger-ui/
101+
if-no-files-found: error
102+
103+
pages-preview:
104+
name: Publish Swagger UI to GitHub Pages
105+
needs: openapi-generate
106+
if: github.event_name == 'pull_request'
107+
runs-on: ubuntu-latest
108+
109+
permissions:
110+
contents: write
111+
issues: write
112+
pull-requests: write
113+
114+
steps:
115+
- name: Checkout repository
116+
uses: actions/checkout@v4
117+
118+
- name: Download Swagger UI artifact
119+
uses: actions/download-artifact@v4
120+
with:
121+
name: swagger-ui
122+
path: swagger-ui
123+
124+
- name: Build public directory for GitHub Pages
125+
run: |
126+
PR_PATH="openapi/pr-${{ github.event.number }}"
127+
mkdir -p "public/${PR_PATH}"
128+
cp -R swagger-ui/* "public/${PR_PATH}/"
129+
echo "Built GitHub Pages content at public/${PR_PATH}"
130+
131+
- name: Publish to GitHub Pages
132+
uses: peaceiris/actions-gh-pages@v4
133+
with:
134+
github_token: ${{ secrets.GITHUB_TOKEN }}
135+
publish_dir: ./public
136+
keep_files: true
137+
138+
- name: Comment preview URL on PR
139+
uses: actions/github-script@v7
140+
with:
141+
github-token: ${{ secrets.GITHUB_TOKEN }}
142+
script: |
143+
const prNumber = context.payload.pull_request.number;
144+
const owner = context.repo.owner;
145+
const repo = context.repo.repo;
146+
const baseUrl = `https://${owner}.github.io/${repo}`;
147+
const previewPath = `/openapi/pr-${prNumber}/`;
148+
const url = `${baseUrl}${previewPath}`;
149+
150+
const body = [
151+
'📘 **OpenAPI / Swagger preview**',
152+
'',
153+
`➡️ ${url}`,
154+
'',
155+
'This page is automatically updated on each push to this PR.'
156+
].join('\n');
157+
158+
await github.rest.issues.createComment({
159+
owner,
160+
repo,
161+
issue_number: prNumber,
162+
body,
163+
});
164+

0 commit comments

Comments
 (0)