Skip to content

Generate OpenAPI Client #329

Generate OpenAPI Client

Generate OpenAPI Client #329

Workflow file for this run

name: Generate OpenAPI Client
on:
push:
branches:
- main
schedule: ## Schedule the job to run bi-weekly at 8AM.
- cron: '* 8 1,15 * *'
jobs:
check-for-updates:
runs-on: ubuntu-latest
env:
OPENAPI_REPO: "https://raw.githubusercontent.com/meraki/openapi/master/openapi/spec2.json"
CLIENT_REPO: "https://api.github.com/repos/core-infra-svcs/dashboard-api-go/releases/latest"
outputs:
result: ${{ steps.generate-new-client.outputs.RESULT }}
current: ${{ steps.generate-new-client.outputs.CURRENT }}
new: ${{ steps.generate-new-client.outputs.NEW }}
steps:
- name: "Install JSON Parser"
run: sudo apt update && sudo apt install -y jq
- name: "Get Previous API Version"
run: echo "CURRENT_API_VERSION=$(curl -s GET $CLIENT_REPO | jq -r '.tag_name' | head -n1)" >> $GITHUB_ENV
- name: "Get New API Version"
run: echo "NEW_API_VERSION=v$(curl $OPENAPI_REPO | jq '.info.version' | tr -d '\"')" >> $GITHUB_ENV
- name: "Check if Client Build Required"
id: generate-new-client
run: |
echo "NEW=${{ env.NEW_API_VERSION }}" >> $GITHUB_OUTPUT
echo "CURRENT=${{ env.CURRENT_API_VERSION }}" >> $GITHUB_OUTPUT
echo "current: ${{ env.CURRENT_API_VERSION }}"
echo "New: ${{ env.NEW_API_VERSION }}"
if [ "${{ env.NEW_API_VERSION }}" != "${{ env.CURRENT_API_VERSION }}" ] && [[ "${{ env.NEW_API_VERSION }}" = v* ]]; then
echo "generate new client"
echo "RESULT=true" >> $GITHUB_OUTPUT
else
echo "skip client generation"
echo "RESULT=false" >> $GITHUB_OUTPUT
fi
generate-client:
needs: check-for-updates
runs-on: ubuntu-latest
if: needs.check-for-updates.outputs.result == 'true'
steps:
- name: "Checkout Repository"
uses: actions/checkout@v3
- name: "Fetch Specification"
run: wget https://github.com/meraki/openapi/archive/refs/tags/${{ needs.check-for-updates.outputs.new }}.zip && unzip -j ${{ needs.check-for-updates.outputs.new }}.zip '*/spec2.json'
- name: "Install JRE"
run: sudo apt update && sudo apt install -y default-jre
- name: "Install OpenAPI Generator"
run: wget https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/5.3.0/openapi-generator-cli-5.3.0.jar -O openapi-generator-cli.jar
- name: "Run OpenAPI Generator"
run: rm -rf client/; java -jar openapi-generator-cli.jar generate -i spec2.json -g go -o client -p enumClassPrefix=true -p structPrefix=true --package-name client -p packageVersion=${{ needs.check-for-updates.outputs.new }} -t .github/templates --git-user-id core-infra-svcs --git-repo-id dashboard-api-go/client --git-host github.com
- name: "Cleanup"
run: rm openapi-generator-cli.jar; rm spec2.json; rm ${{ needs.check-for-updates.outputs.new }}.zip
- name: "Git Config"
run: |
git config user.name "GitHub Actions"
git config user.email "<>"
- name: "Git Commit"
run: |
git add -A
git commit -m "OpenAPI Client Generation ${{ needs.check-for-updates.outputs.new }}"
git tag ${{ needs.check-for-updates.outputs.new }}
git push origin main
git push origin ${{ needs.check-for-updates.outputs.new }}
- name: Create a GitHub release
uses: ncipollo/release-action@v1
with:
tag: ${{ needs.check-for-updates.outputs.new }}
name: Release ${{ needs.check-for-updates.outputs.new }}
body: Meraki Golang Dashboard API ${{ needs.check-for-updates.outputs.new }}