TL;DR
API定義ファイルが更新されたらGitHub ActionsでOpenAPI Generatorを動かしてPRを作る
用意するもの
- OpenAPI-Specification v3に準拠したAPI定義ファイル
- API定義ファイルを管理するリポジトリ
- 生成したクライアントを管理するリポジトリ
GitHub Actionsで動かすOpenAPI Generator
本編です
1. sample-openapiの用意
- 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も対応できます