sync-openapi #105
This file contains hidden or 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: sync-openapi | |
| on: | |
| workflow_dispatch: | |
| repository_dispatch: | |
| types: | |
| - openapi-updated | |
| schedule: | |
| - cron: "*/15 * * * *" | |
| permissions: | |
| contents: write | |
| concurrency: | |
| group: sync-openapi | |
| cancel-in-progress: false | |
| jobs: | |
| sync: | |
| if: github.repository == 'justoneapi/justoneapi-python' | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| ref: main | |
| fetch-depth: 0 | |
| - uses: actions/setup-python@v5 | |
| with: | |
| python-version: "3.11" | |
| - name: Install dependencies | |
| run: python -m pip install -e '.[dev]' | |
| - name: Backup current spec | |
| run: cp openapi/public-api.json "$RUNNER_TEMP/public-api.previous.json" | |
| - name: Fetch OpenAPI | |
| env: | |
| OPENAPI_BASIC_AUTH_USERNAME: ${{ secrets.OPENAPI_BASIC_AUTH_USERNAME }} | |
| OPENAPI_BASIC_AUTH_PASSWORD: ${{ secrets.OPENAPI_BASIC_AUTH_PASSWORD }} | |
| run: python scripts/fetch_openapi.py | |
| - name: Normalize OpenAPI | |
| run: python scripts/normalize_openapi.py | |
| - name: Generate SDK | |
| run: python scripts/generate_sdk.py | |
| - name: Run tests | |
| run: python -m pytest | |
| - name: Build sync summary | |
| run: python scripts/diff_openapi.py "$RUNNER_TEMP/public-api.previous.json" openapi/public-api.json --output "$RUNNER_TEMP/openapi-sync-summary.md" | |
| - name: Detect SDK changes | |
| id: changes | |
| run: | | |
| if git diff --quiet -- openapi/public-api.json openapi/public-api.normalized.json justoneapi/generated; then | |
| echo "changed=false" >> "$GITHUB_OUTPUT" | |
| echo "OpenAPI spec and generated SDK are already up to date." | |
| else | |
| echo "changed=true" >> "$GITHUB_OUTPUT" | |
| fi | |
| - name: Bump package version | |
| if: steps.changes.outputs.changed == 'true' | |
| id: version | |
| run: | | |
| version="$(python scripts/bump_version.py)" | |
| echo "version=${version}" >> "$GITHUB_OUTPUT" | |
| echo "tag=v${version}" >> "$GITHUB_OUTPUT" | |
| echo "Bumped package version to ${version}" | |
| - name: Commit generated SDK update | |
| if: steps.changes.outputs.changed == 'true' | |
| env: | |
| VERSION: ${{ steps.version.outputs.version }} | |
| run: | | |
| git config user.name "github-actions[bot]" | |
| git config user.email "41898282+github-actions[bot]@users.noreply.github.com" | |
| git add openapi/public-api.json openapi/public-api.normalized.json justoneapi/generated justoneapi/_version.py | |
| { | |
| echo "chore: sync OpenAPI spec and generated SDK v${VERSION}" | |
| echo | |
| cat "$RUNNER_TEMP/openapi-sync-summary.md" | |
| } > "$RUNNER_TEMP/commit-message.txt" | |
| git commit -F "$RUNNER_TEMP/commit-message.txt" | |
| - name: Push main and release tag | |
| if: steps.changes.outputs.changed == 'true' | |
| env: | |
| TAG: ${{ steps.version.outputs.tag }} | |
| run: | | |
| if git ls-remote --exit-code --tags origin "refs/tags/${TAG}" >/dev/null 2>&1; then | |
| echo "Tag ${TAG} already exists on origin." >&2 | |
| exit 1 | |
| fi | |
| for attempt in 1 2 3; do | |
| git fetch --tags origin main | |
| git rebase origin/main | |
| git tag -f -a "${TAG}" -m "Release ${TAG}" | |
| if git push --atomic origin HEAD:main "refs/tags/${TAG}"; then | |
| exit 0 | |
| fi | |
| git tag -d "${TAG}" || true | |
| sleep 10 | |
| done | |
| echo "Failed to push main and ${TAG} after 3 attempts." >&2 | |
| exit 1 | |
| - name: Trigger release workflow | |
| if: steps.changes.outputs.changed == 'true' | |
| env: | |
| GITHUB_TOKEN: ${{ github.token }} | |
| TAG: ${{ steps.version.outputs.tag }} | |
| run: | | |
| curl --fail-with-body --silent --show-error \ | |
| --request POST \ | |
| --header "Accept: application/vnd.github+json" \ | |
| --header "Authorization: Bearer ${GITHUB_TOKEN}" \ | |
| --header "X-GitHub-Api-Version: 2022-11-28" \ | |
| "https://api.github.com/repos/${GITHUB_REPOSITORY}/dispatches" \ | |
| --data "{\"event_type\":\"openapi-sdk-release\",\"client_payload\":{\"tag\":\"${TAG}\"}}" |