LoginSignup
11
3

More than 3 years have passed since last update.

GitHub ActionsでOpenAPI Generatorを動かす [Docker版]

Last updated at Posted at 2020-03-18

TL;DR

API定義ファイルが更新されたらGitHub ActionsでOpenAPI Generatorを動かしてPRを作る

用意するもの

  1. OpenAPI-Specification v3に準拠したAPI定義ファイル
  2. API定義ファイルを管理するリポジトリ
  3. 生成したクライアントを管理するリポジトリ

GitHub Actionsで動かすOpenAPI Generator

本編です

1. sample-openapiの用意

  1. GitHub Actionsを作成
generate.yaml
# This is a basic workflow to help you get started with Actions

name: generate

# Controls when the action will run. Triggers the workflow on push or pull request 
# events but only for the master branch and petstore.yaml
on:
  push:
    branches: [ master ]
    paths: petstore.yaml

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
    # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
    - uses: actions/checkout@v2

    # clientをcheckout
    - uses: actions/checkout@v2
      with:
        token: ${{ secrets.GITHUB_TOKEN }}
        repository: yumemi-nkomiya/github-actions-client-openapi
        path: client

    # openapi generate
    - uses: docker://openapitools/openapi-generator-cli
      with:
        args: generate -i ./petstore.yaml -g swift5 -o ./client/

    # Create pull request
    - uses: peter-evans/create-pull-request@v2
      with:
        token: ${{ secrets.REPO_ACCESS_TOKEN }}
        path: client
        commit-message: update client
        title: update client
        body: update client
        branch: feature/update_client
        branch-suffix: short-commit-hash # 同じプルリクは作らない

解説

  • trigger

ブランチとファイルを条件指定

  • clientをcheckout

client-openapiをチェックアウトします。
client-openapiがprivateならPersonal access tokenを作ってsample-openapiのsecretsに登録して使います

  • openapi generate

mavenビルドしてとか、jarをリポジトリに入れてとかあったんだけど公式にDockerが提供されているので利用する
パラメータはargsに指定します

  • Create pull request

client-openapiの権限があるPersonal access tokenが必要

2. GitHub Actionsの実行

sample-openapiにpetstore.yamlをpush

  • GitHub Actionsの実行結果

https://github.com/yumemi-nkomiya/github-actions-sample-openapi/actions/runs/57991712)

  • 作成されたPR


おまけ

generatorがpodspecやPackage.swiftを生成してくれるのでCocoaPodsや、tag打つだけでSwiftPMやCarthageも対応できます


参考記事

11
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
3