Update dependencies #27
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: Create required label if not exists | |
run: | | |
# Check if label exists | |
LABEL_EXISTS=$(gh label list | grep "automated-dependencies-update" || true) | |
if [ -z "$LABEL_EXISTS" ]; then | |
echo "Creating automated-dependencies-update label..." | |
gh label create "automated-dependencies-update" \ | |
--color "2DA44E" \ | |
--description "Automated PR for updating project dependencies" | |
fi | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- name: Handle dependency updates | |
if: steps.git-check.outputs.changed == 'true' | |
run: | | |
git config user.name github-actions | |
git config user.email github-actions@github.com | |
# Function to commit changes | |
commit_changes() { | |
git add requirements.txt pyproject.toml pdm.lock | |
git commit -m "Update dependencies" | |
} | |
# Function to create PR | |
create_pr() { | |
local BRANCH=$1 | |
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 $BRANCH \ | |
--label "automated-dependencies-update" | |
} | |
if [ -n "${{ inputs.pr_branch }}" ]; then | |
# Push to existing branch | |
BRANCH_NAME=$(echo ${{ inputs.pr_branch }} | cut -d'/' -f 3) | |
git checkout $BRANCH_NAME | |
commit_changes | |
git push origin $BRANCH_NAME | |
else | |
# Check for existing PR - strict search for exact title and our specific branch pattern | |
EXISTING_PR=$(gh pr list --search "in:title Update dependencies is:open label:automated-dependencies-update" --json headRefName,number,author -q '.[0]') | |
if [ -n "$EXISTING_PR" ]; then | |
# Check if PR has our automation label | |
BRANCH_NAME=$(echo $EXISTING_PR | jq -r .headRefName) | |
if [[ "$BRANCH_NAME" == update-dependencies-* ]]; then | |
echo "Found valid automated PR with branch $BRANCH_NAME. Updating it." | |
git checkout -B $BRANCH_NAME | |
commit_changes | |
git push -f origin $BRANCH_NAME | |
else | |
echo "Found PR but wrong branch pattern. Creating new branch." | |
NEW_BRANCH="update-dependencies-${{ github.run_id }}" | |
git checkout -b $NEW_BRANCH | |
commit_changes | |
git push origin $NEW_BRANCH | |
create_pr $NEW_BRANCH | |
fi | |
else | |
echo "No existing PR found. Creating new branch and PR." | |
NEW_BRANCH="update-dependencies-${{ github.run_id }}" | |
git checkout -b $NEW_BRANCH | |
commit_changes | |
git push origin $NEW_BRANCH | |
create_pr $NEW_BRANCH | |
fi | |
fi | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |