|
| 1 | +name: CI |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + - cron: "0 16 * * 1,4" |
| 6 | + push: |
| 7 | + paths: |
| 8 | + - 'gget/**' |
| 9 | + - 'tests/**' |
| 10 | + - '!tests/pytest_results_py*.txt' |
| 11 | + workflow_dispatch: |
| 12 | + |
| 13 | +permissions: |
| 14 | + contents: write |
| 15 | + |
| 16 | +jobs: |
| 17 | + build: |
| 18 | + runs-on: ubuntu-22.04 |
| 19 | + strategy: |
| 20 | + fail-fast: false |
| 21 | + matrix: |
| 22 | + python: ['3.11', '3.12'] |
| 23 | + os: ['ubuntu-22.04'] |
| 24 | + name: Test on Python ${{ matrix.python }} |
| 25 | + |
| 26 | + steps: |
| 27 | + - name: Checkout branch |
| 28 | + uses: actions/checkout@v4 |
| 29 | + with: |
| 30 | + fetch-depth: 0 |
| 31 | + |
| 32 | + - name: Setup python |
| 33 | + uses: actions/setup-python@v5 |
| 34 | + with: |
| 35 | + python-version: ${{ matrix.python }} |
| 36 | + |
| 37 | + - name: Install dependencies |
| 38 | + run: pip install -r requirements.txt && pip install -r dev-requirements.txt |
| 39 | + |
| 40 | + # Always run tests on push |
| 41 | + - name: Run tests |
| 42 | + if: github.event_name == 'push' |
| 43 | + run: pytest -ra -v --tb=long --maxfail=5 --durations=10 --cov=gget --cov-report=term-missing tests |
| 44 | + |
| 45 | + # On schedule/manual, run tests AND save output (and capture real pytest exit code) |
| 46 | + - name: Run tests (save output to file) |
| 47 | + id: pytest_saved |
| 48 | + if: github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' |
| 49 | + continue-on-error: true |
| 50 | + shell: bash |
| 51 | + run: | |
| 52 | + set -o pipefail |
| 53 | + OUT="tests/pytest_results_py${{ matrix.python }}.txt" |
| 54 | + echo "Pytest results (Python ${{ matrix.python }}) - $(date -u +"%Y-%m-%dT%H:%M:%SZ")" > "$OUT" |
| 55 | + echo "" >> "$OUT" |
| 56 | +
|
| 57 | + pytest -ra -v --tb=long --maxfail=5 --durations=10 \ |
| 58 | + --cov=gget --cov-report=term-missing tests 2>&1 | tee -a "$OUT" |
| 59 | +
|
| 60 | + code=${PIPESTATUS[0]} |
| 61 | + echo "exit_code=$code" >> "$GITHUB_OUTPUT" |
| 62 | + echo "pytest exit code: $code" |
| 63 | +
|
| 64 | + - name: Upload pytest results artifact |
| 65 | + # create and commit results only for one python version to avoid matrix push races |
| 66 | + if: always() && matrix.python == '3.12' && (github.event_name == 'schedule' || github.event_name == 'workflow_dispatch') |
| 67 | + uses: actions/upload-artifact@v4 |
| 68 | + with: |
| 69 | + name: pytest-results-py${{ matrix.python }} |
| 70 | + path: tests/pytest_results_py${{ matrix.python }}.txt |
| 71 | + |
| 72 | + - name: Commit and push pytest results |
| 73 | + # create and commit results only for one python version to avoid matrix push races |
| 74 | + if: always() && matrix.python == '3.12' && (github.event_name == 'schedule' || github.event_name == 'workflow_dispatch') |
| 75 | + shell: bash |
| 76 | + run: | |
| 77 | + set -euo pipefail |
| 78 | + |
| 79 | + git config user.name "github-actions[bot]" |
| 80 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 81 | + |
| 82 | + git add -A tests/pytest_results_py*.txt |
| 83 | + |
| 84 | + if git diff --cached --quiet; then |
| 85 | + echo "No changes to commit." |
| 86 | + exit 0 |
| 87 | + fi |
| 88 | + |
| 89 | + git commit -m "CI: update pytest results" |
| 90 | + |
| 91 | + # Keep branch up-to-date (handles concurrent updates) + retry push |
| 92 | + for attempt in 1 2 3 4 5; do |
| 93 | + echo "Push attempt $attempt..." |
| 94 | + # autostash prevents "cannot rebase: You have unstaged changes" if anything is modified locally |
| 95 | + git pull --rebase --autostash origin main || true |
| 96 | + if git push origin main; then |
| 97 | + exit 0 |
| 98 | + fi |
| 99 | + sleep $((attempt * 5)) |
| 100 | + done |
| 101 | + |
| 102 | + echo "Push failed after retries." |
| 103 | + exit 1 |
| 104 | + |
| 105 | + # Make schedule/manual runs still fail if pytest failed |
| 106 | + - name: Fail job if pytest failed |
| 107 | + if: always() && (github.event_name == 'schedule' || github.event_name == 'workflow_dispatch') |
| 108 | + shell: bash |
| 109 | + run: | |
| 110 | + code="${{ steps.pytest_saved.outputs.exit_code }}" |
| 111 | + if [ -n "$code" ] && [ "$code" != "0" ]; then |
| 112 | + exit "$code" |
| 113 | + fi |
0 commit comments