# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
name: Build and publish

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  run-tests-and-coverage:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: [3.7, 3.8]
        django-version: [2.0, 3.0]
    steps:
    - uses: actions/checkout@v2
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v2
      with:
        python-version: ${{ matrix.python-version }}
    - name: Prepare environment
      run: |
        pip install pipenv
        pipenv install
    - name: Install Django ${{ matrix.django-version }}
      run: |
        pipenv run pip install django==${{ matrix.django-version }}
    - name: Install package and check installed middle is working
      run: |
        pipenv run flit install
        DJANGO_SETTINGS_MODULE=fake_django.settings pipenv run pytest django-for-development
        pipenv run pip uninstall hmin
    - name: Run tests
      run: |
        pipenv run pytest -n2 hmin --cov=./ --cov-report=xml
    - name: Upload code coverage to be accessed in next job
      uses: actions/upload-artifact@v2
      with:
        name: coverage.xml
        path: ./coverage.xml

  upload-codecov:
    runs-on: ubuntu-latest
    needs: run-tests-and-coverage
    steps:
    - uses: actions/download-artifact@v2
      with:
        name: coverage.xml
    - name: Upload coverage to Codecov
      uses: codecov/codecov-action@v1
      with:
        token: ${{ secrets.CODECOV_TOKEN }}
        file: ./coverage.xml
        flags: unittests
        env_vars: OS,PYTHON
        name: codecov-umbrella
        fail_ci_if_error: true

  publish-pypi:
    needs: upload-codecov
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Set up Python
      uses: actions/setup-python@v2
      with:
        python-version: 3.8
    - name: Publish package
      env:
        FLIT_USERNAME: ${{ secrets.FLIT_USERNAME }}
        FLIT_PASSWORD: ${{ secrets.FLIT_PASSWORD }}
      continue-on-error: true
      run: |
        pip install pipenv
        pipenv install
        pipenv run flit publish
