Git Modules #75
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Git Modules | |
on: | |
push: | |
branches: [main] | |
paths: | |
- .gitmodules | |
- .github/workflows/gitmodules.yaml | |
schedule: | |
- cron: "0 9 * * 1-5" # Every weekday at 9am UTC. | |
- cron: "0 13 * * 1-5" # Every weekday at 1pm UTC. | |
- cron: "0 17 * * 1-5" # Every weekday at 5pm UTC. | |
workflow_call: | |
secrets: | |
CFL_BOT_GH_TOKEN: | |
description: "The CFL-bot's GitHub token. Used to sync." | |
required: false | |
workflow_dispatch: | |
jobs: | |
sync: | |
runs-on: ubuntu-22.04 | |
# Sync if: | |
# - the repo's owner is Ocado Tech. | |
# - the repo 'codeforlife-workspace' triggered the event OR the event was | |
# triggered by the repo's default branch. | |
if: | | |
github.repository_owner_id == 2088731 && ( | |
github.event.repository.name == 'codeforlife-workspace' || | |
github.event.repository.default_branch == github.ref_name | |
) | |
steps: | |
- name: π« Checkout Workspace | |
uses: actions/checkout@v4 | |
with: | |
token: ${{ secrets.CFL_BOT_GH_TOKEN }} | |
repository: ocadotechnology/codeforlife-workspace | |
submodules: ${{ github.event.repository.name == 'codeforlife-workspace' }} | |
- name: π Get Submodule Path | |
if: github.event.repository.name != 'codeforlife-workspace' | |
id: get-submodule-path | |
run: | | |
target_url="${{ github.repositoryUrl }}" | |
target_url="${target_url#git://}" | |
readarray -t submodule_lines < <( | |
git config --file .gitmodules --list | \ | |
grep 'submodule\..*\.url=' | |
) | |
for line in "${submodule_lines[@]}"; do | |
url=$(echo "$line" | cut -d'=' -f2-) | |
url="${url#https://}" | |
if [ "$url" = "$target_url" ]; then | |
name=$(echo "$line" | cut -d'.' -f2 | cut -d'=' -f1) | |
path="$(git config --file .gitmodules "submodule.$name.path")" | |
echo "value=$path" >> "$GITHUB_OUTPUT" | |
exit 0 | |
fi | |
done | |
exit 1 | |
- name: π« Checkout Submodule | |
uses: actions/checkout@v4 | |
if: steps.get-submodule-path.conclusion == 'success' | |
with: | |
token: ${{ secrets.CFL_BOT_GH_TOKEN }} | |
path: ${{ steps.get-submodule-path.outputs.value }} | |
ref: ${{ github.event.repository.default_branch }} | |
- name: π Update Submodule HEAD | |
if: steps.get-submodule-path.conclusion == 'success' | |
run: | | |
echo "Adding submodule to workspace's staging area..." | |
git add "${{ steps.get-submodule-path.outputs.value }}" || { | |
echo "Failed to add submodule." | |
exit 1 | |
} | |
- name: π Update All Submodule HEADs | |
if: steps.get-submodule-path.conclusion == 'skipped' | |
continue-on-error: true | |
uses: ocadotechnology/codeforlife-workspace/.github/actions/workspace/process-submodules@main | |
env: | |
REMOTE: origin | |
with: | |
download: false | |
process: | | |
submodule_path="$2" | |
echo "-------------------------------------------------------------" | |
echo "Processing submodule: $submodule_path" | |
if [ ! -d "$submodule_path" ]; then | |
echo "Submodule path '$submodule_path' not found." | |
exit_code=1 | |
continue | |
fi | |
pushd "$submodule_path" > /dev/null | |
function continue_on_error() { | |
echo "$1" | |
exit_code=1 | |
popd > /dev/null | |
continue | |
} | |
echo "Getting default branch from remote '${{ env.REMOTE }}'." | |
default_branch=$( | |
git remote show ${{ env.REMOTE }} 2>/dev/null | \ | |
grep 'HEAD branch' | \ | |
awk '{print $NF}' | |
) | |
if [ -z "$default_branch" ]; then | |
continue_on_error "Default branch not found." | |
fi | |
echo "Checking out '$default_branch'..." | |
git checkout "$default_branch" || \ | |
continue_on_error "Failed to checkout branch." | |
popd > /dev/null | |
echo "Adding submodule to workspace's staging area..." | |
git add "$submodule_path" || \ | |
continue_on_error "Failed to add submodule." | |
# run: | | |
# submodule_paths="$( | |
# git config \ | |
# --file .gitmodules \ | |
# --get-regexp path | \ | |
# awk '{ print $2 }' | |
# )" | |
# if [ -z "$submodule_paths" ]; then | |
# echo "No submodules found in .gitmodules." | |
# exit 1 | |
# fi | |
# exit_code=0 | |
# remote="origin" | |
# for submodule_path in $submodule_paths; do | |
# echo "-------------------------------------------------------------" | |
# echo "Processing submodule: $submodule_path" | |
# if [ ! -d "$submodule_path" ]; then | |
# echo "Submodule path '$submodule_path' not found." | |
# exit_code=1 | |
# continue | |
# fi | |
# pushd "$submodule_path" > /dev/null | |
# function continue_on_error() { | |
# echo "$1" | |
# exit_code=1 | |
# popd > /dev/null | |
# continue | |
# } | |
# echo "Getting default branch from remote '$remote'." | |
# default_branch=$( | |
# git remote show $remote 2>/dev/null | \ | |
# grep 'HEAD branch' | \ | |
# awk '{print $NF}' | |
# ) | |
# if [ -z "$default_branch" ]; then | |
# continue_on_error "Default branch not found." | |
# fi | |
# echo "Checking out '$default_branch'..." | |
# git checkout "$default_branch" || \ | |
# continue_on_error "Failed to checkout branch." | |
# popd > /dev/null | |
# echo "Adding submodule to workspace's staging area..." | |
# git add "$submodule_path" || \ | |
# continue_on_error "Failed to add submodule." | |
# done | |
# exit $exit_code | |
- name: π€ Set up cfl-bot as Git User | |
uses: ocadotechnology/codeforlife-workspace/.github/actions/git/setup-bot@main | |
- name: β¬οΈ Push Submodule HEAD Updates | |
run: | | |
if git diff --cached --quiet; then | |
echo "No submodule HEAD updates to commit in the workspace." | |
else | |
echo "Committing and pushing workspace's changes..." | |
git commit -m "chore(submodules): sync [skip ci]" | |
git push origin HEAD | |
fi |