Deploy to Production Server with Git using PHP

Using automatic Webhooks from GitHub.

Assuming:

  • You have an online GitHub repo
  • You have a Hosting Server for Production
  • You’re using PHP

./START

In Your Code (local) Repository

Where you edit your code (ie. local machine, Cloud9)
Create deploy.php
Update Master

1. Add the Deploy script “deploy.php” to your repository.

deploy.php

To do this, first start by creating the file.

$ touch deploy.php

Make sure the deploy script is in a directory that makes it accessable through a URL.

Open the file and past the code from the Gist below.
Save.

2. Add, commit, and push the change to GitHub.

$ git add .
$ git commit -m "Added the git deployment script"
$ git push origin master

On The Production Server

Where the code will be served from.
Install Git.
Setup Git.
Generate new SSH key.

Setup Git

3. Check if Git is installed

$ git --version

4. Git version
If git version is greater than “1.7.9.5" you’re good!
If not, update Git.

If Git is not installed:
https://git-scm.com/book/en/v2/Getting-Started-Installing-Git

Generating a new SSH key

5. Set your Git username on the server:

Paste the text below, substituting “YOUR_USERNAME” with your personal GitHub username.

$ git config --global user.name "YOUR_USERNAME"

Confirm that you have set your GitHub username correctly:

$ git config --global user.name
YOUR_USERNAME

6. Set your GitHub email:

Paste the text below, substituting “YOUR_EMAIL” with your email that is registered with GitHub.

$ git config --global user.email "YOUR_EMAIL"

7. Generating a new SSH key

Paste the text below, substituting “YOUR_EMAIL” with your email that is registered with GitHub.

$ ssh-keygen -t rsa -b 4096 -C "YOUR_EMAIL"

This should create a new SSH key (using the provided email as a label) and put it in the newly created directory ‘.ssh’

Hit enter through the rest of the setup to take the defaults.

Copy the new SSH key

8. Copy the string of characters that are output from executing the following command:

$ cat ~/.ssh/id_rsa.pub

Copy the string starting at ssh-rsa to the end of your email.

On GitHub

Where your repository is stored.
Add the SSH key to your Git profile.
Set up push service Webhook.

Add SSH key

9. Add the SSH Key to your User profile.

GoTo: https://github.com/settings/keys

10. Click “New SSH key

11. Title the key after the name of your server.
This is not required, but it helps keep track of what/where the key is.

12. Past your SSH key into the “Key” box.
The key is the string of characters that was output from Step: 8.

13. Click “Add SSH key

Set up a Webhook

14. Go to your GitHub repository.

15. Click “Settings

16. Click “Webhooks

https://github.com/USER_NAME/REPO/settings/hooks

17. Click “Add webhook

18. Enter the URL
The URL that will be notified by GitHub when an update to the repository is made.

http://www.example.com/deploy.php

You should be able to leave everything else as default.

19. Click “Add webhook”

On the Server

Go to site folder.
Clone Repository.

Go to site folder

20. cd to the site folder.
The directory on the server that will serve the site.

$ pwd
/home2/NAME/public_html/SITE_FOLDER

Clone Repository

21. Copy the SSH direct link from GitHub.
NOT the HTTPS.

22. Clone the Repository to the site folder on the server.

git clone git@github.com:USERNAME/REPO.git
  • Note: You may have to update the directory that the site is being served from to the directory that was just created with git clone. Folder name should be the name of the GitHub repository.

You can test to see if successful by navigating to the deployment script to trigger a pull and see the output.

./END


Was this useful? Let me know.

rioweber.com