Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pull git submodule automatically?

I am assigned a project, where I will be doing backend development and another person will be handling frontend development. There is a requirement that the frontend developer does not have access to the development server, nor the project code. So, we decided to create another repository just for frontend.

The problem is that I don't know how to make submodule to immediately pull from the remote server when something is committed to the remote repo. I need the developer to commit and immediately see the change in the frontend.

I have been reading about git hooks and I have found that I can do this using post-receive hook. However, it doesn't seem to work. Here is what I did:

  1. From the main project's root directory, I found the following directory /.git/modules/submodule-directory/hooks
  2. I created post-receive file with executable permission allowed.
  3. Contents of post-receive file:

    #!/bin/sh
    git pull
    
  4. Pushed a change from my local machine to the submodule repo but nothing happened.

What am I doing wrong?

EDIT: I am going to clarify some points regarding my question.

I have two repositories: The "main" repository, which is the entire project; and another repository, which is frontend stuff (views, assets etc). Let's call this "frontend" repository.

To my main repository, I added frontend repo as a submodule, located inside /submodules/frontend. This way, the outsourced frontend developers will not be able to see anything about the actual project. They will only see what they are working on.

This actually works perfectly in terms of access control and I can easily monitor the frontend development's progress separately from project. But there is one problem that I don't know how to do.

Our project has two separate servers - a development server and a production server. The development server needs to always be in the master branch for both main repo and frontend repo. This way, both frontend team and the backend team will be able to see their updates. Even though frontend team mainly works with templates, the templates have access to data that is stored in the database and are using a lot of stuff that are integrated to backend (e.g models).

In order for this "system" to work, I need the frontend repo to always fetch and merge whenever there is a new commit in "frontend" remote repo. In other words, if the frontend team does git commit and git push, the "frontend" repo submodule in the development server needs to immediately git pull. So, when the frontend guys go test their stuff, they will immediately see the latest changes.

I read that post-receive hook can do that but I need this hook to exist only for submodule. So, after some digging I found that all the submodule hooks exist in /.git/modules/submodules/frontend/hooks. So, I created post-receive hook in that directory with the following code:

#!/bin/sh
git pull

But this didn't work. My question is how to make the hooks work with post-receive inside submodule?

I know the route that I have taken is far from perfect but this is the only way that makes sense to me when it comes to allowing devs to only access one directory without having access to the development server.

I hope this makes sense.

like image 685
Gasim Avatar asked Oct 18 '22 08:10

Gasim


1 Answers

When you git pull, this is (basically) equivalent to git fetch && git merge. This will not update submodules.

If I understand your needs, What you want can be done with the commands git submodule update (or git submodule foreach 'git pull') in your post-receive hooks (in the main .git/hooks directory).

ADD (based on your editions): based on what I understood, you need to add the post-receive hook on the gitlab frontend repository, and your hook must execute the command git submodule update (or maybe git submodule foreach 'git fetch && git merge' in the development server (with a ssh command or something).

But it may be easier to rethink how your development server is installed, you can install the 2 repos as totally separated (not frontend being a submodule), and maybe adding the submodule/frontend to your .gitignore directory. This not necessarily means you have to remove frontend from the submodule, but just not using it in your development server.

like image 153
Asenar Avatar answered Oct 21 '22 06:10

Asenar