Skip to content

feat: Github pages

feat: Github pages #4

name: Run Tests and Update Documentation
on:
push:
branches:
- main
- master
jobs:
test-and-update-docs:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
fetch-depth: 0
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.12"
cache: "pip"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -e ".[dev]"
pip install pytest-cov
- name: Run tests with coverage
run: |
python -m pytest --cov=src tests/ -v > test_results.txt
python -m pytest --cov=src --cov-report=xml tests/
- name: Generate coverage badge
run: |
COVERAGE=$(python -c "import xml.etree.ElementTree as ET; print(ET.parse('coverage.xml').getroot().attrib['line-rate'])")
COVERAGE_PCT=$(python -c "print(round(float('${COVERAGE}') * 100, 2))")
echo "COVERAGE_PCT=${COVERAGE_PCT}" >> $GITHUB_ENV
# Determine color based on coverage percentage
if (( $(echo "${COVERAGE_PCT} >= 90" | bc -l) )); then
BADGE_COLOR="brightgreen"
elif (( $(echo "${COVERAGE_PCT} >= 80" | bc -l) )); then
BADGE_COLOR="green"
elif (( $(echo "${COVERAGE_PCT} >= 70" | bc -l) )); then
BADGE_COLOR="yellowgreen"
elif (( $(echo "${COVERAGE_PCT} >= 60" | bc -l) )); then
BADGE_COLOR="yellow"
else
BADGE_COLOR="red"
fi
# Save the badge with the color
echo "[![Coverage](https://img.shields.io/badge/Coverage-${COVERAGE_PCT}%25-${BADGE_COLOR})]()" > coverage_badge.txt
- name: Update README.md with test results and coverage
run: |
# For README.md
if grep -q "## Test Results" README.md; then
# Remove everything from "## Test Results" to the end of the file
sed -i '/## Test Results/,$d' README.md
fi
# Add the new content
echo -e "\n## Test Results\n\n\`\`\`" >> README.md
cat test_results.txt >> README.md
echo -e "\`\`\`\n" >> README.md
cat coverage_badge.txt >> README.md
- name: Update src/index.md with test results and coverage
run: |
# For src/index.md
if grep -q "## Test Results" src/index.md; then
# Remove everything from "## Test Results" to the end of the file
sed -i '/## Test Results/,$d' src/index.md
fi
# Add the new content
echo -e "\n## Test Results\n\n\`\`\`" >> src/index.md
cat test_results.txt >> src/index.md
echo -e "\`\`\`\n" >> src/index.md
cat coverage_badge.txt >> src/index.md
- name: Commit and push changes
run: |
git config --global user.name "GitHub Actions Bot"
git config --global user.email "github-actions-bot@users.noreply.github.com"
git add README.md src/index.md
git commit -m "Update test results and coverage [skip ci]" || echo "No changes to commit"
git push