Update dependencies #26
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Update dependencies | |
on: | |
schedule: | |
- cron: '0 0 * * 0' # Runs at 00:00 every Sunday | |
workflow_dispatch: | |
inputs: | |
pr_branch: | |
description: 'Branch to push changes to (optional)' | |
required: false | |
type: string | |
workflow_call: | |
inputs: | |
pr_branch: | |
description: 'Branch to push changes to' | |
required: true | |
type: string | |
jobs: | |
update-deps: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
pull-requests: write | |
steps: | |
- uses: actions/checkout@v4 | |
name: Checkout repository | |
with: | |
fetch-depth: 0 # Fetch all history for all branches | |
- name: Checkout target branch | |
if: inputs.pr_branch | |
run: | | |
BRANCH_NAME=$(echo ${{ inputs.pr_branch }} | sed 's|refs/heads/||') | |
git fetch origin $BRANCH_NAME | |
git checkout $BRANCH_NAME | |
git pull origin $BRANCH_NAME | |
- name: Setup PDM | |
uses: pdm-project/setup-pdm@v4 | |
with: | |
cache: true | |
- name: Lock dependencies | |
run: pdm lock -G :all | |
- name: Export requirements | |
run: pdm run export | |
- name: Check for changes | |
id: git-check | |
run: | | |
git diff --exit-code --quiet requirements.txt || echo "changed=true" >> $GITHUB_OUTPUT | |
- name: Handle dependency updates | |
if: steps.git-check.outputs.changed == 'true' | |
run: | | |
git config user.name github-actions | |
git config user.email [email protected] | |
if [ -n "${{ inputs.pr_branch }}" ]; then | |
# Push to existing branch | |
BRANCH_NAME=$(echo ${{ inputs.pr_branch }} | cut -d'/' -f 3) | |
git checkout $BRANCH_NAME | |
git add requirements.txt pyproject.toml pdm.lock | |
git commit -m "Update dependencies" | |
git push origin $BRANCH_NAME | |
else | |
# Check for existing PR | |
EXISTING_PR=$(gh pr list --search "Update dependencies in:title is:open" --json headRefName,number -q '.[0]') | |
if [ -n "$EXISTING_PR" ]; then | |
# Update existing PR | |
BRANCH_NAME=$(echo $EXISTING_PR | jq -r .headRefName) | |
git checkout -B $BRANCH_NAME | |
git add requirements.txt pyproject.toml pdm.lock | |
git commit -m "Update dependencies" | |
git push -f origin $BRANCH_NAME | |
else | |
# Create new branch and PR | |
NEW_BRANCH="update-dependencies-${{ github.run_id }}" | |
git checkout -b $NEW_BRANCH | |
git add requirements.txt pyproject.toml pdm.lock | |
git commit -m "Update dependencies" | |
git push origin $NEW_BRANCH | |
gh pr create \ | |
--title "Update dependencies" \ | |
--body "This PR updates the project dependencies. Please review the changes and merge if everything looks good." \ | |
--base ${{ github.ref_name }} \ | |
--head $NEW_BRANCH | |
fi | |
fi | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |