Anton Egorov
Anton Egorov

Reputation: 1374

Git hooks not firing OR clearing things up with git hooks

I've read many articles on using git hooks, but don't quite get the whole picture. My set-up is the following: I'm working on a project for my company. I write code for a web-app on my mac, then I push it to a repo on bitbucket. In the company's local network, there is a virtual machine running a server. For now I remotely login to this VM and execute a git pull command if I've pushed a new piece of code to the repo. I want to configure a push-to-deploy scheme with git hooks, so basically whenever I push some new code into the repo, the VM should automatically pull it (you know, like some services allow you, e.g. Laravel Forge).

In the virtual machine there is a directory for the repo, say, C:\project (yes, it's running Windows, *sigh*). I've tried putting a post-receive hook into C:\project\.git\hooks directory with some "hello-world" code, like

#!/bin/sh
ECHO "HELLO"
pause

But it's not firing after I git push from my development environment. I feel like I'm doing something totally wrong here, but cannot find any solution. Thanks in advance.

EDIT

Okay, to clear things up, what I want to achieve:

  1. I write some code on my mac and I push to a private repo on bitbucket;
  2. The VM in my company's local network has this repository cloned already;
  3. It figures out that there are some changes on bitbucket;
  4. Using git-hooks, it runs some code to pull from bitbucket, like:

List item

#!/bin/sh
cd ../.. # cd back to project directory
git pull
npm install
bower install
...
  1. I'm even ready to schedule a git fetch task if that's what is need for the case, but no hooks work.

Upvotes: 0

Views: 270

Answers (1)

joran
joran

Reputation: 2883

You should use a post-merge hook on the VM server, since you do git pull there. post-receive is triggerd on the server where you do a git push to.

Move the post-merge hook to your remote repo to where you push.

Edit:

If you do a pull on A from a repo on B then post-merge on A is triggerd. If you push from A to a repo on B then post-receive on B is triggerd.

This may not be an option, but with Bitbucket webhooks you can notify a REST-service when a push event has happend in your project, see https://confluence.atlassian.com/bitbucket/event-payloads-740262817.html#EventPayloads-Push.

There are plenty of alternatives to build a Rest-service that responds on push events (one alternative may be using Phyton scripts and GitPhyton, but there are plenty of other alternatives)

Upvotes: 1

Related Questions