Skip to content

Commit

Permalink
ci/swagger docs (#9)
Browse files Browse the repository at this point in the history
* ci: generate swagger file

* ci: upload file on Swaggerhub

* fix: organization name correction
  • Loading branch information
a20688392 authored Jul 8, 2023
1 parent 2921cba commit 35d1f22
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
54 changes: 54 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,57 @@ jobs:

- name: Unit Test
run: yarn test

generate-swagger:
runs-on: ubuntu-latest
env:
SWAGGERHUB_API_KEY: ${{ secrets.SWAGGERHUB_API_KEY }}
steps:
- name: Checkout Code
uses: actions/checkout@v3

- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "dir=$(yarn config get cacheFolder)" >> $GITHUB_OUTPUT

- uses: actions/cache@v3
id: yarn-cache
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Setup node and cache dependencies
uses: actions/setup-node@v3
with:
node-version: 16
cache: "yarn"

- name: Install Dependencies
run: yarn install

- name: Nest Build
run: yarn build

- name: Install swaggerhub-cli
run: yarn global add swaggerhub-cli

- name: Read package version
uses: tyankatsu0105/read-package-version-actions@v1
id: package-version

- name: Generate Swagger JSON
run: yarn generate-swagger
env:
APP_SWAGGER_Version: ${{ steps.package-version.outputs.version }}

- name: Upload on Swaggerhub - Cophr_test
if: github.ref != 'refs/heads/main'
run: |
swaggerhub api:create "Cophr/test/${GITHUB_SHA::8}" --setdefault --file=swagger-docs.json --visibility=public
- name: Upload on Swaggerhub - Cophr_main
if: github.ref == 'refs/heads/main'
run: |
swaggerhub api:create "Cophr/main/${{ env.APP_SWAGGER_Version }}" --setdefault --file=swagger-docs.json --visibility=public
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"scripts": {
"prebuild": "rimraf dist",
"build": "nest build",
"generate-swagger": "node ./dist/swagger/generate-swagger-file.js",
"format": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix && prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
Expand Down
23 changes: 23 additions & 0 deletions src/swagger/generate-swagger-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { NestFactory } from "@nestjs/core";
import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger";
import * as fs from "fs";

import { SwaggerGenerateModule } from "./swagger.module";

async function generateSwaggerJson() {
const app = await NestFactory.create(SwaggerGenerateModule);

const config = new DocumentBuilder()
.setTitle(process.env.APP_SWAGGER_Title ?? "Cophr")
.setDescription(process.env.APP_SWAGGER_Description ?? "")
.setVersion(process.env.APP_SWAGGER_Version ?? "N/A")
.build();

const document = SwaggerModule.createDocument(app, config);

fs.writeFileSync("swagger-docs.json", JSON.stringify(document));

await app.close();
}

void generateSwaggerJson();
14 changes: 14 additions & 0 deletions src/swagger/swagger.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Module } from "@nestjs/common";
import { TypeOrmModule } from "@nestjs/typeorm";
import { AppController } from "src/app.controller";
import { AppService } from "src/app.service";
import { AuthModule } from "src/auth/auth.module";
import { dataSourceJest } from "src/config/data-source";
import { UserModule } from "src/user/user.module";

@Module({
controllers: [AppController],
imports: [TypeOrmModule.forRoot(dataSourceJest), UserModule, AuthModule],
providers: [AppService],
})
export class SwaggerGenerateModule {}

0 comments on commit 35d1f22

Please sign in to comment.