Skip to content

Add Harvard CS50

Add Harvard CS50 #38

name: Update Submodules
on:
push:
branches:
- '**' # This runs on any branch
jobs:
update-submodules:
runs-on: ubuntu-latest
steps:
# Step 1: Checkout the repository with submodules
- name: Checkout repository
uses: actions/checkout@v3
with:
submodules: 'recursive' # Ensure submodules are checked out
fetch-depth: 0 # Fetch full history to ensure we can compare commits
# Step 2: Update submodules only if not at the latest commit
- name: Selectively Update Submodules
id: submodule-update
run: |
# Initialize submodules
git submodule update --init --recursive
# Track if any submodules were updated
updated_submodules=0
# Iterate through each submodule
git submodule foreach '
# Get the current commit hash of the submodule
current_commit=$(git rev-parse HEAD)
# Fetch the latest commits from origin
git fetch origin main
# Get the latest commit hash from origin/main
latest_commit=$(git rev-parse origin/main)
# Compare current and latest commits
if [ "$current_commit" != "$latest_commit" ]; then
echo "Updating submodule: $path"
git checkout main
git pull origin main
updated_submodules=$((updated_submodules + 1))
else
echo "Submodule $path is already up to date"
fi
'
# Set output and environment variable for subsequent steps
if [ $updated_submodules -gt 0 ]; then
echo "submodules_updated=true" >> $GITHUB_ENV
echo "Updated $updated_submodules submodules"
else
echo "submodules_updated=false" >> $GITHUB_ENV
echo "No submodules needed updating"
fi
# Step 3: Commit changes only if submodules were updated
- name: Commit changes
if: env.submodules_updated == 'true'
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git add .
git commit -m "CI: Update specific submodules to latest main branch commits" || echo "No changes to commit"
# Step 4: Pull the latest changes from the remote branch before pushing
- name: Pull the latest changes
if: env.submodules_updated == 'true'
run: git pull origin ${{ github.ref_name }} --rebase --allow-unrelated-histories
# Step 5: Push the changes back to the repository
- name: Push changes
if: env.submodules_updated == 'true'
env:
TOKEN: ${{ secrets.PAT_TOKEN }}
run: |
git remote set-url origin https://x-access-token:${TOKEN}@github.com/${{ github.repository }}.git
git push origin HEAD