|
| 1 | +name: Check Submodule Updates |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + # Run daily at midnight |
| 6 | + - cron: '0 0 * * *' |
| 7 | + # Allow manual trigger |
| 8 | + workflow_dispatch: |
| 9 | + |
| 10 | +jobs: |
| 11 | + check-submodule-updates: |
| 12 | + name: Check for submodule updates |
| 13 | + runs-on: ubuntu-latest |
| 14 | + |
| 15 | + permissions: |
| 16 | + contents: write |
| 17 | + pull-requests: write |
| 18 | + |
| 19 | + env: |
| 20 | + PR_BRANCH: submodule-updates |
| 21 | + PR_TITLE: "[chore]: update submodules" |
| 22 | + |
| 23 | + steps: |
| 24 | + - name: Checkout repository |
| 25 | + uses: actions/checkout@v4 |
| 26 | + with: |
| 27 | + # Fetch all history for all branches and tags |
| 28 | + fetch-depth: 0 |
| 29 | + # Use SSH key authentication for submodules |
| 30 | + submodules: 'recursive' |
| 31 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 32 | + |
| 33 | + - name: Set up Git identity |
| 34 | + run: | |
| 35 | + git config --global user.name "GitHub Actions Bot" |
| 36 | + git config --global user.email "actions@github.com" |
| 37 | + |
| 38 | + - name: Check for existing PR |
| 39 | + id: check_pr |
| 40 | + env: |
| 41 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 42 | + run: | |
| 43 | + PR_EXISTS=$(gh pr list --head ${{ env.PR_BRANCH }} --state open --json number | jq 'length') |
| 44 | + echo "pr_exists=$PR_EXISTS" >> $GITHUB_OUTPUT |
| 45 | + if [ "$PR_EXISTS" -gt "0" ]; then |
| 46 | + PR_NUMBER=$(gh pr list --head ${{ env.PR_BRANCH }} --state open --json number --jq '.[0].number') |
| 47 | + echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT |
| 48 | + fi |
| 49 | +
|
| 50 | + - name: Check for submodule updates |
| 51 | + id: check_updates |
| 52 | + run: | |
| 53 | + # Initialize all submodules |
| 54 | + git submodule update --init --recursive |
| 55 | + |
| 56 | + # Store current submodule commits |
| 57 | + echo "Current submodule states:" > submodule_changes.txt |
| 58 | + git submodule status >> submodule_changes.txt |
| 59 | + |
| 60 | + # Update all submodules to latest on their respective default branches |
| 61 | + git submodule foreach 'git fetch origin && git checkout $(git rev-parse --abbrev-ref origin/HEAD | sed "s|origin/||") && git pull' |
| 62 | + |
| 63 | + # Check if there are changes |
| 64 | + git status |
| 65 | + if git diff --quiet --exit-code; then |
| 66 | + echo "No submodule updates found" |
| 67 | + echo "has_changes=false" >> $GITHUB_OUTPUT |
| 68 | + else |
| 69 | + echo "Submodule updates found" |
| 70 | + echo "has_changes=true" >> $GITHUB_OUTPUT |
| 71 | + |
| 72 | + # Add the changes to submodule_changes.txt |
| 73 | + echo -e "\n\nUpdated submodule states:" >> submodule_changes.txt |
| 74 | + git submodule status >> submodule_changes.txt |
| 75 | + fi |
| 76 | +
|
| 77 | + - name: Create or update branch |
| 78 | + if: steps.check_updates.outputs.has_changes == 'true' |
| 79 | + run: | |
| 80 | + # Check if the branch already exists locally |
| 81 | + if git rev-parse --verify ${{ env.PR_BRANCH }} >/dev/null 2>&1; then |
| 82 | + git checkout ${{ env.PR_BRANCH }} |
| 83 | + # Ensure the branch is up-to-date with main |
| 84 | + git fetch origin main |
| 85 | + git merge origin/main --no-edit |
| 86 | + else |
| 87 | + # Create a new branch |
| 88 | + git checkout -b ${{ env.PR_BRANCH }} |
| 89 | + fi |
| 90 | + |
| 91 | + # Add and commit changes |
| 92 | + git add . |
| 93 | + git commit -m "chore: update git submodules to latest version" |
| 94 | +
|
| 95 | + - name: Push changes |
| 96 | + if: steps.check_updates.outputs.has_changes == 'true' |
| 97 | + run: | |
| 98 | + git push -u origin ${{ env.PR_BRANCH }} --force |
| 99 | +
|
| 100 | + - name: Create pull request |
| 101 | + if: steps.check_updates.outputs.has_changes == 'true' && steps.check_pr.outputs.pr_exists == '0' |
| 102 | + env: |
| 103 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 104 | + run: | |
| 105 | + CHANGES=$(cat submodule_changes.txt) |
| 106 | + PR_BODY=" |
| 107 | + ## Submodule Updates |
| 108 | +
|
| 109 | + This PR updates the following git submodules to their latest versions: |
| 110 | +
|
| 111 | + \`\`\` |
| 112 | + ${CHANGES} |
| 113 | + \`\`\` |
| 114 | +
|
| 115 | + *This PR was automatically generated by GitHub Actions.* |
| 116 | + " |
| 117 | + |
| 118 | + gh pr create --base main --head ${{ env.PR_BRANCH }} --title "${{ env.PR_TITLE }}" --body "$PR_BODY" |
| 119 | +
|
| 120 | + - name: Update existing pull request |
| 121 | + if: steps.check_updates.outputs.has_changes == 'true' && steps.check_pr.outputs.pr_exists != '0' |
| 122 | + env: |
| 123 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 124 | + run: | |
| 125 | + CHANGES=$(cat submodule_changes.txt) |
| 126 | + PR_BODY=" |
| 127 | + ## Submodule Updates |
| 128 | +
|
| 129 | + This PR updates the following git submodules to their latest versions: |
| 130 | +
|
| 131 | + \`\`\` |
| 132 | + ${CHANGES} |
| 133 | + \`\`\` |
| 134 | +
|
| 135 | + *This PR was automatically updated by GitHub Actions on $(date).* |
| 136 | + " |
| 137 | + |
| 138 | + gh pr edit ${{ steps.check_pr.outputs.pr_number }} --body "$PR_BODY" |
0 commit comments