diff --git a/.circleci/config.yml b/.circleci/config.yml index e628aad5..2ca137ef 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -162,7 +162,9 @@ workflows: - ensure_formatting - linter - test - - deploy: + # Manual only: the upload to PyPI waits for an approval in CircleCI + - approve_deploy: + type: approval requires: - build filters: @@ -170,6 +172,14 @@ workflows: only: /[0-9]+(\.[0-9]+)+(\.[0-9]+)?\.?(\w)*/ branches: ignore: /.*/ + - deploy: + requires: + - approve_deploy + filters: + tags: + only: /[0-9]+(\.[0-9]+)+(\.[0-9]+)?\.?(\w)*/ + branches: + ignore: /.*/ - notify_rolling: requires: - build diff --git a/.github/actions/checkout/action.yml b/.github/actions/checkout/action.yml new file mode 100644 index 00000000..20e74b1a --- /dev/null +++ b/.github/actions/checkout/action.yml @@ -0,0 +1,9 @@ +name: Checkout Repository +description: Checkout the repository using a pinned version of actions/checkout + +runs: + using: composite + steps: + # https://github.com/actions/checkout/releases/#release-v7.0.1 + - name: Checkout + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 \ No newline at end of file diff --git a/.github/actions/setup-python/action.yml b/.github/actions/setup-python/action.yml new file mode 100644 index 00000000..0e393fb0 --- /dev/null +++ b/.github/actions/setup-python/action.yml @@ -0,0 +1,27 @@ +name: Setup Python Environment +description: Configure Python and install project development dependencies + +inputs: + python-version: + description: Python version to use + required: false + default: "3.13" + +runs: + using: composite + steps: + # https://github.com/actions/setup-python/releases#release-v7.1.0 + - name: Set up Python + uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0 + with: + python-version: ${{ inputs.python-version }} + cache: pip + cache-dependency-path: pyproject.toml + + - name: Update pip + shell: bash + run: python -m pip install --upgrade pip certifi + + - name: Install development dependencies + shell: bash + run: python -m pip install ".[dev]" diff --git a/.github/workflows/ci-lint-format.yml b/.github/workflows/ci-lint-format.yml new file mode 100644 index 00000000..77753742 --- /dev/null +++ b/.github/workflows/ci-lint-format.yml @@ -0,0 +1,47 @@ +name: Lint & Format + +on: + workflow_call: + +permissions: + contents: read + +jobs: + ensure_formatting: + name: Ensure Formatting + runs-on: ubuntu-24.04 + timeout-minutes: 5 + + steps: + - name: Checkout Repository + uses: $/.github/actions/checkout + + - name: Setup Python Environment + uses: $/.github/actions/setup-python + + - name: Confirm Black version + run: black --version + + - name: Check isort + run: isort --check-only --diff . + + - name: Check Black + run: black --check --diff . + + linter: + name: Linter + runs-on: ubuntu-24.04 + timeout-minutes: 5 + + steps: + - name: Checkout Repository + uses: $/.github/actions/checkout + + - name: Setup Python Environment + uses: $/.github/actions/setup-python + + - name: Install flake8 + run: python -m pip install flake8 + + - name: Run Flake8 + run: flake8 --ignore=E,W . diff --git a/.github/workflows/ci-pipeline.yml b/.github/workflows/ci-pipeline.yml new file mode 100644 index 00000000..7c31a3b4 --- /dev/null +++ b/.github/workflows/ci-pipeline.yml @@ -0,0 +1,277 @@ +name: "[Client Python] CI" + +on: + push: + branches: + - "main" + tags: + - "*" + + pull_request: + + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + # ────────────────────────────────────────────────────────────────── + # Lint & Format + # ────────────────────────────────────────────────────────────────── + lint-format: + name: Lint - Format + uses: $/.github/workflows/ci-lint-format.yml + + # ────────────────────────────────────────────────────────────────── + # Tests + # ────────────────────────────────────────────────────────────────── + tests: + name: Tests + runs-on: ubuntu-24.04 + timeout-minutes: 10 + + steps: + - name: Checkout Repository + uses: $/.github/actions/checkout + + - name: Setup Python Environment + uses: $/.github/actions/setup-python + + - name: Run Tests + run: python -m pytest + + # ────────────────────────────────────────────────────────────────── + # Build + # ────────────────────────────────────────────────────────────────── + build: + name: Build + runs-on: ubuntu-24.04 + timeout-minutes: 10 + + needs: + - lint-format + - tests + + steps: + - name: Checkout Repository + uses: $/.github/actions/checkout + + - name: Setup Python Environment + uses: $/.github/actions/setup-python + + - name: Check Version + if: startsWith(github.ref, 'refs/tags/') + shell: bash + run: | + package_version="$( + python -c 'import pyoaev; print(pyoaev.__version__)' + )" + + tag_version="${GITHUB_REF_NAME}" + if [ "${tag_version}" != "${package_version}" ]; then + echo "::error::Version mismatch: Git tag version '${tag_version}' does not match package version '${package_version}'." + exit 1 + fi + + - name: Build Package + run: | + SOURCE_DATE_EPOCH="$(git log -1 --pretty=%ct)" \ + python -m build + + # https://github.com/actions/upload-artifact/releases/tag/v7.0.2 + - name: Upload Package + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: pyoaev-dist + path: dist/ + if-no-files-found: error + retention-days: 14 + + # ────────────────────────────────────────────────────────────────── + # Pipeline Gate + # Required branch-protection check: "Pipeline Gate" + # Fails if any mandatory upstream job did not succeed. + # ────────────────────────────────────────────────────────────────── + pipeline-gate: + name: "Pipeline Gate" + needs: + - lint-format + - tests + - build + if: ${{ always() }} + runs-on: ubuntu-24.04 + timeout-minutes: 15 + permissions: + actions: read + + steps: + - name: Check all upstream job results + env: + NEEDS_JSON: ${{ toJSON(needs) }} + GH_TOKEN: ${{ github.token }} + GH_REPOSITORY: ${{ github.repository }} + GH_RUN_ID: ${{ github.run_id }} + GH_API_URL: ${{ github.api_url }} + run: | + set -euo pipefail + + FAILED=0 + TOTAL=0 + + echo "══════════════════════════════════════════════════" + echo " 🔎 Upstream Job Results" + echo "══════════════════════════════════════════════════" + echo "" + + # Retrieve actual job timestamps from GitHub API + JOBS_JSON=$(curl -fsSL \ + -H "Authorization: Bearer $GH_TOKEN" \ + -H "Accept: application/vnd.github+json" \ + "$GH_API_URL/repos/$GH_REPOSITORY/actions/runs/$GH_RUN_ID/jobs?per_page=100") + + # Initialize GitHub Actions Summary + { + echo "## Upstream Job Results" + echo "" + echo "| Job | Started | Finished | Duration | Status |" + echo "|---|---|---|---|---|" + } >> "$GITHUB_STEP_SUMMARY" + + # Dynamically iterate over all jobs declared in needs + for job in $(jq -r 'keys[]' <<< "$NEEDS_JSON"); do + result=$(jq -r --arg j "$job" '.[$j].result' <<< "$NEEDS_JSON") + TOTAL=$((TOTAL + 1)) + + case "$result" in + success) + icon="✅" + status="Passed" + ;; + skipped) + icon="⏭️" + status="Skipped" + FAILED=$((FAILED + 1)) + ;; + failure) + icon="❌" + status="Failed" + FAILED=$((FAILED + 1)) + ;; + cancelled) + icon="🚫" + status="Cancelled" + FAILED=$((FAILED + 1)) + ;; + *) + icon="❓" + status="Unknown" + FAILED=$((FAILED + 1)) + ;; + esac + + started_at="" + finished_at="" + started="—" + finished="—" + duration="—" + + job_data=$(jq -c \ + --arg name "$job" \ + ' + def normalize: + ascii_downcase + | gsub("[^a-z0-9]+"; "-") + | gsub("(^-+|-+$)"; ""); + + ($name | normalize) as $target | + + [ + .jobs[] + | select( + (.name | normalize) == $target + or + (.name | normalize | startswith($target + "-")) + ) + ] as $matches | + + if ($matches | length) == 0 then + empty + else + { + started_at: ( + [$matches[].started_at | select(. != null)] + | sort + | first // null + ), + completed_at: ( + [$matches[].completed_at | select(. != null)] + | sort + | last // null + ) + } + end + ' <<< "$JOBS_JSON") + + if [[ -n "$job_data" ]]; then + started_at=$(jq -r '.started_at // empty' <<< "$job_data") + finished_at=$(jq -r '.completed_at // empty' <<< "$job_data") + fi + + # Format start timestamp + if [[ -n "$started_at" ]]; then + started=$(date -u -d "$started_at" '+%Y-%m-%d %H:%M:%S UTC') + fi + + # Format finish timestamp + if [[ -n "$finished_at" ]]; then + finished=$(date -u -d "$finished_at" '+%Y-%m-%d %H:%M:%S UTC') + fi + + # Calculate job duration + if [[ -n "$started_at" && -n "$finished_at" ]]; then + start_epoch=$(date -u -d "$started_at" +%s) + finish_epoch=$(date -u -d "$finished_at" +%s) + duration_seconds=$((finish_epoch - start_epoch)) + + duration=$(printf '%02dm %02ds' \ + $((duration_seconds / 60)) \ + $((duration_seconds % 60))) + fi + + # Display result in workflow logs + printf " %s %-30s %s\n" "$icon" "$job" "$result" + + # Add result to GitHub Actions Summary + printf "| %s %s | %s | %s | %s | %s |\n" \ + "$icon" "$job" "$started" "$finished" "$duration" "$status" \ + >> "$GITHUB_STEP_SUMMARY" + + done + + echo "" + echo "══════════════════════════════════════════════════" + + if [[ "$FAILED" -gt 0 ]]; then + echo "❌ Gate failed: $FAILED/$TOTAL job(s) did not succeed." + + { + echo "" + echo "**❌ Gate failed: $FAILED/$TOTAL required job(s) did not succeed.**" + } >> "$GITHUB_STEP_SUMMARY" + + exit 1 + fi + + echo "✅ All $TOTAL upstream jobs succeeded." + echo "✅ Pipeline Gate passed." + + { + echo "" + echo "**✅ All $TOTAL upstream jobs succeeded.**" + echo "" + echo "### Pipeline Gate: Passed" + } >> "$GITHUB_STEP_SUMMARY" \ No newline at end of file diff --git a/.github/workflows/ci-retry.yml b/.github/workflows/ci-retry.yml new file mode 100644 index 00000000..aeb69532 --- /dev/null +++ b/.github/workflows/ci-retry.yml @@ -0,0 +1,80 @@ +name: "[Client Python] Auto-Retry Failed CI" + +# Triggers when the CI workflow completes unsuccessfully. +# Retries failed jobs up to twice, so back-to-back infra flakes are still covered. +# +# A job killed by its timeout-minutes limit does not make the run "failure": GitHub reports +# the run as "cancelled", like a manual cancel or a superseded run. A cancelled run is retried +# only when all its cancelled jobs hit their timeout, so a deliberate cancel is never undone. +# A run is not retried either once a newer run of the same workflow exists for its branch: +# the CI concurrency group cancels in progress, so the re-run would cancel the newer run. + +on: + workflow_run: + # Workflow names are glob patterns, so literal brackets must be escaped. + # https://github.com/github/docs/issues/12572#issuecomment-1014035978 + workflows: ['\[Client Python\] CI'] + types: [completed] + +permissions: + actions: write + checks: read + +jobs: + retry: + name: "🔄 Retry Failed Jobs" + if: >- + ${{ + (github.event.workflow_run.conclusion == 'failure' || + github.event.workflow_run.conclusion == 'cancelled') && + github.event.workflow_run.run_attempt <= 2 + }} + runs-on: ubuntu-24.04 + timeout-minutes: 5 + steps: + - name: Re-run failed jobs + env: + GH_TOKEN: ${{ github.token }} + REPO: ${{ github.repository }} + RUN_ID: ${{ github.event.workflow_run.id }} + RUN_NAME: ${{ github.event.workflow_run.name }} + ATTEMPT: ${{ github.event.workflow_run.run_attempt }} + CONCLUSION: ${{ github.event.workflow_run.conclusion }} + WORKFLOW_ID: ${{ github.event.workflow_run.workflow_id }} + HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }} + EVENT: ${{ github.event.workflow_run.event }} + run: | + set -euo pipefail + latest_run_id=$(gh api --method GET "repos/${REPO}/actions/workflows/${WORKFLOW_ID}/runs" \ + -f branch="${HEAD_BRANCH}" -f event="${EVENT}" -F per_page=1 \ + --jq '.workflow_runs[0].id') + if [ "${latest_run_id}" != "${RUN_ID}" ]; then + echo "⏭️ ${RUN_NAME} run #${RUN_ID} was superseded by run #${latest_run_id} — not retrying." + exit 0 + fi + if [ "${CONCLUSION}" = "cancelled" ]; then + # A run counts as timed out only when every cancelled job carries GitHub's + # timeout annotation. A manual cancel or a superseded run never does, so if GitHub + # rewords the message, timeouts stop being retried instead of cancels being undone. + # Fetched into variables first so an API error fails the step instead of + # reading as "no timeout". + job_ids=$(gh api --paginate \ + "repos/${REPO}/actions/runs/${RUN_ID}/attempts/${ATTEMPT}/jobs" \ + --jq '.jobs[] | select(.conclusion == "cancelled") | .id') + if [ -z "${job_ids}" ]; then + echo "⏭️ ${RUN_NAME} run #${RUN_ID} was cancelled before any job ran — not retrying." + exit 0 + fi + for job_id in ${job_ids}; do + messages=$(gh api --paginate "repos/${REPO}/check-runs/${job_id}/annotations" \ + --jq '.[].message') + if ! grep -q 'exceeded the maximum execution time' <<< "${messages}"; then + echo "⏭️ ${RUN_NAME} run #${RUN_ID} was cancelled (job ${job_id} did not time out) — not retrying." + exit 0 + fi + done + echo "⏱️ ${RUN_NAME} run #${RUN_ID} was cancelled by a job timeout." + fi + echo "🔄 ${RUN_NAME} run #${RUN_ID} failed on attempt ${ATTEMPT} — retrying failed jobs..." + gh api "repos/${REPO}/actions/runs/${RUN_ID}/rerun-failed-jobs" --method POST + echo "✅ Re-run triggered (attempt $((ATTEMPT + 1)))"