270 lines
10 KiB
YAML
270 lines
10 KiB
YAML
name: Pytest CI Workflow
|
|
description: Run pytest with coverage and report upload to Gitea
|
|
inputs:
|
|
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
|
|
|
|
enable_pixi_post_link_scripts:
|
|
description: 'Set to true to enable post-link scripts for Pixi'
|
|
required: false
|
|
default: 'false'
|
|
|
|
runs:
|
|
using: "composite"
|
|
steps:
|
|
- name: Checkout code
|
|
uses: actions/checkout@v4
|
|
with:
|
|
fetch-depth: 1
|
|
|
|
- name: Install Pixi & project dependencies
|
|
shell: bash
|
|
run: |
|
|
OUTPUT_DIR="outputs"
|
|
LOG_FILE="${OUTPUT_DIR}/pixi.log"
|
|
|
|
mkdir -p "$OUTPUT_DIR"
|
|
|
|
if [ "${{ inputs.enable_pixi_post_link_scripts }}" == "true" ]; then
|
|
pixi config set --local run-post-link-scripts insecure
|
|
fi
|
|
|
|
echo "🔧 Installing Pixi..." >> $LOG_FILE
|
|
curl -fsSL https://pixi.sh/install.sh | bash >> $LOG_FILE 2>&1 || {
|
|
echo "❌ Pixi download failed" >> $LOG_FILE
|
|
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" >> $LOG_FILE
|
|
echo "Exit Code: 1" >> $LOG_FILE
|
|
exit 1
|
|
fi
|
|
|
|
echo "📦 Installing dependencies from pixi.toml..." >> $LOG_FILE
|
|
pixi install >> $LOG_FILE 2>&1 || {
|
|
echo "❌ Pixi project dependencies failed" >> $LOG_FILE
|
|
echo "Exit Code: 1" >> $LOG_FILE
|
|
exit 1
|
|
}
|
|
|
|
echo "✅ Pixi installed and environment created" >> $LOG_FILE
|
|
pixi list >> $LOG_FILE
|
|
echo "Exit Code: 0" >> $LOG_FILE
|
|
|
|
cat $LOG_FILE
|
|
|
|
- name: Run tests and generate reports
|
|
shell: bash
|
|
run: |
|
|
LOG_FILE="outputs/test-ci.log"
|
|
|
|
echo "🚀 Running tests and generating reports..." >> $LOG_FILE
|
|
git clone -q https://gitea.psi.ch/tligui_y/test-ci.git ./temp-ci
|
|
cp temp-ci/conftest.py
|
|
|
|
mkdir -p ${{ inputs.report-dir }}/{markdown}
|
|
|
|
set +e
|
|
|
|
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 }}/outputs/pytest-report.json \
|
|
--capture=no >> ${{ inputs.report-dir }}/outputs/full-output.log 2>&1
|
|
|
|
awk '/=+ test session starts =+/{flag=1} /-+ JSON report -+/{flag=0} flag' ${{ inputs.report-dir }}/outputs/long-test-output.log
|
|
awk '/=+ short test summary info =+/,/=+.* in .*s =+/' ${{ inputs.report-dir }}/outputs/short-test-output.log
|
|
|
|
exit_code=$?
|
|
|
|
set -e
|
|
|
|
echo "" >> $LOG_FILE
|
|
echo "📋 Short test summary:" >> $LOG_FILE
|
|
cat ${{ inputs.report-dir }}/outputs/short-test-output.log >> $LOG_FILE || true
|
|
|
|
echo "" >> $LOG_FILE
|
|
echo "📈 Generating coverage summary..." >> $LOG_FILE
|
|
coverage report --format=markdown >> ${{ inputs.report-dir }}/markdown/coverage-summary.md 2>> ${{ inputs.report-dir }}/markdown/coverage-error.log && {
|
|
echo "✅ Coverage summary generated" >> $LOG_FILE
|
|
} || {
|
|
echo "❌ Coverage summary generation failed" >> $LOG_FILE
|
|
echo "🪵 Extracting coverage error:" >> $LOG_FILE
|
|
cat ${{ inputs.report-dir }}/markdown/coverage-error.log >> $LOG_FILE
|
|
echo "Exit Code: 1" >> $LOG_FILE
|
|
exit 1
|
|
}
|
|
|
|
python -m venv venv
|
|
source venv/bin/activate
|
|
|
|
echo "" >> $LOG_FILE
|
|
echo "📊 Generating tests markdown summary..." >> $LOG_FILE
|
|
|
|
python ./temp-ci/json_to_md.py \
|
|
--input "${{ inputs.report-dir }}/markdown/pytest-report.json" \
|
|
--output "${{ inputs.report-dir }}/markdown/TEST-REPORT.md" \
|
|
--log_long "${{ inputs.report-dir }}/markdown/raw-test-output.log" \
|
|
--log_short "${{ inputs.report-dir }}/markdown/short-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" >> $LOG_FILE
|
|
} || {
|
|
echo "❌ Tests markdown generation failed" >> $LOG_FILE
|
|
echo "🪵 Extracting markdown generation error:" >> $LOG_FILE
|
|
cat ${{ inputs.report-dir }}/markdown/md-report.log >> $LOG_FILE
|
|
echo "Exit Code: 1" >> $LOG_FILE
|
|
exit 1
|
|
}
|
|
|
|
echo "" >> $LOG_FILE
|
|
echo "🌳 Generating JSON tree view..." >> $LOG_FILE
|
|
python ./temp-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" >> $LOG_FILE
|
|
} || {
|
|
echo "❌ JSON tree generation failed" >> $LOG_FILE
|
|
echo "🪵 Extracting json file error:" >> $LOG_FILE
|
|
cat ${{ inputs.report-dir }}/markdown/json-tree-gen.log >> $LOG_FILE
|
|
echo "Exit Code: 1" >> $LOG_FILE
|
|
exit 1
|
|
}
|
|
|
|
rm -rf ./temp-test-ci
|
|
echo "Exit Code: 0" >> $LOG_FILE
|
|
|
|
- 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"
|
|
|
|
LOG_FILE="outputs/wiki-clone.log"
|
|
|
|
echo "📚 Cloning wiki repo: $WIKI_REPO" >> $LOG_FILE
|
|
git clone "$WIKI_REPO" wiki >> wiki_clone.log || {
|
|
echo "❌ Wiki clone failed" >> $LOG_FILE
|
|
cat wiki_clone.log >> $LOG_FILE
|
|
echo "Exit Code: 1" >> $LOG_FILE
|
|
exit 1
|
|
}
|
|
echo "✅ Wiki cloned to ./wiki" >> $LOG_FILE
|
|
|
|
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"
|
|
|
|
LOG_FILE="outputs/copy-reports.log"
|
|
|
|
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 "❌ Wiki report preparation failed" >> $LOG_FILE
|
|
cat "${REPORT_LOG}" >> $LOG_FILE
|
|
echo "Exit Code: 1" >> $LOG_FILE
|
|
exit 1
|
|
}
|
|
echo "✅ Wiki report preparation succeded" >> $LOG_FILE
|
|
|
|
mv "${TEMP_CONTENT}" "${HOME_FILE}"
|
|
echo "Exit Code: 0" >> $LOG_FILE
|
|
|
|
- name: Commit and push wiki
|
|
shell: bash
|
|
env:
|
|
CI_TOKEN: ${{ inputs.ci-token }}
|
|
run: |
|
|
LOG_FILE="outputs/commit-push-wiki.log"
|
|
echo "🚀 Pushing wiki changes..." >> $LOG_FILE
|
|
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 >> git.log 2>&1 || {
|
|
echo "❌ Push failed: Unable to push wiki changes" >> $LOG_FILE
|
|
cat git.log >> $LOG_FILE
|
|
echo "Exit Code: 1" >> $LOG_FILE
|
|
exit 1
|
|
}
|
|
echo "✅ Wiki updated successfully" >> $LOG_FILE
|
|
else
|
|
echo "⚠️ No changes to commit" >> $LOG_FILE
|
|
fi
|
|
echo "Exit Code: 0" >> $LOG_FILE
|
|
|
|
- name: Log wiki report URLs
|
|
shell: bash
|
|
run: |
|
|
LOG_FILE="outputs/wiki-report-urls.log"
|
|
|
|
REPO_URL="https://${{ inputs.gitea-domain }}/${{ github.repository }}"
|
|
SHORT_SHA=$(git rev-parse --short HEAD)
|
|
echo "🔗 Wiki Test Report URL:" >> $LOG_FILE
|
|
echo " ${REPO_URL}/wiki/run-${GITHUB_RUN_NUMBER}-TEST-commit-${SHORT_SHA}.md" >> $LOG_FILE
|
|
echo "🔗 Wiki Coverage Report URL:" >> $LOG_FILE
|
|
echo " ${REPO_URL}/wiki/run-${GITHUB_RUN_NUMBER}-COVERAGE-commit-${SHORT_SHA}.md" >> $LOG_FILE
|