📄 Push API Spec to SwaggerHub #22
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: Set variables | |
id: vars | |
run: | | |
API_NAME=${{ github.event.inputs['api-name'] }} | |
API_VERSION=${{ github.event.inputs['api-version'] }} | |
if [ -z "$API_NAME" ] || [ -z "$API_VERSION" ]; then | |
echo "Error: Required variables not set" | |
exit 1 | |
fi | |
echo "API_NAME=$API_NAME" >> $GITHUB_ENV | |
echo "API_VERSION=$API_VERSION" >> $GITHUB_ENV | |
echo "PACKAGE_NAME=@api-client-sdk-streamline-sample/${API_NAME}-client" >> $GITHUB_ENV | |
echo "Using API: $API_NAME version: $API_VERSION" | |
- name: Install Dependencies | |
run: | | |
cd apps/${{ env.API_NAME }} | |
npm install | |
- name: Generate Spec | |
run: | | |
cd apps/${{ env.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 './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/${{ env.API_NAME }} | |
API_NAME_WITH_PREFIX="sample-${{ env.API_NAME }}" | |
API_NAME_AND_VERSION="${{ env.API_NAME }} v${{ env.API_VERSION }}" | |
if swaggerhub api:update ${USERNAME}/${API_NAME_WITH_PREFIX}/${{ env.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}/${{ env.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 | |