-
Notifications
You must be signed in to change notification settings - Fork 0
143 lines (117 loc) · 4.82 KB
/
update-submodules.yml
File metadata and controls
143 lines (117 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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 "jadit19@gmail.com"
- 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"