Skip to content

Add workflow without testing model #3

Add workflow without testing model

Add workflow without testing model #3

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/learn-github-actions/contexts
- name: Check for model changes
id: check_changes
run: |
git fetch origin ${{ github.base_ref }}
MODEL_CHANGED=false
if git diff --name-only origin/$BASE_BRANCH ${{ github.sha }} | grep -q "^${{ env.WORKING_DIRECTORY }}/models/"; then
MODEL_CHANGED=true
fi
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 makemigrations
poetry run python manage.py migrate
- name: Run Pytest Without Model
if: env.MODEL_CHANGED == 'true'
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 == 'false'
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