Skip to content

Update Test Weights #12

Update Test Weights

Update Test Weights #12

name: Update Test Weights
on:
schedule:
# Run every Sunday at 2am UTC
- cron: "0 2 * * 0"
workflow_dispatch:
inputs:
run_count:
description: "Number of recent CI runs to analyze (default: 3)"
required: false
default: "3"
type: string
permissions:
contents: write # To create branches and commits
actions: read # To fetch workflow runs and artifacts
pull-requests: write # To create pull requests
jobs:
update-weights:
if: github.repository == 'datahub-project/datahub'
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- name: Checkout repository
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: "3.10"
- name: Download test artifacts from recent CI runs
env:
GH_TOKEN: ${{ github.token }}
run: |
echo "Downloading test artifacts from recent successful CI runs..."
bash .github/scripts/download_test_artifacts.sh \
--output-dir ./test-artifacts \
--run-count ${{ github.event.inputs.run_count || '3' }} \
--workflow docker-unified.yml \
--repository ${{ github.repository }}
- name: Backup existing weights
run: |
mkdir -p ./weights-backup
cp smoke-test/tests/cypress/test_weights.json ./weights-backup/cypress_weights.json || echo "No existing Cypress weights"
cp smoke-test/pytest_test_weights.json ./weights-backup/pytest_weights.json || echo "No existing Pytest weights"
- name: Generate new test weights
run: |
echo "Generating test weights from downloaded artifacts..."
python .github/scripts/generate_test_weights.py \
--input-dir ./test-artifacts \
--cypress-output smoke-test/tests/cypress/test_weights.json \
--pytest-output smoke-test/pytest_test_weights.json
- name: Compare weights and generate PR body
id: compare
run: |
echo "Comparing old and new weights..."
python .github/scripts/compare_test_weights.py \
--old-cypress ./weights-backup/cypress_weights.json \
--new-cypress smoke-test/tests/cypress/test_weights.json \
--old-pytest ./weights-backup/pytest_weights.json \
--new-pytest smoke-test/pytest_test_weights.json \
--output ./pr-body.md \
--threshold 5.0
# Check if PR body was generated (non-empty file means changes exceed threshold)
if [ -s ./pr-body.md ]; then
echo "create_pr=true" >> "$GITHUB_OUTPUT"
echo "✓ Changes exceed 5% threshold. Will create PR."
else
echo "create_pr=false" >> "$GITHUB_OUTPUT"
echo "✓ Changes below 5% threshold. No PR needed."
fi
- name: Create branch and commit changes
if: steps.compare.outputs.create_pr == 'true'
run: |
# Configure git
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
# Create branch with date
BRANCH_NAME="auto/update-test-weights-$(date +%Y%m%d-%H%M%S)"
git checkout -b "$BRANCH_NAME"
# Add and commit changes
git add smoke-test/tests/cypress/test_weights.json smoke-test/pytest_test_weights.json
git commit -m "chore(test): update test weights from recent CI runs
Automated update of test weights based on analysis of recent successful CI runs.
This improves batch balancing and reduces overall CI execution time.
Generated by: ${{ github.workflow }} workflow
Run: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
# Push branch
git push origin "$BRANCH_NAME"
# Save branch name for PR creation
echo "BRANCH_NAME=$BRANCH_NAME" >> "$GITHUB_ENV"
- name: Create pull request
if: steps.compare.outputs.create_pr == 'true'
env:
GH_TOKEN: ${{ github.token }}
run: |
# Read PR body from file
PR_BODY=$(cat ./pr-body.md)
# Create PR
gh pr create \
--title "chore(test): Update test weights from CI runs ($(date +%Y-%m-%d))" \
--body "$PR_BODY" \
--head "$BRANCH_NAME" \
--base master
echo "✓ Pull request created successfully"
- name: Summary
if: always()
run: |
{
echo "### Test Weight Update Summary"
echo ""
if [ "${{ steps.compare.outputs.create_pr }}" == "true" ]; then
echo "✅ **PR Created**: Changes exceeded 5% threshold"
echo ""
echo "Branch: \`$BRANCH_NAME\`"
else
echo "ℹ️ **No PR Created**: Changes below 5% threshold"
echo ""
echo "Test weights were updated but the changes were too small to warrant a PR."
fi
echo ""
echo "Workflow run: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
} >> "$GITHUB_STEP_SUMMARY"