Generate OpenAPI Client #255
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/spec3.json" | |
CLIENT_REPO: "https://api.github.com/repos/meraki/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=$(curl $OPENAPI_REPO | jq '.info.version' | tr -d '\"')" >> $GITHUB_ENV | |
- name: "Export Results" | |
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 }}" ]]; then | |
if [[ "${{ env.NEW_API_VERSION }}" = v* ]] && [[ "${{ env.CURRENT_API_VERSION }}" != v* ]]; then | |
echo "generate new client" | |
echo "RESULT=true" >> $GITHUB_OUTPUT | |
elif [[ "${{ env.NEW_API_VERSION }}" != v* ]] && [[ "${{ env.CURRENT_API_VERSION }}" = v* ]]; then | |
echo "generate new client" | |
echo "RESULT=true" >> $GITHUB_OUTPUT | |
else | |
echo "skip client generation" | |
echo "RESULT=false" >> $GITHUB_OUTPUT | |
fi | |
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: "Check Version Prefix" | |
id: version-prefix | |
run: | | |
if [[ "${{ needs.check-for-updates.outputs.new }}" != v* ]]; then | |
echo "NEW_VERSION=v${{ needs.check-for-updates.outputs.new }}" >> $GITHUB_ENV | |
else | |
echo "NEW_VERSION=${{ needs.check-for-updates.outputs.new }}" >> $GITHUB_ENV | |
fi | |
- name: "Fetch Specification" | |
run: wget https://github.com/meraki/openapi/archive/refs/tags/${{ env.NEW_VERSION }}.zip && unzip -j ${{ env.NEW_VERSION }}.zip '*/spec3.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/6.2.1/openapi-generator-cli-6.2.1.jar -O .openapi/generator/openapi-generator-cli.jar | |
- name: "Run OpenAPI Generator" | |
run: rm -rf client/; java -jar .openapi/generator/openapi-generator-cli.jar generate -i spec3.json -g go -o client -p enumClassPrefix=true -p structPrefix=true --package-name client -p packageVersion=${{ env.NEW_VERSION }} -t .openapi/templates --git-user-id meraki --git-repo-id dashboard-api-go/client --git-host github.com | |
- name: "Cleanup" | |
run: rm .openapi/generator/openapi-generator-cli.jar; rm spec3.json; rm ${{ env.NEW_VERSION }}.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 ${{ env.NEW_VERSION }}" | |
git tag ${{ env.NEW_VERSION }} | |
git push origin main | |
git push origin ${{ env.NEW_VERSION }} | |
- name: Create a GitHub release | |
uses: ncipollo/release-action@v1 | |
with: | |
tag: ${{ env.NEW_VERSION }} | |
name: Release ${{ env.NEW_VERSION }} | |
body: Meraki Golang Dashboard API ${{ env.NEW_VERSION }} |