|
| 1 | +# This workflow will install Python dependencies, run tests and lint with a variety of Python versions |
| 2 | +# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions |
| 3 | + |
| 4 | +name: Latest release |
| 5 | + |
| 6 | +env: |
| 7 | + CACHE_VERSION: 1 |
| 8 | + DEFAULT_PYTHON: 3.9 |
| 9 | + PRE_COMMIT_HOME: ~/.cache/pre-commit |
| 10 | + |
| 11 | +# Only run on merges |
| 12 | +on: |
| 13 | + pull_request: |
| 14 | + types: closed |
| 15 | + branches: |
| 16 | + - main |
| 17 | + |
| 18 | +jobs: |
| 19 | + # Prepare default python version environment |
| 20 | + prepare: |
| 21 | + runs-on: ubuntu-latest |
| 22 | + name: Prepare |
| 23 | + steps: |
| 24 | + - name: Check out committed code |
| 25 | + uses: actions/checkout@v2 |
| 26 | + - name: Set up Python ${{ env.DEFAULT_PYTHON }} |
| 27 | + id: python |
| 28 | + uses: actions/setup-python@v2 |
| 29 | + with: |
| 30 | + python-version: ${{ env.DEFAULT_PYTHON }} |
| 31 | + - name: Restore base Python ${{ env.DEFAULT_PYTHON }} virtual environment |
| 32 | + id: cache-venv |
| 33 | + uses: actions/cache@v2 |
| 34 | + with: |
| 35 | + path: venv |
| 36 | + key: >- |
| 37 | + ${{ env.CACHE_VERSION}}-${{ runner.os }}-base-venv-${{ |
| 38 | + steps.python.outputs.python-version }}-${{ |
| 39 | + hashFiles('requirements_test.txt') }}-${{ |
| 40 | + hashFiles('setup.py') }} |
| 41 | + restore-keys: | |
| 42 | + ${{ env.CACHE_VERSION}}-${{ runner.os }}-base-venv-${{ steps.python.outputs.python-version }}-${{ hashFiles('requirements_test.txt') }}-${{ hashFiles('setup.py') }}- |
| 43 | + ${{ env.CACHE_VERSION}}-${{ runner.os }}-base-venv-${{ steps.python.outputs.python-version }}-${{ hashFiles('requirements_test.txt') }} |
| 44 | + ${{ env.CACHE_VERSION}}-${{ runner.os }}-base-venv-${{ steps.python.outputs.python-version }}- |
| 45 | + - name: Create Python virtual environment |
| 46 | + if: steps.cache-venv.outputs.cache-hit != 'true' |
| 47 | + run: | |
| 48 | + python -m venv venv |
| 49 | + . venv/bin/activate |
| 50 | + pip install -U pip setuptools |
| 51 | + pip install -e . |
| 52 | + pip install -r requirements_test.txt |
| 53 | + - name: Restore pre-commit environment from cache |
| 54 | + id: cache-precommit |
| 55 | + uses: actions/cache@v2 |
| 56 | + with: |
| 57 | + path: ${{ env.PRE_COMMIT_HOME }} |
| 58 | + key: | |
| 59 | + ${{ env.CACHE_VERSION}}-${{ runner.os }}-pre-commit-${{ hashFiles('.pre-commit-config.yaml') }} |
| 60 | + restore-keys: | |
| 61 | + ${{ env.CACHE_VERSION}}-${{ runner.os }}-pre-commit- |
| 62 | + - name: Install pre-commit dependencies |
| 63 | + if: steps.cache-precommit.outputs.cache-hit != 'true' |
| 64 | + run: | |
| 65 | + . venv/bin/activate |
| 66 | + pre-commit install-hooks |
| 67 | +
|
| 68 | + publishing: |
| 69 | + name: Build and publish Python 🐍 distributions 📦 to PyPI |
| 70 | + runs-on: ubuntu-latest |
| 71 | + needs: prepare |
| 72 | + # Only trigger on merges, not just closes |
| 73 | + if: github.event.pull_request.merged == true |
| 74 | + steps: |
| 75 | + - name: Check out committed code |
| 76 | + uses: actions/checkout@main |
| 77 | + - name: Set up Python ${{ env.DEFAULT_PYTHON }} |
| 78 | + id: python |
| 79 | + uses: actions/setup-python@v2 |
| 80 | + with: |
| 81 | + python-version: ${{ env.DEFAULT_PYTHON }} |
| 82 | + - name: Restore base Python ${{ env.DEFAULT_PYTHON }} virtual environment |
| 83 | + id: cache-venv |
| 84 | + uses: actions/cache@v2 |
| 85 | + with: |
| 86 | + path: venv |
| 87 | + key: >- |
| 88 | + ${{ env.CACHE_VERSION}}-${{ runner.os }}-base-venv-${{ |
| 89 | + steps.python.outputs.python-version }}-${{ |
| 90 | + hashFiles('requirements_test.txt') }}-${{ |
| 91 | + hashFiles('setup.py') }} |
| 92 | + - name: Fail job if Python cache restore failed |
| 93 | + if: steps.cache-venv.outputs.cache-hit != 'true' |
| 94 | + run: | |
| 95 | + echo "Failed to restore Python virtual environment from cache" |
| 96 | + exit 1 |
| 97 | + - name: Build a distribution |
| 98 | + run: >- |
| 99 | + python setup.py sdist |
| 100 | + - name: Publish distribution 📦 to Test PyPI |
| 101 | + uses: pypa/gh-action-pypi-publish@master |
| 102 | + with: |
| 103 | + password: ${{ secrets.pypi_token }} |
| 104 | + skip_existing: true |
0 commit comments