Github deploy to server

David Carr

4 min read - 21st Oct, 2017

This tutorial will show you how to deploy from GitHub to your server using GIT. Deploying with GIT makes it possible to automatically have your server updated every time you commit to GitHub.

Connect to your server

First SSH into your server:

ssh username@domain.com -p 7822 (the 7822 is the port number and may be different on your server)

Go to the directory when you want to have the GitHub repo deployed to. Next get the GitHub ssh url from the GitHub project it will be like https://github.com/username/project.git

To clone the repo type

git clone https://github.com/username/project.git

On the first time you may get this warning:

The authenticity of host 'github.com' can't be established.
RSA key fingerprint is b6:03:0e:39:97:9e:d0:e7:24:ce:a3:77:3e:01:42:09.
Are you sure you want to continue connecting (yes/no)?

Enter yes, next you will get:

Warning: Permanently added 'github.com' (RSA) to the list of known hosts.
Connection closed
fatal: The remote end hung up unexpectedly

Now trying to do a git clone again results in the error:

Initialized empty Git repository in /home/username/public_html/project/.git/
Permission denied (publickey).
fatal: The remote end hung up unexpectedly

The reason for this is the server has not been authenticated again.

Create a public/private key

To get setup follow these steps:

type

ssh-keygen

The output:

Generating public/private rsa key pair.
Enter file in which to save the key (/home/username/.ssh/id_rsa):

Press enter to accept the default path

Enter passphrase (empty for no passphrase):

Press enter to accept no password or enter a password first.

The response will look something like:

The key's randomart image is:
+--[ RSA 2048]----+
|        .. .  .oB|
|       . .. + .++|
|        . o. o...|
|         o   .   |
|        S   .. . |
|           o. o  |
|          o..+   |
|          ....o  |
|           oE.   |
+-----------------+

Get the public key:

Go to .ssh folder

cd ~/.ssh

Open id_rsa.pub in vim

vi id_rsa.pub

Copy the full key. To exit vim press : then type wq

On GitHub go to Settings -> Deploy Keys → Add Deploy Keys

Add a title this is a reference so it can be anything then copy the key into the key box.

Now you should be all set up so once again go to where you want the repo to be pulled into and again type 

git clone https://github.com/username/project.git

the output will be:

initialized empty Git repository in /home/username/public_html/project/.git/
remote: Counting objects: 3048, done.
remote: Compressing objects: 100% (2125/2125), done.
remote: Total 3048 (delta 688), reused 2994 (delta 648)
Receiving objects: 100% (3048/3048), 9.83 MiB | 5.81 MiB/s, done.
Resolving deltas: 100% (688/688), done.

This will create a directory and pull in the repo contents.

Now anytime you want to update go into the directory and type 

git pull

Automatically Deploy 

To get git pull to run on the server every time there is a commit, can be achieved by using Webhooks.

On GitHub go to Settings -> Webhooks → Add Webhook

Enter a url to your server and a file to handle the webhook for this tutorial I will create a file called github.php to the url will be http://example.com/github.php

Enter your url tick just the push event and click Add Webook.

Now head over to your server and create a php file on the server.

touch github.php (you can name it anything but it much match the webhook on GitHub)

As the file created won’t have the right permissions change it with chmod

chmod 644 github.php

Now edit the file with vim:

vi github.php

Press i to go into edit mode

Type:

<?php `git pull`

Then press escape to go into read mode then save and exit by pressing : then type wq and enter.

By using backticks in the file the server will treat the file as a bash script. 

Alternatively, you can use system_exec(‘git pull’)

Now make a change on GitHub and the change will be pushed to your server automatically.

0 comments
Add a comment

Copyright © 2006 - 2024 DC Blog - All rights reserved.