yonaskolb / SwagGen

OpenAPI/Swagger 3.0 Parser and Swift code generator
MIT License
626 stars 146 forks source link

Docker Support #235

Closed mackoj closed 3 years ago

mackoj commented 4 years ago

Let's restart the discussion around docker image support. This aim to replace https://github.com/yonaskolb/SwagGen/pull/163 and add support for docker for this projet.

Why

After almost a year of working with Swaggen in a Docker container I do prefer this workflow. Because it guarantees that it is working the same way as your CI is working or if your backend dev are on Linux it works for them to too test if they didn't break your library. With one script anyone(back/front end dev) in the team can generate a custom version of the library. It's easier to maintain and update.

The generated images are 77mb of size and have everything required for swift and swaggen to work.

How to use the generated image

# Pull this image
docker pull yonaskolb/swaggen:latest

declare DOCKER_MOUNTED_PATH="/tmp/workdir"

docker run                                                              \
  --rm                                                                  \
  -v "$(pwd):${DOCKER_MOUNTED_PATH}"                                    \
  yonaskolb/swaggen:latest                                                 \
  swaggen generate "${DOCKER_MOUNTED_PATH}/swagger.json"                \
  --language swift                                                      \
  --template "${DOCKER_MOUNTED_PATH}/Templates/Swift/template.yml"      \
  --destination "${DOCKER_MOUNTED_PATH}/ProjectXYZ" \
  --clean all                                                           \
  --verbose

When to update it

You will need to update the Swaggen image for every new Swift build. But luckily it's a script to run with nothing to change in it and it will create/tag/publish everything.

Change description

getLastSwiftTag.swift is a script that look for all the tag of a repo then search the one that match the pattern given in parameter(version of the Swift Toolchains for Linux released) then sort then and take the one with the bigger version number. updateDockerImage.sh is the main script here it goal is to generate a new docker image base from the official swift docker image but it add swaggen to it. It designed to be run every time a new version of swift is available publicly or for every new version of Swaggen. It build tag test and push the image to docker hub. The way the script is done allow for easy customization it's pretty easy to build a docker image for a branch of a repo. I'm not great a shell script but it is doing the job pretty well. Docker.md aims to the be central documentation point for docker matter. Dockerfile is the recipe used to build the docker images it based on mithun work on the matter.

The way updateDockerImage.sh is written allow you to use it in other place pretty easily.

Todo for the maintainer

In order for all of this to work @yonaskolb you have to make an account with yonaskolb username and create the project swaggen on it on https://hub.docker.com it's free for public docker image. Once it done please update the top of updateDockerImage.sh with what you chose as login and project. Then just run it.

Other people do the same

OpenAPI provide a Docker image for their generator.

# Pull this image
docker pull docker pull openapitools/openapi-generator-cli:latest

declare DOCKER_MOUNTED_PATH="/tmp/workdir"

docker run                                                              \
  --rm                                                                  \
  -v "$(pwd):${DOCKER_MOUNTED_PATH}"                                    \
  openapitools/openapi-generator-cli:latest generate                    \
  -i "${DOCKER_MOUNTED_PATH}/swagger.json"                              \
  -g swift5                                                             \
  -o "${DOCKER_MOUNTED_PATH}/ProjectXYZ"                 \
  --verbose

I hope it helps.