|
| 1 | +name: Update Dependencies |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + # Run at 9:00 AM UTC every Monday |
| 6 | + - cron: 0 9 * * 1 |
| 7 | + # Allow manual runs |
| 8 | + workflow_dispatch: |
| 9 | + inputs: |
| 10 | + package: |
| 11 | + description: > |
| 12 | + Specific package to update (e.g., numpy, librosa). |
| 13 | + Leave empty to check all packages. |
| 14 | + required: false |
| 15 | + version: |
| 16 | + description: > |
| 17 | + Target version for the package. Leave empty to use latest. |
| 18 | + required: false |
| 19 | + |
| 20 | +env: |
| 21 | + PYTHON_VERSION: 3.11 |
| 22 | + PIP_VERSION: 24.0 |
| 23 | + |
| 24 | +jobs: |
| 25 | + check-and-update: |
| 26 | + runs-on: ubuntu-latest |
| 27 | + if: github.repository_owner == 'JaclynCodes' |
| 28 | + permissions: |
| 29 | + contents: write # Needed to push changes |
| 30 | + steps: |
| 31 | + - name: Checkout |
| 32 | + uses: actions/checkout@v4 |
| 33 | + with: |
| 34 | + # Use default token for now, can be updated to use PAT if needed |
| 35 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 36 | + |
| 37 | + - name: Set Git author |
| 38 | + run: | |
| 39 | + git config user.name "github-actions[bot]" |
| 40 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 41 | +
|
| 42 | + - name: Setup Python |
| 43 | + uses: actions/setup-python@v5 |
| 44 | + with: |
| 45 | + python-version: ${{ env.PYTHON_VERSION }} |
| 46 | + |
| 47 | + - name: Upgrade pip |
| 48 | + run: | |
| 49 | + python -m pip install --upgrade pip==${{ env.PIP_VERSION }} |
| 50 | +
|
| 51 | + - name: Install dependencies |
| 52 | + run: | |
| 53 | + pip install -r requirements.txt |
| 54 | +
|
| 55 | + - name: Check for updates |
| 56 | + id: check_updates |
| 57 | + run: | |
| 58 | + # Install pip-tools for dependency management |
| 59 | + pip install pip-tools |
| 60 | +
|
| 61 | + # Check for outdated packages |
| 62 | + echo "Checking for outdated packages..." |
| 63 | + pip list --outdated > outdated.txt |
| 64 | + cat outdated.txt |
| 65 | +
|
| 66 | + if [ -s outdated.txt ]; then |
| 67 | + echo "updates_available=true" >> $GITHUB_OUTPUT |
| 68 | + else |
| 69 | + echo "updates_available=false" >> $GITHUB_OUTPUT |
| 70 | + fi |
| 71 | +
|
| 72 | + - name: Update dependencies |
| 73 | + if: steps.check_updates.outputs.updates_available == 'true' |
| 74 | + run: | |
| 75 | + # Create a new branch for the update |
| 76 | + BRANCH_NAME="automated/update-dependencies-$(date +%Y%m%d)" |
| 77 | + git checkout -b "$BRANCH_NAME" |
| 78 | +
|
| 79 | + # If specific package and version provided, update that |
| 80 | + if [ -n "${{ inputs.package }}" ]; then |
| 81 | + if [ -n "${{ inputs.version }}" ]; then |
| 82 | + echo "Updating ${{ inputs.package }} to version \ |
| 83 | + ${{ inputs.version }}" |
| 84 | + # Use word boundaries to match exact package name |
| 85 | + sed -i "s/^${{ inputs.package }}[>=<].*/${{ inputs.package }}==\ |
| 86 | + ${{ inputs.version }}/" requirements.txt |
| 87 | + else |
| 88 | + echo "Updating ${{ inputs.package }} to latest \ |
| 89 | + compatible version" |
| 90 | + # Get latest version compatible with constraints |
| 91 | + LATEST=$(pip index versions "${{ inputs.package }}" | \ |
| 92 | + grep "Available versions:" | head -1 | cut -d: -f2 | \ |
| 93 | + tr ',' '\n' | head -1 | xargs) |
| 94 | + if [ -z "$LATEST" ]; then |
| 95 | + echo "Error: Could not determine latest version for \ |
| 96 | + ${{ inputs.package }}" |
| 97 | + exit 1 |
| 98 | + fi |
| 99 | + # Use word boundaries to match exact package name |
| 100 | + sed -i "s/^${{ inputs.package }}[>=<].*/${{ inputs.package }}>=\ |
| 101 | + $LATEST/" requirements.txt |
| 102 | + fi |
| 103 | + else |
| 104 | + echo "No specific package provided for update." |
| 105 | + echo "Please run manually with package name to update." |
| 106 | + echo "Bulk updates require manual review and are not \ |
| 107 | + automated." |
| 108 | + exit 0 |
| 109 | + fi |
| 110 | +
|
| 111 | + # Commit changes if there are any |
| 112 | + if git diff --quiet requirements.txt; then |
| 113 | + echo "No changes to commit" |
| 114 | + exit 0 |
| 115 | + else |
| 116 | + git add requirements.txt |
| 117 | + COMMIT_MSG="Update ${{ inputs.package }} dependency" |
| 118 | + git commit -m "$COMMIT_MSG" |
| 119 | + git push origin "$BRANCH_NAME" |
| 120 | +
|
| 121 | + # Create PR using GitHub CLI would go here if available |
| 122 | + echo "Branch $BRANCH_NAME created with updates" |
| 123 | + fi |
0 commit comments