summaryrefslogtreecommitdiff
path: root/.github/workflows/build.yml
blob: 10f778dc6ed27a3283cf608621c94f16f9d22e2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
name: Build

on:
  push:
    branches: [master, dev]
    tags: ['v*']
  pull_request:
    branches: [master, dev]
  workflow_dispatch:
env:
  LATEST_PY_VERSION: '3.9'
  COVERAGE_ARGS: '--cov --cov-report=term --cov-report=html'

jobs:
  # Run unit tests for each supported python version
  test:
    runs-on: ubuntu-18.04
    strategy:
      matrix:
        python-version: [3.6, 3.7, 3.8, 3.9]
    services:
      nginx:
        image: kennethreitz/httpbin
        ports:
          - 80:80

    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}

      # Start integration test databases
      - uses: supercharge/mongodb-github-action@1.3.0
        with:
          mongodb-version: 4.4
      - uses: supercharge/redis-github-action@1.2.0
        with:
          redis-version: 6
      - uses: rrainn/dynamodb-action@v2.0.0

      # Cache packages per python version, and reuse until setup.py changes
      - name: Cache pip packages
        uses: actions/cache@v2
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ matrix.python-version }}-${{ hashFiles('setup.py') }}
          restore-keys: ${{ runner.os }}-pip-${{ matrix.python-version }}
      - name: Install dependencies
        run: pip install ".[dev]"

      # Latest python version: Run tests with coverage and send to coveralls
      - name: Run tests with code coverage report
        if: ${{ matrix.python-version == env.LATEST_PY_VERSION }}
        # Run unit tests first (and with multiprocessing) to fail quickly if there are issues
        run: |
          pytest tests/unit --numprocesses=auto ${{ env.COVERAGE_ARGS }}
          pytest tests/integration --cov-append ${{ env.COVERAGE_ARGS }}
      - name: Send code coverage report to Coveralls
        if: ${{ matrix.python-version == env.LATEST_PY_VERSION }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: coveralls --service=github

      # All other python versions: just run tests
      - name: Run tests
        if: ${{ matrix.python-version != env.LATEST_PY_VERSION }}
        run: |
          pytest --numprocesses=auto tests/unit
          pytest tests/integration

      # Run longer stress tests if this is a release or merge to master
      - name: Run stress tests
        if: startsWith(github.ref, 'refs/tags/v') || endsWith(github.ref, '/master')
        run: STRESS_TEST_MULTIPLIER=5 pytest tests/integration/ -k 'multithreaded'

  # Run code analysis checks
  analyze:
    runs-on: ubuntu-18.04
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v2
        with:
          python-version: ${{ env.LATEST_PY_VERSION }}

      # Cache packages for latest python version, and reuse until setup.py changes
      - name: Cache pip packages
        uses: actions/cache@v2
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ env.LATEST_PY_VERSION }}-${{ hashFiles('setup.py') }}
          restore-keys: ${{ runner.os }}-pip-${{ env.LATEST_PY_VERSION }}
      - name: Install dependencies
        run: pip install ".[dev]"

      - name: Run linter
        run: flake8 .
      - name: Run code style checks
        run: |
          black --check --diff .
          isort --check --diff .
      - name: Run cyclomatic complexity check
        run: radon cc --show-complexity --average --order SCORE requests_cache

  # Deploy pre-release builds from dev branch, and stable builds on tags only
  release:
    needs: [test, analyze]
    if: startsWith(github.ref, 'refs/tags/v') || endsWith(github.ref, '/dev')
    runs-on: ubuntu-18.04
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v2
        with:
          python-version: ${{ env.LATEST_PY_VERSION }}

      - name: Install dependencies
        run: pip install -U ".[build]"
      - name: Build wheel
        run: python setup.py sdist bdist_wheel
      - name: Deploy to pypi
        env:
          TWINE_USERNAME: __token__
          TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
        run: twine upload dist/*