How to perform automatic pull with Git?

Asked

Viewed 1,771 times

13

Imagine that I have a test environment for a web application, where there is a cloned Github repository that is directly inside the server, and any modification in it already results in a real-time modification when the user accesses it via browser.

My question is, is there any way to add a Listener in this local repository so that whenever there is change in the official repository (Github) it already downloads the changes automatically? Illustration to facilitate understanding:

I’ve been doing some research and I haven’t found anything about it, but I know that to update my local repository, the command would be:

$ git pull origin master

Is there a Git or Gulp solution for this type of Listener?

  • I don’t know if that would be the case, but have you thought of a cron task for that? Another option would be to search for Git Hooks.

1 answer

11


It is possible to use a web hook provided by Github API. He is your Listener.

Documentation Git for hooks configuration.

To get what you want automatically, you need to write a script. An example.

A simple example:

Create a script do the pull. Example:

#!/bin/bash
echo "Content-type: text/plain"
echo ""
cd ~/project
git pull

Register the URL of this script on Github on option Webhooks & Services (in the Settings) creating a new webhook and accepting everything as the default. It will be called when a change occurs in the repository.

Basically the only action really needed in the script is the git pull. Then it depends on how you want to do the deploy and how your Git is configured.

Obviously the script needs to be accessible by HTTP and proper execution permission is required.

  • So if I understand correctly it’s the same Webhook that will access a URL of mine that would trigger the action I want, correct?

  • Exactly. Your script is the action to be performed by the Github event. The script is who signs the event and is listening when he has something to tell you. Of course script will run on your server.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.