name: auto CI

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]
  schedule:
  - cron: "0 2 * * 1-5"

jobs:
  build:
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
        include:
        - os: ubuntu-latest
          path: ~/.cache/pip
        - os: macos-latest
          path: ~/Library/Caches/pip
        - os: windows-latest
          path: ~\AppData\Local\pip\Cache
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
        
      - name: Set up Python 3.8
        id: setup-python
        uses: actions/setup-python@v4
        with:
          python-version: 3.8

      - uses: actions/cache@v2
        with:
          path: ${{ matrix.path }}
          key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
          restore-keys: |
            ${{ runner.os }}-pip-

      - uses: actions/cache@v2
        with:
          path: ~/.local/share/virtualenvs
          key: ${{ runner.os }}-python-${{ steps.setup-python.outputs.python-version }}-pipenv-${{ hashFiles('Pipfile.lock') }}
        
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
  
      - name: Update Deny Host
        run: |
          pwd
          python cmd/refresh.py

      - name: Push to built branch
        uses: Automattic/action-commit-to-branch@master
        with:
          branch: 'master'
          commit_message: 'Update Host'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Publish distribution 📦 to Test PyPI
        uses: pypa/gh-action-pypi-publish@master
        with:
          user: __token__
          password: ${{ secrets.test_pypi_password }}
          repository_url: https://test.pypi.org/legacy/

      - name: Publish distribution 📦 to PyPI
        if: startsWith(github.event.ref, 'refs/tags')
        uses: pypa/gh-action-pypi-publish@master
        with:
          user: __token__
          password: ${{ secrets.pypi_password }}