Enhance CI workflow and refine project configuration #56
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Python package | |
on: | |
push: | |
branches: [main] | |
pull_request: | |
branches: [main] | |
workflow_dispatch: | |
inputs: | |
bump_type: | |
description: 'Version bump type' | |
required: true | |
default: 'patch' | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: | |
python-version: ['3.9', '3.10', '3.11', '3.12'] | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
token: ${{ secrets.GITHUB_TOKEN }} | |
- name: Set up Python | |
uses: actions/setup-python@v5 | |
with: | |
python-version: ${{ matrix.python-version }} | |
- name: Install Poetry | |
run: pip install poetry | |
- name: Cache Poetry virtual environment | |
uses: actions/cache@v4 | |
with: | |
path: ~/.cache/pypoetry | |
key: ${{ runner.os }}-poetry-${{ hashFiles('**/pyproject.toml') }} | |
restore-keys: ${{ runner.os }}-poetry- | |
- name: Install dependencies | |
run: poetry install | |
- name: Lint (flake8) | |
run: poetry run flake8 datasafari tests | |
- name: Run tests with coverage (pytest) | |
run: poetry run pytest --cov=datasafari --cov-report=xml | |
- name: Build the package | |
run: poetry build | |
- name: Check the package (Twine) | |
run: poetry run twine check dist/* | |
- name: Static security scan (Bandit) | |
run: poetry run bandit -c pyproject.toml -r . | |
- name: Scan for dependency vulnerabilities (SafetyCLI 3) | |
uses: pyupio/[email protected] | |
with: | |
api-key: ${{ secrets.SAFETY_CLI_API_KEY }} | |
- name: Install SSH key | |
uses: webfactory/[email protected] | |
with: | |
ssh-private-key: ${{ secrets.DS_SERVER_SSH_PRIVATE_KEY }} | |
- name: Add SSH key to known hosts | |
run: | | |
mkdir -p ~/.ssh | |
echo "${{ secrets.DS_SERVER_HOST_KEY_RSA }}" >> ~/.ssh/known_hosts | |
echo "${{ secrets.DS_SERVER_HOST_KEY_ECDSA }}" >> ~/.ssh/known_hosts | |
echo "${{ secrets.DS_SERVER_HOST_KEY_ED25519 }}" >> ~/.ssh/known_hosts | |
- name: Configure Git Identity | |
run: | | |
git config --global user.email "[email protected]" | |
git config --global user.name "GitHub Actions" | |
- name: Determine Bump Type | |
run: | | |
set -e # Ensure the script fails on error | |
set -x # Debug: Print each command before executing | |
TAG=$(git describe --tags $(git rev-list --tags --max-count=1)) | |
echo "Described tag: $TAG" | |
if [[ "$TAG" == *"-patch" ]]; then | |
BUMP_TYPE="patch" | |
elif [[ "$TAG" == *"-minor" ]]; then | |
BUMP_TYPE="minor" | |
elif [[ "$TAG" == *"-major" ]]; then | |
BUMP_TYPE="major" | |
else | |
BUMP_TYPE="patch" # Default to patch if no specific tag | |
fi | |
echo "Bump Type: $BUMP_TYPE" | |
echo "bump_type=$BUMP_TYPE" >> $GITHUB_ENV | |
set +x # Turn off debugging | |
- name: Bump Version (bump2version) | |
run: poetry run bump2version --config-file .bumpversion.cfg ${{ env.bump_type }} --no-tag --commit | |
- name: Build documentation (Sphinx) | |
run: poetry run sphinx-build -b html docs/ docs/_build/html | |
- name: Sync documentation to server | |
run: rsync -avz --delete -e "ssh -p ${{ secrets.DS_SERVER_SSH_PORT }}" --verbose ./docs/_build/html/ ${{ secrets.DS_SERVER_USERNAME }}@${{ secrets.DS_SERVER_IP }}:${{ secrets.DS_SERVER_DOCS_PATH }} | |
env: | |
RSYNC_RSH: ssh -o StrictHostKeyChecking=accept-new | |
- name: Create Release Tag | |
if: github.event_name == 'push' && startsWith(github.ref, 'refs/heads/') | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
const newTag = `v${{ steps.bump2version.outputs.new_version }}`; | |
const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/'); | |
const { execSync } = require('child_process'); | |
execSync(`git config --global user.email "[email protected]"`); | |
execSync(`git config --global user.name "GitHub Actions"`); | |
execSync(`git tag ${newTag}`); | |
execSync(`git push origin ${newTag}`); | |
console.log(`Tag ${newTag} created and pushed`); | |
- name: Publish to PyPI | |
if: startsWith(github.ref, 'refs/tags/') | |
env: | |
TWINE_USERNAME: __token__ | |
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }} | |
run: poetry publish --username $TWINE_USERNAME --password $TWINE_PASSWORD |