Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci/swagger docs #9

Merged
merged 6 commits into from
Jul 8, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 - Cosphr_develop
if: github.ref != 'refs/heads/main'
run: |
swaggerhub api:update "a20688392/Cosphr_test/${{ env.APP_SWAGGER_Version }}" --setdefault --file=swagger-docs.json --visibility=public

- name: Upload on Swaggerhub - Cosphr_main
if: github.ref == 'refs/heads/main'
run: |
swaggerhub api:create "a20688392/Cosphr/${{ 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 {}