Created
April 25, 2022 07:29
-
-
Save Remi-Gau/5a02717f1b96d39d3c583231582fe6ae to your computer and use it in GitHub Desktop.
Github workflow to update submodules
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: update submodules | |
# requires sudmodules URL to be "https..." (no ssh) | |
# | |
# requires submodule to be specified to follow a specific branch (stored in .gitmodules) | |
# | |
# clone them with: | |
# | |
# git submodule add -b branch_to_follow https://github.com/... submodule_path | |
# | |
# or specify it with: | |
# | |
# git config -f .gitmodules submodule.submodule_path.branch branch_to_follow | |
# | |
# Uses the cron schedule for github actions | |
# | |
# https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows#scheduled-events | |
# | |
# ┌───────────── minute (0 - 59) | |
# │ ┌───────────── hour (0 - 23) | |
# │ │ ┌───────────── day of the month (1 - 31) | |
# │ │ │ ┌───────────── month (1 - 12 or JAN-DEC) | |
# │ │ │ │ ┌───────────── day of the week (0 - 6 or SUN-SAT) | |
# │ │ │ │ │ | |
# │ │ │ │ │ | |
# │ │ │ │ │ | |
# * * * * * | |
on: | |
push: | |
branches: | |
- main | |
- master | |
- dev | |
schedule: | |
- cron: "* * * * *" | |
# to trigger update manually from the Action tab in github | |
workflow_dispatch: | |
inputs: | |
log: | |
description: "Log" | |
required: false | |
env: | |
# to update all submodules | |
# SUBMOD_TO_UPDATE: "*" | |
# otherwise use a space separated list of the relative paths of each submodule to update | |
SUBMOD_TO_UPDATE: "lib/sub_3 lib/sub_1" | |
defaults: | |
run: | |
shell: bash | |
jobs: | |
update_submodules: | |
# only trigger update on upstream repo | |
if: github.repository_owner == 'Remi-Gau' | |
runs-on: ubuntu-latest | |
steps: | |
- name: Clone repo | |
uses: actions/checkout@v3 | |
with: | |
submodules: true | |
# check out the correct branch for each submodule and pull them all | |
# https://stackoverflow.com/questions/5828324/update-git-submodule-to-latest-commit-on-origin | |
- name: Update submodules | |
run: | | |
start_dir=$PWD | |
if [ "${SUBMOD_TO_UPDATE}" = "*" ]; then | |
submodules=$(git submodule | awk '{print $2}') | |
else | |
submodules=$(echo -e ${SUBMOD_TO_UPDATE} | sed "s/ /\n/g") | |
fi | |
nb_submod=$(echo "${submodules}" | wc -l) | |
echo -e "\nUPDATING ${nb_submod} SUBMODULES" | |
echo -e "${submodules}" | |
for i in $(seq 1 ${nb_submod}); do | |
path=$(echo -e ${submodules} | awk -v i=${i} '{print $i}') | |
branch=$(git config --get --file .gitmodules submodule.${path}.branch) | |
echo -e "\nswitching submodule ${path} to ${branch}" | |
cd "${path}" || exit | |
git checkout ${branch} | |
cd "${start_dir}" | |
done | |
git submodule update --remote --merge | |
# if there have been changes, | |
# a PR is created using the checkout branch for this workflow | |
# https://github.com/peter-evans/create-pull-request | |
- name: Create Pull-Request | |
uses: peter-evans/create-pull-request@v3 | |
with: | |
commit-message: Update submodules | |
delete-branch: true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When trying to update submodule that tracks something else that the default branch, things don't always got smoothly: