295 lines
11 KiB
YAML
295 lines
11 KiB
YAML
name: Pytest CI Workflow
|
|
description: Run pytest with coverage and report upload to Gitea
|
|
inputs:
|
|
absolute_workspace_path:
|
|
description: "Path from main repo"
|
|
required: true
|
|
|
|
ci-branch:
|
|
description: 'Target branch where test reports will be committed'
|
|
required: true
|
|
|
|
module-dir:
|
|
description: 'Directory (folder) containing the Python module to test'
|
|
required: true
|
|
|
|
report-dir:
|
|
description: 'Directory (folder) where all test and coverage reports will be saved'
|
|
required: true
|
|
|
|
gitea-domain:
|
|
description: 'Domain name of your Gitea instance without protocol (e.g., gitea.psi.ch)'
|
|
required: true
|
|
|
|
repo-path:
|
|
description: 'Relative path to the root of your repository'
|
|
required: true
|
|
|
|
ci-token:
|
|
description: ' CI authentication token (used for pushing to Gitea)'
|
|
required: true
|
|
runs:
|
|
using: "composite"
|
|
steps:
|
|
- name: Checkout code
|
|
uses: actions/checkout@v4
|
|
with:
|
|
fetch-depth: 1
|
|
|
|
- name: Symlink conftest.py into module-dir
|
|
shell: bash
|
|
run: |
|
|
ln -sf "$GITHUB_WORKSPACE/test-ci/conftest.py" ${{ inputs.module-dir }}/conftest.py
|
|
|
|
- name: Install Pixi & project dependencies
|
|
shell: bash
|
|
run: |
|
|
echo "🔧 Installing Pixi..."
|
|
curl -fsSL https://pixi.sh/install.sh | bash > pixi-download.log 2>&1 || {
|
|
echo "❌ Pixi download failed"
|
|
cat pixi-download.log
|
|
exit 1
|
|
}
|
|
|
|
echo "$HOME/.pixi/bin" >> $GITHUB_PATH
|
|
export PATH="$HOME/.pixi/bin:$PATH"
|
|
|
|
if [ ! -f "pixi.toml" ]; then
|
|
echo "❌ Missing pixi.toml at repo root : please add one to use Pixi"
|
|
exit 1
|
|
fi
|
|
|
|
echo "📦 Installing dependencies from pixi.toml..."
|
|
pixi install > pixi-install.log 2>&1 || {
|
|
echo "❌ Pixi project dependencies failed"
|
|
cat pixi-install.log
|
|
exit 1
|
|
}
|
|
|
|
echo "✅ Pixi installed and environment created"
|
|
pixi list
|
|
|
|
- name: Install Python test/report tools
|
|
shell: bash
|
|
run: |
|
|
echo "🐍 Setting up Python environment..."
|
|
python -m venv venv
|
|
source venv/bin/activate
|
|
|
|
pip install -U pip > pip.log 2>&1 || {
|
|
echo "❌ pip upgrade failed"
|
|
cat pip.log
|
|
exit 1
|
|
}
|
|
|
|
pip install pytest pytest-cov pytest-html pytest-md-report rich > pip.log 2>&1 || {
|
|
echo "❌ Python testing tools install failed"
|
|
cat pip.log
|
|
exit 1
|
|
}
|
|
|
|
echo "✅ Python tools installed"
|
|
pip list
|
|
|
|
- name: Run tests and generate reports
|
|
shell: bash
|
|
run: |
|
|
echo "🚀 Running tests and generating reports..."
|
|
source venv/bin/activate
|
|
mkdir -p ${{ inputs.report-dir }}/{allure,coverage,markdown}
|
|
|
|
set +e
|
|
|
|
echo "🧪 Running pytest with coverage and report generation..."
|
|
pixi run pytest . \
|
|
--continue-on-collection-errors \
|
|
--cov=${{ inputs.module-dir }} \
|
|
--cov-report=xml:${{ inputs.report-dir }}/coverage/coverage.xml \
|
|
--cov-report=html:${{ inputs.report-dir }}/coverage/ \
|
|
--json-report \
|
|
--json-report-file=${{ inputs.report-dir }}/markdown/pytest-report.json \
|
|
--capture=no > ${{ inputs.report-dir }}/markdown/raw-test-output.log 2>&1
|
|
|
|
exit_code=$?
|
|
|
|
set -e
|
|
|
|
echo ""
|
|
echo "📋 Short test summary:"
|
|
awk '/=+ short test summary info =+/,/=+.* in .*s =+/' ${{ inputs.report-dir }}/markdown/raw-test-output.log || true
|
|
|
|
[ -f ${{ inputs.report-dir }}/markdown/runtime_params.json ] || echo "❌ runtime_params.json not found to get tests parameters"
|
|
|
|
echo "📦 Pytest exit code: $exit_code"
|
|
|
|
echo ""
|
|
echo "📈 Generating coverage summary..."
|
|
coverage report --format=markdown > ${{ inputs.report-dir }}/markdown/coverage-summary.md 2> ${{ inputs.report-dir }}/markdown/coverage-error.log && {
|
|
echo "✅ Coverage summary generated"
|
|
} || {
|
|
echo "⚠️ Coverage summary generation failed"
|
|
echo "🪵 Extracting coverage error:"
|
|
cat ${{ inputs.report-dir }}/markdown/coverage-error.log
|
|
}
|
|
|
|
echo ""
|
|
echo "📊 Generating tests markdown summary..."
|
|
python test-ci/json_to_md.py \
|
|
--input "${{ inputs.report-dir }}/markdown/pytest-report.json" \
|
|
--output "${{ inputs.report-dir }}/markdown/TEST-REPORT.md" \
|
|
--log "${{ inputs.report-dir }}/markdown/raw-test-output.log" \
|
|
--params "${{ inputs.report-dir }}/markdown/runtime_params.json" \
|
|
--exit-code $exit_code > "${{ inputs.report-dir }}/markdown/md-report.log" 2>&1 && {
|
|
echo "✅ Tests markdown generated"
|
|
} || {
|
|
echo "⚠️ Tests markdown generation failed"
|
|
echo "🪵 Extracting markdown generation error:"
|
|
cat ${{ inputs.report-dir }}/markdown/md-report.log
|
|
}
|
|
|
|
echo ""
|
|
echo "🌳 Generating JSON tree view..."
|
|
python test-ci/json_to_tree.py \
|
|
"${{ inputs.report-dir }}/markdown/pytest-report.json" \
|
|
> "${{ inputs.report-dir }}/markdown/json-tree-gen.log" 2>&1 && {
|
|
echo "✅ JSON tree view generated"
|
|
} || {
|
|
echo "❌ JSON tree generation failed"
|
|
cat ${{ inputs.report-dir }}/markdown/json-tree-gen.log
|
|
}
|
|
|
|
echo "🧹 Cleaning up ${{ inputs.report-dir }}/markdown..."
|
|
find ${{ inputs.report-dir }}/markdown -type f ! \( \
|
|
-name 'coverage-summary.md' -o \
|
|
-name 'json-tree-view.txt' -o \
|
|
-name 'pytest-report.json' -o \
|
|
-name 'runtime_params.json' -o \
|
|
-name 'TEST-REPORT.md' \
|
|
\) -delete
|
|
|
|
|
|
|
|
|
|
- name: Commit and push reports
|
|
shell: bash
|
|
run: |
|
|
echo "📤 Committing test reports..."
|
|
git config user.name "ci-bot"
|
|
git config user.email "ci-bot@example.com"
|
|
git add ${{ inputs.report-dir }}/
|
|
git commit -m "CI: update test report and coverage files" || echo "⚠️ Nothing to commit"
|
|
|
|
echo "🚀 Pushing to branch '${{ inputs.ci-branch }}'..."
|
|
git push https://ci-token:${{ inputs.ci-token }}@${{ inputs.gitea-domain }}/${{ inputs.repo-path }}.git HEAD:${{ inputs.ci-branch }} > push.log 2>&1 || {
|
|
echo "❌ Push failed"
|
|
cat push.log
|
|
exit 1
|
|
}
|
|
echo "✅ Reports pushed"
|
|
|
|
- name: Get commit info
|
|
id: info
|
|
shell: bash
|
|
run: |
|
|
short_sha=$(git rev-parse --short HEAD)
|
|
commit_msg=$(git log -1 --pretty=%B)
|
|
|
|
echo "🔍 Commit SHA: $short_sha"
|
|
echo "🔍 Commit message: $commit_msg"
|
|
|
|
echo "short_sha=$short_sha" >> $GITHUB_OUTPUT
|
|
echo "commit_msg=$commit_msg" >> $GITHUB_OUTPUT
|
|
|
|
|
|
# - name: Clone Gitea Wiki
|
|
# shell: bash
|
|
# env:
|
|
# CI_TOKEN: ${{ inputs.ci-token }}
|
|
# run: |
|
|
# REPO_NAME="${{ github.repository }}"
|
|
# DOMAIN="${{ inputs.gitea-domain }}"
|
|
# WIKI_REPO="https://ci-token:${CI_TOKEN}@${DOMAIN}/${REPO_NAME}.wiki.git"
|
|
#
|
|
# echo "📚 Cloning wiki repo: $WIKI_REPO"
|
|
# git clone "$WIKI_REPO" wiki > wiki_clone.log 2>&1 || {
|
|
# echo "❌ Wiki clone failed"
|
|
# cat wiki_clone.log
|
|
# exit 1
|
|
# }
|
|
# echo "✅ Wiki cloned to ./wiki"
|
|
#
|
|
# - name: Copy reports into wiki
|
|
# shell: bash
|
|
# run: |
|
|
# SHORT_SHA=$(git rev-parse --short HEAD)
|
|
# REPO_URL="https://${{ inputs.gitea-domain }}/${{ github.repository }}"
|
|
# RUN_LINK="${REPO_URL}/actions/runs/${GITHUB_RUN_NUMBER}"
|
|
#
|
|
# TEST_FILE="run-${GITHUB_RUN_NUMBER}-TEST-commit-${SHORT_SHA}.md"
|
|
# COVERAGE_FILE="run-${GITHUB_RUN_NUMBER}-COVERAGE-commit-${SHORT_SHA}.md"
|
|
#
|
|
# echo "## Test Report" > "wiki/${TEST_FILE}"
|
|
# echo "[View CI Run ${GITHUB_RUN_NUMBER}](${RUN_LINK}) | [Commit ${SHORT_SHA}](${REPO_URL}/commit/${SHORT_SHA})" >> "wiki/${TEST_FILE}"
|
|
# cat ${{ inputs.report-dir }}/markdown/TEST-REPORT.md >> "wiki/${TEST_FILE}"
|
|
#
|
|
# echo "## Coverage Report" > "wiki/${COVERAGE_FILE}"
|
|
# echo "[View CI Run ${GITHUB_RUN_NUMBER}](${RUN_LINK}) | [Commit ${SHORT_SHA}](${REPO_URL}/commit/${SHORT_SHA})" >> "wiki/${COVERAGE_FILE}"
|
|
# cat ${{ inputs.report-dir }}/markdown/coverage-summary.md >> "wiki/${COVERAGE_FILE}"
|
|
#
|
|
# HOME_FILE="wiki/Home.md"
|
|
# TEMP_CONTENT=$(mktemp)
|
|
# REPORT_LOG="reports.log"
|
|
#
|
|
# {
|
|
# echo "# Test Reports History"
|
|
# echo ""
|
|
# echo "## Latest Run"
|
|
# echo "- [run ${GITHUB_RUN_NUMBER} TEST commit ${SHORT_SHA}](${TEST_FILE})"
|
|
# echo "- [run ${GITHUB_RUN_NUMBER} COVERAGE commit ${SHORT_SHA}](${COVERAGE_FILE})"
|
|
# echo ""
|
|
# echo "## Previous Runs"
|
|
#
|
|
# if [[ -f "${HOME_FILE}" && -s "${HOME_FILE}" ]]; then
|
|
# sed -n '/## Latest Run/{n;p;n;p}' "${HOME_FILE}"
|
|
# sed -n '/## Previous Runs/,$p' "${HOME_FILE}" | tail -n +2 | grep -v "^#" || true
|
|
# fi
|
|
# } > "${TEMP_CONTENT}" 2> "${REPORT_LOG}" || {
|
|
# echo "❌ Report preparation failed"
|
|
# cat "${REPORT_LOG}"
|
|
# exit 1
|
|
# }
|
|
#
|
|
# mv "${TEMP_CONTENT}" "${HOME_FILE}"
|
|
#
|
|
# - name: Commit and push wiki
|
|
# shell: bash
|
|
# env:
|
|
# CI_TOKEN: ${{ inputs.ci-token }}
|
|
# run: |
|
|
# echo "🚀 Pushing wiki changes..."
|
|
# cd wiki
|
|
#
|
|
# git config user.name "ci-bot"
|
|
# git config user.email "ci-bot@example.com"
|
|
# git add .
|
|
#
|
|
# if git commit -m "Update wiki with run ${GITHUB_RUN_NUMBER}"; then
|
|
# REPO_NAME="${{ github.repository }}"
|
|
# DOMAIN="${{ inputs.gitea-domain }}"
|
|
# WIKI_REPO="https://ci-token:${CI_TOKEN}@${DOMAIN}/${REPO_NAME}.wiki.git"
|
|
# git push "$WIKI_REPO" HEAD:main
|
|
# echo "✅ Wiki updated successfully"
|
|
# else
|
|
# echo "⚠️ No changes to commit"
|
|
# fi
|
|
#
|
|
# - name: Log wiki report URLs
|
|
# shell: bash
|
|
# run: |
|
|
# REPO_URL="https://${{ inputs.gitea-domain }}/${{ github.repository }}"
|
|
# SHORT_SHA=$(git rev-parse --short HEAD)
|
|
# echo "🔗 Wiki Test Report URL:"
|
|
# echo " ${REPO_URL}/wiki/run-${GITHUB_RUN_NUMBER}-TEST-commit-${SHORT_SHA}.md"
|
|
# echo "🔗 Wiki Coverage Report URL:"
|
|
# echo " ${REPO_URL}/wiki/run-${GITHUB_RUN_NUMBER}-COVERAGE-commit-${SHORT_SHA}.md"
|