name: Build & Publish

on:
  push:
    branches:
      - master
    tags:
      - v5.*

jobs:
  build:
    runs-on: ubuntu-latest
    services:
      mysql:
        image: mariadb:10.3
        ports:
          - 3306
        env:
          MYSQL_ROOT_PASSWORD: testing
          MYSQL_DATABASE: karp
          MYSQL_PASSWORD: testing
          MYSQL_USER: karp
        options: --health-cmd "mysqladmin ping" --health-interval 10s --health-timeout 5s --health-retries 10

    strategy:
      max-parallel: 4
      matrix:
        python-version: [3.6, 3.7, 3.8]
        es-version: ["6.2.4", "6.5.4", "6.8.6"]
    steps:
      - uses: actions/checkout@v1
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v1
        with:
          python-version: ${{ matrix.python-version }}
      - uses: actions/cache@v1
        name: Look for cached dependencies
        id: cache-pip
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ hashFiles('**/setup.cfg') }}-${{ hashFiles('**/requirements.txt') }}
          restore-keys: |
            ${{ runner.os }}-pip-${{ hashFiles('**/setup.cfg') }}-
            ${{ runner.os }}-pip-
      - name: Install dependencies
        # if: steps.cache-pip.outputs.cache-hit != 'true'
        run: |
          python -m pip install --upgrade pip
          make install-dev
      # - uses: actions/cache@v1
      #   name: Look for cached elasticsearch
      #   id: cache-es
      #   with:
      #     path: elasticsearch-${{ matrix.es-version }}
      #     key: es-${{ matrix.es-version }}
      - name: Download elasticsearch
        # if: steps.cache-es.outputs.cache-hit != 'true'
        run: |
          wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${{ matrix.es-version }}.tar.gz
          tar -xzf elasticsearch-${{ matrix.es-version }}.tar.gz
      - name: Create .env file
        run: |
          echo "ELASTICSEARCH_ENABLED=True" >> .env
          echo "ES_HOME=`realpath elasticsearch-${{ matrix.es-version }}`" >> .env
          cat .env
      - name: Lint with flake8 for syntax errors
        run: |
          pip install flake8
          # stop the build if there are Python syntax errors or undefined names
          flake8 karp5 setup.py wsgi.py --count --select=E9,F63,F7,F82 --show-source --statistics
      - name: Lint with pylint
        run: |
          make lint-no-fail
          # pylint --rcfile=.pylintrc --exit-zero karp5 setup.py run.py cli.py
      - name: Test with pytest
        run: |
          echo "KARP5_DBPASS=${KARP5_DBPASS}"
          make test-cov
          # pytest -vv --cov=karp5 --cov-report=term karp5/tests
          docker logs "${{ job.services.mysql.id }}"
        env:
          KARP5_DBPASS: karp:testing@127.0.0.1:${{ job.services.mysql.ports['3306'] }}
      # - name: Coveralls
      #   uses: coverallsapp/github-action@master
      #   with:
      #     github-token: ${{ secrets.github_token }}
  publish:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - name: Set up Python 3.6
        uses: actions/setup-python@v1
        with:
          python-version: 3.6
      - name: Build distribution
        run: |
          pip install --upgrade setuptools twine wheel pbr
          python setup.py sdist bdist_wheel
          echo "github.ref = ${{ github.ref }}"
      - name: Check distributions
        run: twine check dist/*
      - name: Publish package to TestPyPI
        uses: pypa/gh-action-pypi-publish@master
        if: github.repository == 'spraakbanken/karp-backend' && !startsWith(github.ref, 'refs/tags/')
        with:
          user: __token__
          password: ${{ secrets.test_pypi_password }}
          repository_url: https://test.pypi.org/legacy/
      - name: Publish package to PyPI
        uses: pypa/gh-action-pypi-publish@master
        if: github.repository == 'spraakbanken/karp-backend' && startsWith(github.ref, 'refs/tags/')
        with:
          user: __token__
          password: ${{ secrets.pypi_password }}
