Skip to content

Add workflow without testing model #17

Add workflow without testing model

Add workflow without testing model #17

name: Run Pytest without Model
on:
pull_request:
types: [opened, reopened, synchronize, ready_for_review]
env:
WORKING_DIRECTORY: application
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: test-db
MYSQL_HOST: 127.0.0.1
MYSQL_PORT: 3306
MYSQL_USER: test
MYSQL_PASSWORD: test
DJANGO_SETTINGS_MODULE: project.settings.local
jobs:
Setup:
name: Run Test Code
runs-on: ubuntu-20.04
defaults:
run:
working-directory: application
services:
db:
image: mysql:8.0
ports:
- 3306:3306
env:
MYSQL_ROOT_PASSWORD: ${{ env.MYSQL_ROOT_PASSWORD }}
MYSQL_DATABASE: ${{ env.MYSQL_DATABASE }}
MYSQL_USER: ${{ env.MYSQL_USER }}
MYSQL_PASSWORD: ${{ env.MYSQL_PASSWORD }}
options: >-
--health-cmd "mysqladmin ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Chekcout code
uses: actions/checkout@v4
# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idif
- name: Check for model changes
# https://www.ibm.com/docs/ja/aix/7.1?topic=g-grep-command#grep__row-d3e144113
id: check_changes
run: |
MODEL_CHANGED=false
if git diff origin/${{ github.base_ref }} --name-only | grep -c ${{ env.WORKING_DIRECTORY }}/application/models/ > 0; then
echo $MODEL_CHANGED
MODEL_CHANGED=true
fi
echo $MODEL_CHANGED
echo "MODEL_CHANGED=$MODEL_CHANGED" >> $GITHUB_ENV
- name: Grant privileges to user
run: mysql --protocol=tcp -h 127.0.0.1 -P 3306 -u root -p$MYSQL_ROOT_PASSWORD -e "GRANT ALL PRIVILEGES ON *.* TO '$MYSQL_USER'@'%'; FLUSH PRIVILEGES;"
- name: Setup Poetry
uses: ./.github/actions/set-up-poetry
with:
working-directory: ${{ env.WORKING_DIRECTORY }}
- name: Run migration
run: poetry run python manage.py migrate
- name: Run Pytest Without Model
# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idif
if: env.MODEL_CHANGED == 'false'
run: |
set -o pipefail
poetry run pytest ${{ env.WORKING_DIRECTORY }}/tests/serializers ${{ env.WORKING_DIRECTORY }}/tests/views --junitxml=pytest.xml -x -n auto --cov --no-cov-on-fail --suppress-no-test-exit-code | tee pytest-coverage.txt
- name: Run Pytest
if: env.MODEL_CHANGED == 'true'
run: |
set -o pipefail
poetry run pytest --junitxml=pytest.xml -x -n auto --cov --no-cov-on-fail --suppress-no-test-exit-code | tee pytest-coverage.txt
- name: Pytest coverage comment
uses: MishaKav/pytest-coverage-comment@main
with:
pytest-coverage-path: ${{ env.WORKING_DIRECTORY }}/pytest-coverage.txt
junitxml-path: ${{ env.WORKING_DIRECTORY }}/pytest.xml