Search code examples
gitcontinuous-integrationgit-submodulesgithooks

How to force my main repo to update when a submodules is updated?


I have a git repo which consists of multiple git submodules from different repos (which I have access to apply any changes regarding CI or Hooks)

My question is how can my main repo get updated automatically and have the latest changes from my submodules master branch whenever there is a commit to it?

I was thinking about hook and CI, but not sure how!

The reason we are not using a package manager for this specific module is that we need the source code and also we do the compilation simultaneously with the main repo code.


Solution

  • After two days of research and trying to find a solution and by keeping in mind that maybe using a package manager could be the best solution, I ended up with this .gitlab-ci.yml change which I applied to my submodule repo:

    android:
      stage: deploy
      script:
        - cd ~
        - mkdir .ssh
        - echo SSH_KEY_TO_ACCESS_GIT > ~/.ssh/id_rsa
        - chmod 600 ~/.ssh/id_rsa
        - eval `ssh-agent -s`
        - ssh-add ~/.ssh/id_rsa
        - ssh-keyscan -t rsa YOUR.GIT.com > ~/.ssh/known_hosts
        - git config --global user.email "[email protected]"
        - git config --global user.name "CI"
        - git clone --single-branch -b develop [email protected]_REPO.git
        - cd YOUR_REPO_FOLDER
        - git submodule init
        - git submodule update
        - cd YOUR_SUBMODULE_FOLDER
        - git fetch --all
        - git reset --hard origin/master
        - cd ..
        - git add .
        - git commit -m "Update YOUR SUBMODULE automatically to latest"
        - git push origin develop
      only:
        - master
    

    This task will pull my repo at the end of CI tasks and update the submodule and push a commit into the repo.

    Keep in mind you need to add an ssh key to your account to allow CI to push into your repo.

    I am still open to any suggestion which is more convenient.