Skip to content

Check Submodule Updates #84

Check Submodule Updates

Check Submodule Updates #84

name: Check Submodule Updates
on:
schedule:
# Run daily at midnight
- cron: '0 0 * * *'
# Allow manual trigger
workflow_dispatch:
jobs:
check-submodule-updates:
name: Check for submodule updates
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
env:
PR_BRANCH: submodule-updates
PR_TITLE: "[chore]: update submodules"
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
# Fetch all history for all branches and tags
fetch-depth: 0
# Use SSH key authentication for submodules
submodules: 'recursive'
token: ${{ secrets.GH_WORKFLOW_PAT }}
- name: Set up Git identity
run: |
git config --global user.name "jadit19"
git config --global user.email "[email protected]"
- name: Check for existing PR
id: check_pr
env:
GH_TOKEN: ${{ secrets.GH_WORKFLOW_PAT }}
run: |
PR_EXISTS=$(gh pr list --head ${{ env.PR_BRANCH }} --state open --json number | jq 'length')
echo "pr_exists=$PR_EXISTS" >> $GITHUB_OUTPUT
if [ "$PR_EXISTS" -gt "0" ]; then
PR_NUMBER=$(gh pr list --head ${{ env.PR_BRANCH }} --state open --json number --jq '.[0].number')
echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT
fi
- name: Check for submodule updates
id: check_updates
run: |
# Initialize all submodules
git submodule update --init --recursive
# Store current submodule commits
echo "Current submodule states:" > submodule_changes.txt
git submodule status >> submodule_changes.txt
# Update all submodules to latest on their respective default branches
git submodule foreach 'git fetch origin && git checkout $(git rev-parse --abbrev-ref origin/HEAD | sed "s|origin/||") && git pull'
# Check if there are changes
git status
if git diff --quiet --exit-code; then
echo "No submodule updates found"
echo "has_changes=false" >> $GITHUB_OUTPUT
else
echo "Submodule updates found"
echo "has_changes=true" >> $GITHUB_OUTPUT
# Add the changes to submodule_changes.txt
echo -e "\n\nUpdated submodule states:" >> submodule_changes.txt
git submodule status >> submodule_changes.txt
fi
- name: Create or update branch
if: steps.check_updates.outputs.has_changes == 'true'
run: |
# Check if the branch already exists locally
if git rev-parse --verify ${{ env.PR_BRANCH }} >/dev/null 2>&1; then
git checkout ${{ env.PR_BRANCH }}
# Ensure the branch is up-to-date with main
git fetch origin main
git merge origin/main --no-edit
else
# Create a new branch
git checkout -b ${{ env.PR_BRANCH }}
fi
# Add and commit changes
git add lib
git commit -m "chore: update git submodules to latest version"
- name: Configure Git to use PAT for push
if: steps.check_updates.outputs.has_changes == 'true'
run: |
git remote set-url origin https://x-access-token:${{ secrets.GH_WORKFLOW_PAT }}@github.com/${{ github.repository }}
- name: Push changes
if: steps.check_updates.outputs.has_changes == 'true'
run: |
git push -u origin ${{ env.PR_BRANCH }} --force
- name: Create pull request
if: steps.check_updates.outputs.has_changes == 'true' && steps.check_pr.outputs.pr_exists == '0'
env:
GH_TOKEN: ${{ secrets.GH_WORKFLOW_PAT }}
run: |
CHANGES=$(cat submodule_changes.txt)
PR_BODY="
## Submodule Updates
This PR updates the following git submodules to their latest versions:
\`\`\`
${CHANGES}
\`\`\`
*This PR was automatically generated by GitHub Actions.*
"
gh pr create --base main --head ${{ env.PR_BRANCH }} --title "${{ env.PR_TITLE }}" --body "$PR_BODY"
- name: Update existing pull request
if: steps.check_updates.outputs.has_changes == 'true' && steps.check_pr.outputs.pr_exists != '0'
env:
GH_TOKEN: ${{ secrets.GH_WORKFLOW_PAT }}
run: |
CHANGES=$(cat submodule_changes.txt)
PR_BODY="
## Submodule Updates
This PR updates the following git submodules to their latest versions:
\`\`\`
${CHANGES}
\`\`\`
*This PR was automatically updated by GitHub Actions on $(date).*
"
gh pr edit ${{ steps.check_pr.outputs.pr_number }} --body "$PR_BODY"