ci: check and dispatch api schema changes
- check if api schema is outdated - do not cancel in progress workflow Since this workflow commit to an external repo, it might squash multiple changes into a single commit. - dispatch each schema update to client repo - cache pip dependencies
This commit is contained in:
parent
08bcaf459c
commit
b10696265f
|
@ -3,40 +3,104 @@ name: API schema
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
branches: [main]
|
branches: [main]
|
||||||
|
paths:
|
||||||
|
- .github/workflows/**
|
||||||
|
- api/**
|
||||||
|
|
||||||
|
pull_request:
|
||||||
|
branches: [main]
|
||||||
|
paths:
|
||||||
|
- .github/workflows/**
|
||||||
|
- api/**
|
||||||
|
|
||||||
concurrency:
|
concurrency:
|
||||||
group: ${{ github.workflow }}-${{ github.ref }}
|
group: ${{ github.workflow }}-${{ github.ref }}
|
||||||
cancel-in-progress: true
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
generate:
|
check:
|
||||||
if: github.repository_owner == 'libretime'
|
|
||||||
name: Generate
|
|
||||||
|
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
- uses: actions/setup-python@v4
|
- uses: actions/setup-python@v4
|
||||||
with:
|
with:
|
||||||
python-version: "3.x"
|
python-version: "3.x"
|
||||||
|
|
||||||
- name: Clone client repo
|
- uses: actions/cache@v3
|
||||||
uses: actions/checkout@v3
|
|
||||||
with:
|
with:
|
||||||
repository: "${{ github.repository_owner }}/client"
|
path: ~/.cache/pip
|
||||||
path: client-repo
|
key: ${{ runner.os }}-pip-api-${{ hashFiles('api/**/setup.py') }}
|
||||||
ssh-key: "${{ secrets.API_CLIENT_DEPLOY_KEY }}"
|
restore-keys: |
|
||||||
|
${{ runner.os }}-pip-api
|
||||||
|
|
||||||
- name: Generate schema
|
- name: Install
|
||||||
run: |
|
run: make install
|
||||||
make schema
|
|
||||||
cp schema.yml ../client-repo/schema.yml
|
|
||||||
working-directory: api
|
working-directory: api
|
||||||
|
|
||||||
- name: Push schema changes to API clients
|
- name: Get pull request commit range
|
||||||
uses: stefanzweifel/git-auto-commit-action@v4
|
if: github.event_name == 'pull_request'
|
||||||
|
run: echo "COMMIT_RANGE=origin/${{ github.base_ref }}..${{ github.sha }}" >> $GITHUB_ENV
|
||||||
|
|
||||||
|
- name: Get push commit range
|
||||||
|
if: github.event_name == 'push'
|
||||||
|
run: echo "COMMIT_RANGE=${{ github.event.before }}..${{ github.sha }}" >> $GITHUB_ENV
|
||||||
|
|
||||||
|
- name: Check if schema is outdated
|
||||||
|
run: |
|
||||||
|
for commit in $(git rev-list --reverse --no-merges ${{ env.COMMIT_RANGE }}); do
|
||||||
|
git checkout $commit
|
||||||
|
|
||||||
|
make --quiet schema
|
||||||
|
git add schema.yml
|
||||||
|
git diff-index --quiet HEAD -- || {
|
||||||
|
echo "ERROR: Schema is outdated for commit $commit"
|
||||||
|
git show --quiet
|
||||||
|
git diff -- schema.yml
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
done
|
||||||
|
working-directory: api
|
||||||
|
|
||||||
|
dispatch:
|
||||||
|
if: >
|
||||||
|
github.repository_owner == 'libretime'
|
||||||
|
&& github.event_name == 'push'
|
||||||
|
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
with:
|
with:
|
||||||
commit_message: "chore: update schema for ${{ github.repository }}@${{ github.sha }}"
|
fetch-depth: 0
|
||||||
commit_user_name: "github-actions[bot]"
|
|
||||||
commit_user_email: "github-actions[bot]@users.noreply.github.com"
|
- uses: actions/checkout@v3
|
||||||
repository: ./client-repo
|
with:
|
||||||
|
repository: libretime/client
|
||||||
|
path: client
|
||||||
|
ssh-key: "${{ secrets.API_CLIENT_DEPLOY_KEY }}"
|
||||||
|
|
||||||
|
- name: Dispatch schema update commits
|
||||||
|
run: |
|
||||||
|
git config --global user.email "libretime-bot"
|
||||||
|
git config --global user.name "libretime-bot@users.noreply.github.com"
|
||||||
|
|
||||||
|
for commit in $(git rev-list --reverse --no-merges ${{ github.event.before }}..${{ github.sha }} -- api/schema.yml); do
|
||||||
|
cp api/schema.yml client/
|
||||||
|
|
||||||
|
git show \
|
||||||
|
--quiet \
|
||||||
|
--format="%B%n${{ github.repository }}@%H" \
|
||||||
|
"$commit" \
|
||||||
|
> commit-message
|
||||||
|
|
||||||
|
pushd client/
|
||||||
|
git add schema.yml
|
||||||
|
git diff-index --quiet HEAD -- || {
|
||||||
|
git commit --file=../commit-message
|
||||||
|
git push
|
||||||
|
}
|
||||||
|
popd
|
||||||
|
|
||||||
|
rm commit-message
|
||||||
|
done
|
||||||
|
|
|
@ -15,7 +15,8 @@ test: $(VENV)
|
||||||
source $(VENV)/bin/activate
|
source $(VENV)/bin/activate
|
||||||
DJANGO_SETTINGS_MODULE=libretime_api.settings.testing pytest -n $(CPU_CORES) --color=yes -v libretime_api
|
DJANGO_SETTINGS_MODULE=libretime_api.settings.testing pytest -n $(CPU_CORES) --color=yes -v libretime_api
|
||||||
|
|
||||||
|
SCHEMA_FILE ?= schema.yml
|
||||||
schema: $(VENV)
|
schema: $(VENV)
|
||||||
source $(VENV)/bin/activate
|
source $(VENV)/bin/activate
|
||||||
DJANGO_SETTINGS_MODULE=libretime_api.settings.testing libretime-api spectacular --file schema.yml
|
DJANGO_SETTINGS_MODULE=libretime_api.settings.testing libretime-api spectacular --file $(SCHEMA_FILE)
|
||||||
if command -v npx > /dev/null; then npx prettier --write schema.yml; fi
|
if command -v npx > /dev/null; then npx prettier --write $(SCHEMA_FILE); fi
|
||||||
|
|
Loading…
Reference in New Issue