Skip to content

Instantly share code, notes, and snippets.

@mtvbrianking
Created March 28, 2020 17:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mtvbrianking/57faef18645e1e7f69c59ac18423610c to your computer and use it in GitHub Desktop.
Save mtvbrianking/57faef18645e1e7f69c59ac18423610c to your computer and use it in GitHub Desktop.
Gitlab pages deploy swagger docs
# https://gitlab.com/mbrian/ects-ea-docs/-/ci/lint
# This file is a template, and might need editing before it works on your project.
image: node:10-alpine
# specify the location of the Open API Specification files within your project
# and the filename of the specification that you would like to display by default
variables:
DOCS_FOLDER: "specs"
SPEC_TO_DISPLAY: "rects.yaml"
# These folders are cached between builds
cache:
paths:
- ./node_modules
# publishes all files from the $DOCS_FOLDER together with the static version of SwaggerUI
# sets the specification file named in $SPEC_TO_DISPLAY to be displayed by default
pages:
stage: deploy
before_script:
- npm install swagger-ui-dist@3.22.1
script:
- mkdir public
- cp -rp node_modules/swagger-ui-dist/* public
- cp -rp $DOCS_FOLDER/* public
- sed -i "s#https://petstore\.swagger\.io/v2/swagger\.json#$SPEC_TO_DISPLAY#g" public/index.html
artifacts:
paths:
- public
only:
- master
@mtvbrianking
Copy link
Author

Project Structure

rects-docs
|--specs
|--|--rects.yml
|--|--petstore.yml
|--gitlab-ci.yml
|--license.md
|--readme.md

@mtvbrianking
Copy link
Author

ReDoc alternative

image: node:latest

pages:
  stage: deploy
  script:
  - npm install -g redoc-cli
  - redoc-cli bundle -o public/index.html documentation/openapi.yaml
  artifacts:
    paths:
    - public
  only:
  - master

Source: https://medium.com/hoursofoperation/auto-generate-swagger-docs-to-gitlab-pages-ca040230df3a

@damienburke
Copy link

Hi, thanks for the write-up.

Could u explain a bit what this line is doing, and why it refers to the petstore? Thanks

- sed -i "s#https://petstore\.swagger\.io/v2/swagger\.json#$SPEC_TO_DISPLAY#g" public/index.html

@saumyadudeja
Copy link

Hi, thanks for the write-up.

Could u explain a bit what this line is doing, and why it refers to the petstore? Thanks

- sed -i "s#https://petstore\.swagger\.io/v2/swagger\.json#$SPEC_TO_DISPLAY#g" public/index.html

After installation, you'll find that swagger-ui-dist/ dir is created, which contains index.html file. This file renders the swagger.json file that resides here: https://petstore.swagger.io/v2/swagger.json
"sed -t" looks for the line which contains the path to swagger.json file and replaces it with "your" json file, so that it renders your custom json with the same formatting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment