Skip to content

test-and-report

test-and-report #5

name: test-and-report
on:
workflow_dispatch:
inputs:
node_versions:
description: 'JSON list of Node.js versions'
required: false
default: '["22.x"]'
type: string
test_script:
description: 'npm script to run tests'
required: false
default: 'test:github'
type: string
allure_version:
description: 'Allure CLI version'
required: false
default: '2.34.0'
type: string
deploy_pages:
description: 'Deploy Allure report to GitHub Pages (first matrix only)'
required: false
default: false
type: boolean
permissions:
contents: read
pages: write # needed only if deploy_pages=true
id-token: write # needed only if deploy_pages=true
jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node: ${{ fromJSON(inputs.node_versions) }}
env:
ALLURE_RESULTS_DIR: allure-results
ALLURE_REPORT_DIR: allure-report
TEST_SCRIPT: ${{ inputs.test_script }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node ${{ matrix.node }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
# auto cache: pnpm > yarn > npm
cache: ${{ hashFiles('pnpm-lock.yaml') != '' && 'pnpm' || (hashFiles('yarn.lock') != '' && 'yarn' || 'npm') }}
- name: Enable Corepack
run: corepack enable
- name: Setup pnpm
if: ${{ hashFiles('pnpm-lock.yaml') != '' }}
uses: pnpm/action-setup@v4
with:
version: 9
- name: Install dependencies (pnpm)
if: ${{ hashFiles('pnpm-lock.yaml') != '' }}
run: pnpm install --frozen-lockfile
- name: Install dependencies (yarn)
if: ${{ hashFiles('pnpm-lock.yaml') == '' && hashFiles('yarn.lock') != '' }}
run: yarn install --frozen-lockfile
- name: Install dependencies (npm)
if: ${{ hashFiles('pnpm-lock.yaml') == '' && hashFiles('yarn.lock') == '' }}
run: npm ci
# Allure CLI needs Java; Temurin 17 is the safe default
- name: Setup Java for Allure
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: '17'
# Install specific Allure CLI version from GitHub releases
- name: Install Allure CLI ${{ inputs.allure_version }}
run: |
set -eux
curl -L -o allure.tgz "https://github.com/allure-framework/allure2/releases/download/${{ inputs.allure_version }}/allure-${{ inputs.allure_version }}.tgz"
sudo tar -C /opt -xzf allure.tgz
sudo mv "/opt/allure-${{ inputs.allure_version }}" /opt/allure
sudo ln -sf /opt/allure/bin/allure /usr/local/bin/allure
allure --version
- name: Run tests (prefers your test script)
shell: bash
continue-on-error: true
run: |
set -eux
mkdir -p "$ALLURE_RESULTS_DIR" "$ALLURE_REPORT_DIR"
# Determine package manager
if [ -f pnpm-lock.yaml ]; then PM="pnpm"; elif [ -f yarn.lock ]; then PM="yarn"; else PM="npm"; fi
# If repo provides run-tests.sh, use it for consistency with your local runs
if [ -x ./run-tests.sh ]; then
./run-tests.sh
else
# Fallback: direct npm script invocation
$PM run "$TEST_SCRIPT"
fi
- name: Generate Allure report
if: always()
shell: bash
run: |
set -eux
# Generate even if tests fail so you can inspect failures
allure generate "$ALLURE_RESULTS_DIR" --clean -o "$ALLURE_REPORT_DIR" || true
- name: Upload Allure results (raw)
if: always()
uses: actions/upload-artifact@v4
with:
name: allure-results-${{ matrix.node }}
path: ${{ env.ALLURE_RESULTS_DIR }}
retention-days: 30
- name: Upload Allure report (HTML)
if: always()
uses: actions/upload-artifact@v4
with:
name: allure-report-${{ matrix.node }}
path: ${{ env.ALLURE_REPORT_DIR }}
retention-days: 30
- name: Job Summary
if: always()
shell: bash
run: |
echo "## Test & Allure Summary (Node ${{ matrix.node }})" >> $GITHUB_STEP_SUMMARY
echo "- Node.js: \`${{ matrix.node }}\`" >> $GITHUB_STEP_SUMMARY
echo "- Test script: \`${{ inputs.test_script }}\`" >> $GITHUB_STEP_SUMMARY
echo "- Allure version: \`${{ inputs.allure_version }}\`" >> $GITHUB_STEP_SUMMARY
if [ -f "${{ env.ALLURE_REPORT_DIR }}/widgets/summary.json" ]; then
sudo apt-get update -y >/dev/null 2>&1 || true
sudo apt-get install -y jq >/dev/null 2>&1 || true
P=$(jq '.statistic.passed' ${{ env.ALLURE_REPORT_DIR }}/widgets/summary.json 2>/dev/null || echo 0)
F=$(jq '.statistic.failed' ${{ env.ALLURE_REPORT_DIR }}/widgets/summary.json 2>/dev/null || echo 0)
B=$(jq '.statistic.broken' ${{ env.ALLURE_REPORT_DIR }}/widgets/summary.json 2>/dev/null || echo 0)
S=$(jq '.statistic.skipped' ${{ env.ALLURE_REPORT_DIR }}/widgets/summary.json 2>/dev/null || echo 0)
T=$(jq '.statistic.total' ${{ env.ALLURE_REPORT_DIR }}/widgets/summary.json 2>/dev/null || echo 0)
echo "- Total: **$T** | Passed: **$P** | Failed: **$F** | Broken: **$B** | Skipped: **$S**" >> $GITHUB_STEP_SUMMARY
else
echo "- Allure summary not found (no results?)" >> $GITHUB_STEP_SUMMARY
fi
echo "- Artifacts: \`allure-results-${{ matrix.node }}\`, \`allure-report-${{ matrix.node }}\`" >> $GITHUB_STEP_SUMMARY
deploy-allure:
if: ${{ inputs.deploy_pages }}
needs: [test]
runs-on: ubuntu-latest
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
# publish the report from the first matrix entry
- name: Download Allure report from first matrix entry
uses: actions/download-artifact@v4
with:
name: allure-report-${{ fromJSON(inputs.node_versions)[0] }}
path: ./site
- name: Setup Pages
uses: actions/configure-pages@v5
- name: Upload Pages artifact
uses: actions/upload-pages-artifact@v3
with:
path: ./site
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4