Synchronize Web Server With Github

Synchronize Web Server With Github

Github is a good way to share his sources, and integrated in a workflow, it is a powerful and very effective tool for working alone, or in groups. It is also a good way to deploy its files and sources on his personal server. Wouldn't be great if every time you"pushiez" of updates to Github, that these new files and changes are automatically updated on your server directly?
This article was originally published on the author's personal blog, edenpulse at the date of Saturday, May 17, 2014.
I'll take for granted the fact that you control a minimum the concept of Git, the command line is not your enemy (there is not much to do there promised!) and that you already have a repo that contains files. Make sure that your Web host allows you to connect to SSH if you are on a shared hosting.

step 1: generate a ssh key.

First log into SSH on your personal server.
$: ssh -l votrelogin adresseduserveur
<entrez votre mot de passe>
$: cd ~/.ssh
$: ls -al
<Liste les fichiers dans votre répertoire .ssh>
If you can see the files named id_dsa.pubor id_rsa.pub , you can go directly to the step #2.
Otherwise:
$: ssh-keygen -t rsa -C "votreemail@example.com"
<cela va créer une clé ssh avec votre email comme label>
#Generating public/private rsa key pair.
# Enter file in which to save the key (/Users/you/.ssh/id_rsa): [Presser entrer]
<A ce moment le serveur va vous demander une passphrase, non obligatoire.>
The server will reply something like this:
Your identification has been saved in /Users/you/.ssh/id_rsa.
# Your public key has been saved in /Users/you/.ssh/id_rsa.pub.
# The key fingerprint is:
# 01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@example.com
We must now add the key to ssh-agent.
$: ssh-add ~/.ssh/id_rsa
This done, to copy the key into your Clipboard, the pbcopy command does it very well.
$: pbcopy < ~/.ssh/id_rsa.pub
Now, go on Github to report this new key.
  • Go to the part of SSH Keys.
  • Click on Add SSH key
  • Put a description for the key, for example "personal server"
  • Paste the key (CMD - V / CTRL-V / right-click: paste)
  • Click Add key
  • Confirm your choice by entering your password to Github.

step 2: test the SSH key.

Very well, it has generated the key, must now test to see if it works well and communicates properly with Github.
Back in the terminal:
$: ssh -T git@github.com
You will certainly see a message such as:
The authenticity of host 'github.com (207.97.227.239)' can't be established.
# RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
# Are you sure you want to continue connecting (yes/no)?
Type in full yes.
You should see this appear:
Hi username! You've successfully authenticated, but Github does not
# provide shell access.
If you have problems, Github provided a link to check that everything is correct here

step 3: clone the repo.

For the moment, we have an SSH key that allows to connect direct to Github. Now, cd to the directory where we want to store the clone of the Github repo parent.
Like what:
$: cd /mondomaine/repertoire-ou-je-veux-stocker-la-chose
Not forgetting that Git creates a directory for the repo, and slides the files inside.
Once in the place or you want to store the source files. Clone the repo:
$: git clone git@github.com:votrenom/votrerepo.git
The address can be easily found on the column on the right of your repo on Github: ssh clone url.
The server should you bringing all your repo files now. Once the operation is complete, check if the files are present. ls is your friend). If this is the case, perfect! There are more than a step!

4th and final step: activate the sync

Very good, your repo is cloned. We must now set up Github for what has each update on your part, it notifies the server and it downloads the new data.
Your new server management
  • Go to the directory of your repo.
  • Create a new file named github - sync.php
  • Open the file and paste it to this:
<?php `git pull`;
  • Note you the direct url to this file, for example: http://demo.demo.com/votrerepo/github-sync.php
  • Make you on Github, in the settings of your repo.
  • Go to the part Webhooks & Services
  • Click Add Webhook
  • Fill the box Payload URL with the url for direct access to your file sync: http://demo.demo.com/votrerepo/github-sync.php
  • In Content Type select application/json
  • 'Secret' leave empty.
  • Select in the choices below Just the push event
  • Check the Active (it's better...)
  • Then complete by clicking on Add webhook.
That's it's over, from now on, when you "commiterez" on your Github, your server ain't alone new files!

No comments

Powered by Blogger.