|
| 1 | +name: update submodules |
| 2 | + |
| 3 | +# requires sudmodules URL to be "https..." (no ssh) |
| 4 | +# |
| 5 | +# requires submodule to be specified to follow a specific branch (stored in .gitmodules) |
| 6 | +# |
| 7 | +# clone them with: |
| 8 | +# |
| 9 | +# git submodule add -b branch_to_follow https://github.com/... submodule_path |
| 10 | +# |
| 11 | +# or specify it with: |
| 12 | +# |
| 13 | +# git config -f .gitmodules submodule.submodule_path.branch branch_to_follow |
| 14 | +# |
| 15 | + |
| 16 | + # Uses the cron schedule for github actions |
| 17 | + # |
| 18 | + # https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows#scheduled-events |
| 19 | + # |
| 20 | + # ┌───────────── minute (0 - 59) |
| 21 | + # │ ┌───────────── hour (0 - 23) |
| 22 | + # │ │ ┌───────────── day of the month (1 - 31) |
| 23 | + # │ │ │ ┌───────────── month (1 - 12 or JAN-DEC) |
| 24 | + # │ │ │ │ ┌───────────── day of the week (0 - 6 or SUN-SAT) |
| 25 | + # │ │ │ │ │ |
| 26 | + # │ │ │ │ │ |
| 27 | + # │ │ │ │ │ |
| 28 | + # * * * * * |
| 29 | + |
| 30 | +on: |
| 31 | + push: |
| 32 | + branches: |
| 33 | + - main |
| 34 | + - master |
| 35 | + - dev |
| 36 | + schedule: |
| 37 | + - cron: "0 0 1 * *" |
| 38 | + |
| 39 | + # to trigger update manually from the Action tab in github |
| 40 | + workflow_dispatch: |
| 41 | + inputs: |
| 42 | + log: |
| 43 | + description: "Log" |
| 44 | + required: false |
| 45 | + |
| 46 | +env: |
| 47 | + # to update all submodules |
| 48 | + SUBMOD_TO_UPDATE: "*" |
| 49 | + # otherwise use a space separated list of the relative paths of each submodule to update |
| 50 | + # SUBMOD_TO_UPDATE: "lib/sub_3 lib/sub_1" |
| 51 | + |
| 52 | +defaults: |
| 53 | + run: |
| 54 | + shell: bash |
| 55 | + |
| 56 | +jobs: |
| 57 | + update_submodules: |
| 58 | + # only trigger update on upstream repo |
| 59 | + if: github.repository_owner == 'cpp-lln-lab' |
| 60 | + |
| 61 | + runs-on: ubuntu-latest |
| 62 | + |
| 63 | + steps: |
| 64 | + |
| 65 | + - name: Clone repo |
| 66 | + uses: actions/checkout@v3 |
| 67 | + with: |
| 68 | + submodules: true |
| 69 | + |
| 70 | + # check out the correct branch for each submodule and pull them all |
| 71 | + # https://stackoverflow.com/questions/5828324/update-git-submodule-to-latest-commit-on-origin |
| 72 | + - name: Update submodules |
| 73 | + run: | |
| 74 | + start_dir=$PWD |
| 75 | + if [ "${SUBMOD_TO_UPDATE}" = "*" ]; then |
| 76 | + submodules=$(git submodule | awk '{print $2}') |
| 77 | + else |
| 78 | + submodules=$(echo -e ${SUBMOD_TO_UPDATE} | sed "s/ /\n/g") |
| 79 | + fi |
| 80 | + nb_submod=$(echo "${submodules}" | wc -l) |
| 81 | + echo -e "\nUPDATING ${nb_submod} SUBMODULES" |
| 82 | + echo -e "${submodules}" |
| 83 | + for i in $(seq 1 ${nb_submod}); do |
| 84 | + path=$(echo -e ${submodules} | awk -v i=${i} '{print $i}') |
| 85 | + branch=$(git config --get --file .gitmodules submodule.${path}.branch) |
| 86 | + echo -e "\nswitching submodule ${path} to ${branch}" |
| 87 | + cd "${path}" || exit |
| 88 | + git checkout ${branch} |
| 89 | + cd "${start_dir}" |
| 90 | + done |
| 91 | + git submodule update --remote --merge |
| 92 | +
|
| 93 | + # if there have been changes, |
| 94 | + # a PR is created using the checkout branch for this workflow |
| 95 | + # https://github.com/peter-evans/create-pull-request |
| 96 | + - name: Create Pull-Request |
| 97 | + uses: peter-evans/create-pull-request@v3 |
| 98 | + with: |
| 99 | + commit-message: Update submodules |
| 100 | + delete-branch: true |
0 commit comments