-
Notifications
You must be signed in to change notification settings - Fork 0
91 lines (76 loc) · 3.03 KB
/
push-spec.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
name: Push API Spec to SwaggerHub
on:
workflow_dispatch:
inputs:
api-name:
type: choice
description: Select an API to push spec to SwaggerHub
options:
- products-api
- users-api
required: true
api-version:
description: API Version
required: true
default: 1.0.0
env:
SWAGGERHUB_API_KEY: ${{ secrets.SWAGGERHUB_API_KEY }}
USERNAME: ${{ secrets.SWAGGERHUB_USERNAME }}
jobs:
push-spec:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Install Dependencies
run: |
cd apps/${{ github.event.inputs.api-name }}
npm install
- name: Generate Spec
run: |
cd apps/${{ github.event.inputs.api-name }}
# Create spec generator
cat > generate-spec.ts << 'EOL'
import { NestFactory } from '@nestjs/core';
import { SwaggerModule } from '@nestjs/swagger';
import { writeFileSync } from 'fs';
import { AppModule } from './src/app.module';
import { swaggerConfig } from './src/config/swagger.config';
async function generateSpec() {
let app;
try {
app = await NestFactory.create(AppModule);
const document = SwaggerModule.createDocument(app, swaggerConfig);
writeFileSync('openapi-spec.json', JSON.stringify(document, null, 2));
console.log('✨ OpenAPI spec generated successfully');
} catch (error) {
console.error('Failed to generate OpenAPI spec:',
error instanceof Error ? error.message : error);
process.exit(1);
} finally {
if (app) await app.close();
}
}
generateSpec();
EOL
npx ts-node generate-spec.ts
- name: Install SwaggerHub CLI
run: npm install -g swaggerhub-cli
- name: Push to SwaggerHub
run: |
cd apps/${{ github.event.inputs.api-name }}
API_NAME_WITH_PREFIX="sample-${{ github.event.inputs.api-name }}"
API_NAME_AND_VERSION="${{ github.event.inputs.api-name }} v${{ github.event.inputs.api-version }}"
if swaggerhub api:update ${USERNAME}/${API_NAME_WITH_PREFIX}/${{ github.event.inputs.api-version }} --file openapi-spec.json --visibility public; then
echo "✨ Successfully updated $API_NAME_AND_VERSION on SwaggerHub"
else
echo "Update $API_NAME_AND_VERSION failed, trying to create..."
if swaggerhub api:create ${USERNAME}/${API_NAME_WITH_PREFIX}/${{ github.event.inputs.api-version }} --file openapi-spec.json --visibility public; then
echo "✨ Successfully created $API_NAME_AND_VERSION on SwaggerHub"
else
echo "Failed to push $API_NAME_AND_VERSION to SwaggerHub"
exit 1
fi
fi