Skip to content

Commit

Permalink
ci: Refactor CI workflows and actions (#160)
Browse files Browse the repository at this point in the history
By creating next composite actions in:

- Install Python & poetry
- Run pre-commit
- Run tests

And by creating "Build & deploy package" reusable workflow.
  • Loading branch information
playpauseandstop authored Jun 22, 2022
1 parent 8198ec9 commit 0a7843c
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 146 deletions.
45 changes: 45 additions & 0 deletions .github/actions/install_python_and_poetry/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: "Install Python & poetry"
description: "Composite action to setup Python, install poetry, and setup cache for venv."

inputs:
python-version:
description: "Python version to use"
required: false
python-version-file:
description: "Read Python version from given file"
required: false
poetry-version:
description: "Poetry version to use"
required: false
default: "1.1.13"

outputs:
python-path:
description: "Absolute path to Python executable"
value: "${{ steps.python.outputs.python-path }}"
python-version:
description: "Installed Python version"
value: "${{ steps.python.outputs.python-version }}"
poetry-version:
description: "Installed poetry version"
value: "${{ inputs.poetry-version }}"

runs:
using: "composite"
steps:
- id: "python"
name: "Install Python"
uses: "actions/[email protected]"
with:
python-version: "${{ inputs.python-version }}"
python-version-file: "${{ inputs.python-version-file }}"

- name: "Install poetry"
shell: "bash"
run: "pipx install --python='${{ steps.python.outputs.python-path }}' poetry==${{ inputs.poetry-version }}"

- name: "Cache venv"
uses: "actions/[email protected]"
with:
path: "./.venv/"
key: "venv-${{ runner.os }}-${{ steps.python.outputs.python-version }}-${{ hashFiles('poetry.lock') }}"
23 changes: 23 additions & 0 deletions .github/actions/run_pre_commit/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: "Run pre-commit hooks"
description: "Composite action to install project, setup mypy cache and run pre-commit hooks."

inputs:
python-version:
description: "Python version to use"
required: true

runs:
using: "composite"
steps:
- name: "Install package"
run: "poetry install"
shell: "bash"

- name: "Cache mypy"
uses: "actions/[email protected]"
with:
path: "./.mypy_cache/"
key: "mypy-${{ runner.os }}-${{ inputs.python-version }}"

- name: "Run pre-commit"
uses: "pre-commit/[email protected]"
53 changes: 53 additions & 0 deletions .github/actions/run_tests/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: "Run pre-commit hooks"
description: "Composite action to install tox & tox-gh-actions and run tests via tox."

inputs:
python-path:
description: "Path to Python executable"
required: true
python-version:
description: "Installed Python version"
required: true
tox-version:
description: "Tox version to use"
required: false
default: "3.25.0"
tox-gh-actions-version:
description: "Tox GitHub Actions plugin version to use"
required: false
default: "2.9.1"
use-coveralls:
description: "Send coverage to Coveralls"
required: false
default: "false"
coveralls-token:
description: "GitHub token to use, when sending coverage to Coveralls"
required: false

runs:
using: "composite"
steps:
- name: "Install tox & tox-gh-actions"
run: |
set -euo pipefail
pipx install --python='${{ inputs.python-path }}' tox==${{ inputs.tox-version }}
pipx inject tox tox-gh-actions==${{ inputs.tox-gh-actions-version }}
shell: "bash"

- name: "Cache tox"
uses: "actions/[email protected]"
with:
path: "./.tox/"
key: "tox-${{ inputs.python-version }}"

- name: "Run tests"
run: "tox"
shell: "bash"

- name: "Send coverage to Coveralls"
if: "${{ inputs.use-coveralls == 'true' }}"
env:
GITHUB_TOKEN: "${{ inputs.coveralls-token }}"
run: "poetry run python3 -m coveralls --service=github"
shell: "bash"
135 changes: 21 additions & 114 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,6 @@ on:

env:
PYTHONUNBUFFERED: "1"
# Get latest versions by running,
#
# ```bash
# pip-latest-release poetry tox tox-gh-actions twine
# ```
POETRY_VERSION: "1.1.13"
TOX_VERSION: "3.25.0"
TOX_GH_ACTIONS_VERSION: "2.9.1"
TWINE_VERSION: "4.0.1"

jobs:
dev:
Expand All @@ -36,22 +27,10 @@ jobs:
steps:
- uses: "actions/[email protected]"

- name: "Install Python"
uses: "actions/[email protected]"
- uses: "./.github/actions/install_python_and_poetry"
with:
python-version-file: ".python-version"

- name: "Install poetry"
uses: "snok/[email protected]"
with:
version: "${{ env.POETRY_VERSION }}"

- name: "Cache venv"
uses: "actions/[email protected]"
with:
path: "./.venv/"
key: "venv-dev-${{ hashFiles('poetry.lock') }}"

- name: "Install package"
run: "poetry install"

Expand All @@ -77,112 +56,52 @@ jobs:
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11.0-beta.3"]
include:
- python-version: "3.7"
python-cache-key: "3.7"

- python-version: "3.8"
python-cache-key: "3.8"

- python-version: "3.9"
python-cache-key: "3.9"

- python-version: "3.10"
python-cache-key: "dev"

- python-version: "3.11.0-beta.3"
python-cache-key: "3.11.0-beta.3"
dev: "true"

runs-on: "ubuntu-latest"
continue-on-error: "${{ startsWith(matrix.python-version, '3.11') }}"

steps:
- uses: "actions/[email protected]"

- name: "Install Python"
uses: "actions/[email protected]"
- id: "python_and_poetry"
uses: "./.github/actions/install_python_and_poetry"
with:
python-version: "${{ matrix.python-version }}"

- name: "Install poetry"
uses: "snok/[email protected]"
with:
version: "${{ env.POETRY_VERSION }}"

- name: "Install other deps"
run: "python3 -m pip install tox==${{ env.TOX_VERSION }} tox-gh-actions==${{ env.TOX_GH_ACTIONS_VERSION }}"

- name: "Cache venv"
uses: "actions/[email protected]"
- if: "${{ matrix.dev == 'true' }}"
uses: "./.github/actions/run_pre_commit"
with:
path: "./.venv/"
key: "venv-${{ matrix.python-cache-key }}-${{ hashFiles('poetry.lock') }}"

- name: "Install package with dev dependencies"
if: "${{ matrix.python-cache-key == 'dev' }}"
run: "poetry install"

- name: "Install package without dev dependencies"
if: "${{ matrix.python-cache-key != 'dev' }}"
run: "poetry install --no-dev"

- name: "Run pre-commit"
if: "${{ matrix.python-cache-key == 'dev' }}"
uses: "pre-commit/[email protected]"
python-version: "${{ steps.python_and_poetry.outputs.python-version }}"

- name: "Setup git for test needs"
run: |
set -euo pipefail
git config --global init.defaultBranch main
git config --global user.name badabump-release-bot[bot]
git config --global user.email badabump-release-bot[bot]@users.noreply.github.com
- name: "Run tests"
run: "python3 -m tox"

- name: "Send report to coveralls"
if: "${{ matrix.python-cache-key == 'dev' }}"
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
run: "poetry run python3 -m coveralls --service=github"
- uses: "./.github/actions/run_tests"
with:
python-path: "${{ steps.python_and_poetry.outputs.python-path }}"
python-version: "${{ steps.python_and_poetry.outputs.python-version }}"
use-coveralls: "${{ matrix.dev }}"
coveralls-token: "${{ secrets.GITHUB_TOKEN }}"

package:
needs: ["test"]
name: "Build & deploy package"

runs-on: "ubuntu-latest"

steps:
- uses: "actions/[email protected]"

- name: "Install Python"
uses: "actions/[email protected]"
with:
python-version-file: ".python-version"

- name: "Install poetry"
uses: "snok/[email protected]"
with:
version: "${{ env.POETRY_VERSION }}"

- name: "Build package"
run: "poetry build"

- name: "Check package"
run: |
set -euo pipefail
python3 -m pip install twine==${{ env.TWINE_VERSION }}
python3 -m twine check ./dist/*
- name: "Publish package"
if: "${{ github.event_name == 'push' && startsWith(github.event.ref, 'refs/tags') }}"
uses: "pypa/[email protected]"
with:
user: "${{ secrets.PYPI_USERNAME }}"
password: "${{ secrets.PYPI_PASSWORD }}"
uses: "./.github/workflows/ci_package.yml"
secrets:
PYPI_USERNAME: "${{ secrets.PYPI_USERNAME }}"
PYPI_PASSWORD: "${{ secrets.PYPI_PASSWORD }}"

release:
needs: ["package"]
if: "${{ startsWith(github.ref, 'refs/tags/') }}"

name: "Create GitHub release"

runs-on: "ubuntu-latest"
Expand All @@ -197,22 +116,10 @@ jobs:
git fetch --depth=1 origin +refs/tags/*:refs/tags/*
git fetch --prune --unshallow
- name: "Install Python"
uses: "actions/[email protected]"
- uses: "./.github/actions/install_python_and_poetry"
with:
python-version-file: ".python-version"

- name: "Install poetry"
uses: "snok/[email protected]"
with:
version: "${{ env.POETRY_VERSION }}"

- name: "Cache venv"
uses: "actions/[email protected]"
with:
path: ".venv"
key: "venv-dev-${{ hashFiles('poetry.lock') }}"

- name: "Install package without dev dependencies"
run: "poetry install --no-dev"

Expand Down
48 changes: 48 additions & 0 deletions .github/workflows/ci_package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: "Build & deploy package"

on:
workflow_call:
inputs:
twine-version:
description: "Twine version to use."
type: "string"
required: false
default: "4.0.1"

secrets:
PYPI_USERNAME:
description: "PyPI username to use."
required: true
PYPI_PASSWORD:
description: "PyPI password (token) to use."
required: true

jobs:
package:
name: "Build & deploy package"

runs-on: "ubuntu-latest"

steps:
- uses: "actions/[email protected]"

- id: "python_and_poetry"
uses: "./.github/actions/install_python_and_poetry"
with:
python-version-file: ".python-version"

- name: "Build package"
run: "poetry build"

- name: "Install twine"
run: "pipx install --python='${{ steps.python_and_poetry.outputs.python-path }}' twine==${{ inputs.twine-version }}"

- name: "Check package"
run: "twine check ./dist/*"

- name: "Publish package"
if: "${{ startsWith(github.ref, 'refs/tags/') }}"
uses: "pypa/[email protected]"
with:
user: "${{ secrets.PYPI_USERNAME }}"
password: "${{ secrets.PYPI_PASSWORD }}"
Loading

0 comments on commit 0a7843c

Please sign in to comment.