Git Modules #812
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_APP_PRIVATE_KEY: | |
| description: "The private key of CFL's GitHub App. Used to sync." | |
| required: true | |
| 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: 🤖 Set up CFL bot | |
| uses: ocadotechnology/codeforlife-workspace/.github/actions/bot/setup@main | |
| id: setup-bot | |
| with: | |
| cfl-app-private-key: ${{ secrets.CFL_APP_PRIVATE_KEY }} | |
| repositories: codeforlife-workspace | |
| - name: 🛫 Checkout Workspace | |
| uses: actions/checkout@v5 | |
| with: | |
| token: ${{ steps.setup-bot.outputs.token }} | |
| repository: ocadotechnology/codeforlife-workspace | |
| submodules: ${{ github.event.repository.name == 'codeforlife-workspace' }} | |
| persist-credentials: false | |
| - 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@v5 | |
| if: steps.get-submodule-path.conclusion == 'success' | |
| with: | |
| token: ${{ steps.setup-bot.outputs.token }} | |
| path: ${{ steps.get-submodule-path.outputs.value }} | |
| ref: ${{ github.event.repository.default_branch }} | |
| persist-credentials: false | |
| - 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." | |
| - 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 remote set-url origin https://x-access-token:${{ steps.setup-bot.outputs.token }}@github.com/ocadotechnology/codeforlife-workspace.git | |
| git commit -m "chore(submodules): sync [skip ci]" | |
| git push origin HEAD | |
| fi |