Skip to content

update_submodule

update_submodule #73

name: Update Submodules
on:
repository_dispatch:
types: [update_submodule]
permissions:
contents: write
jobs:
update:
runs-on: ubuntu-latest
steps:
- name: Checkout root repo (Default Branch)
uses: actions/checkout@v3
with:
# Start by checking out the default branch to ensure we have a base
ref: ${{ github.event.repository.default_branch }}
token: ${{ secrets.GITHUB_TOKEN }}
fetch-depth: 0
- name: Switch to target branch (Create if missing)
run: |
TARGET_BRANCH="${{ github.event.client_payload.branch }}"
# Check if the branch exists on the remote
if git ls-remote --heads origin "$TARGET_BRANCH" | grep -q "$TARGET_BRANCH"; then
echo "Branch $TARGET_BRANCH exists. Switching..."
git checkout "$TARGET_BRANCH"
else
echo "Branch $TARGET_BRANCH not found. Creating from default..."
git checkout -b "$TARGET_BRANCH"
fi
- name: Sync and update submodules
run: |
git submodule init
TARGET_REPO="${{ github.event.client_payload.repo }}"
TARGET_BRANCH="${{ github.event.client_payload.branch }}"
REPO_NAME=$(basename "$TARGET_REPO")
# Finds the 'submodule.NAME.url' that matches our repo,
# Or gets the 'submodule.NAME.path' for that same NAME.
SUB_NAME=$(git config --file .gitmodules --get-regexp url | grep -i "$REPO_NAME" | head -n 1 | cut -d'.' -f2)
if [ -z "$SUB_NAME" ]; then
echo "Direct URL match failed. Trying a case-insensitive path search..."
SUB_PATH=$(git config --file .gitmodules --get-regexp path | grep -i "$REPO_NAME" | head -n 1 | awk '{print $2}')
else
SUB_PATH=$(git config --file .gitmodules --get "submodule.$SUB_NAME.path")
fi
if [ -n "$SUB_PATH" ]; then
git submodule update --init "$SUB_PATH"
cd "$SUB_PATH"
git fetch origin "$TARGET_BRANCH"
git checkout "$TARGET_BRANCH" || git checkout -b "$TARGET_BRANCH" origin/"$TARGET_BRANCH"
git pull origin "$TARGET_BRANCH"
cd -
else
echo "ERROR: Could not map $TARGET_REPO to a local path in .gitmodules."
echo "--- Current .gitmodules ---"
cat .gitmodules
exit 1
fi
- name: Commit and Push
run: |
git config user.name "GitHub Action"
git config user.email "action@github.com"
git add .
# Only commit and push if there are changes
if ! git diff --cached --quiet; then
git commit -m "Update submodules from ${{ github.event.client_payload.repo }} on branch ${{ github.event.client_payload.branch }}"
# Use -u to set up tracking for newly created branches
git push -u origin HEAD:"${{ github.event.client_payload.branch }}"
else
echo "No changes to commit."
fi