Skip to content

Instantly share code, notes, and snippets.

@oodavid
Forked from aronwoost/README.md
Last active October 11, 2024 00:36
Show Gist options
  • Save oodavid/1809044 to your computer and use it in GitHub Desktop.
Save oodavid/1809044 to your computer and use it in GitHub Desktop.
Deploy your site with git

Deploy your site with git

This gist assumes:

  • you have a local git repo
  • with an online remote repository (github / bitbucket etc)
  • and a cloud server (Rackspace cloud / Amazon EC2 etc)
    • your (PHP) scripts are served from /var/www/html/
    • your webpages are executed by apache
    • apache's home directory is /var/www/
    • (this describes a pretty standard apache setup on Redhat / Ubuntu / CentOS / Amazon AMI etc)

you should be able to do the same with Java, Perl, RoR, JSP etc. however you'll need to recreate the (rather simple) PHP script

1 - On your local machine

Here we add the deployment script and push it to the origin, the deployment script runs git commands to PULL from the origin thus updating your server

Grab a deployment script for your site

See deploy.php

Add, commit and push this to github

git add deploy.php
git commit -m 'Added the git deployment script'
git push -u origin master

2 - On your server

Here we install and setup git on the server, we also create an SSH key so the server can talk to the origin without using passwords etc

Install git...

After you've installed git, make sure it's a relatively new version - old scripts quickly become problematic as github / bitbucket / whatever will have the latests and greatest, if you don't have a recent version you'll need to figure out how to upgrade it :-)

git --version

...on CentOS 5.6

# Add a nice repo
rpm -Uvh http://repo.webtatic.com/yum/centos/5/latest.rpm
# Install git
yum install --enablerepo=webtatic git-all

...using generic yum

sudo yum install git-core

Setup git

git config --global user.name "Server"
git config --global user.email "[email protected]"

Create an ssh directory for the apache user

sudo mkdir /var/www/.ssh
sudo chown -R apache:apache /var/www/.ssh/

Generate a deploy key for apache user

sudo -Hu apache ssh-keygen -t rsa # choose "no passphrase"
sudo cat /var/www/.ssh/id_rsa.pub

3 - On your origin (github / bitbucket)

Here we add the SSH key to the origin to allow your server to talk without passwords. In the case of GitHub we also setup a post-receive hook which will automatically call the deploy URL thus triggering a PULL request from the server to the origin

GitHub instructions

Add the SSH key to your user

  1. https://github.com/settings/ssh
  2. Create a new key
  3. Paste the deploy key you generated on the server

Set up service hook

  1. https://github.com/oodavid/server.com/admin/hooks
  2. Select the Post-Receive URL service hook
  3. Enter the URL to your deployment script - http://server.com/deploy.php
  4. Click Update Settings

Bitbucket instructions

Add the SSH key to your account

  1. https://bitbucket.org/account/ssh-keys/
  2. Create a new key
  3. Paste the deploy key you generated on the server

Set up service hook

  1. Go to: Repo > Admin > Services
  2. Select "POST"
  3. Add the URL to your deployment script - http://server.com/deploy.php
  4. Save

Thanks to DrewAPicture in the comments for this one

4 - On the Server

Here we clone the origin repo into a chmodded /var/www/html folder

Pull from origin

sudo chown -R apache:apache /var/www/html
sudo -Hu apache git clone [email protected]:you/server.git /var/www/html

Rejoice!

Now you're ready to go :-)

Some notes

  • Navigate the the deployment script to trigger a pull and see the output:
  • When you push to GitHub your site will automatically ping the above url (and pull your code)
  • When you push to Bitbucket you will need to manually ping the above url
  • It would be trivial to setup another repo on your server for different branches (develop, release-candidate etc) - repeat most of the steps but checkout a branch after pulling the repo down

Sources

<?php
/**
* GIT DEPLOYMENT SCRIPT
*
* Used for automatically deploying websites via github or bitbucket, more deets here:
*
* https://gist.github.com/1809044
*/
// The commands
$commands = array(
'echo $PWD',
'whoami',
'git pull',
'git status',
'git submodule sync',
'git submodule update',
'git submodule status',
);
// Run the commands for output
$output = '';
foreach($commands AS $command){
// Run it
$tmp = shell_exec($command);
// Output
$output .= "<span style=\"color: #6BE234;\">\$</span> <span style=\"color: #729FCF;\">{$command}\n</span>";
$output .= htmlentities(trim($tmp)) . "\n";
}
// Make it pretty for manual user access (and why not?)
?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>GIT DEPLOYMENT SCRIPT</title>
</head>
<body style="background-color: #000000; color: #FFFFFF; font-weight: bold; padding: 0 10px;">
<pre>
. ____ . ____________________________
|/ \| | |
[| <span style="color: #FF0000;">&hearts; &hearts;</span> |] | Git Deployment Script v0.1 |
|___==___| / &copy; oodavid 2012 |
|____________________________|
<?php echo $output; ?>
</pre>
</body>
</html>
@benyanke
Copy link

I would suggest optionally adding 'git reset --hard,' to ensure the pull happens correctly. If you're using the git workflow properly, it shouldn't matter if local changes are dumped, because all the important files should be in the repo anyways.

@geobde
Copy link

geobde commented Feb 16, 2017

hi guys i try this method with gitlab and dont work you have any ideas?

@webassic15
Copy link

Hi, i am using Ubuntu OS, i cloned repo from bitbucket on local env & set the ssh key access. if i tried to pull / push via terminal then all is working fine. but if i run that script on my local then getting a error : Host key verification failed.
fatal: Could not read from remote repository.

Also whoaim command showing a user - "www-data" but i am using "web-dev" user on my system.

Please help to resolve this issue.

@agrublev
Copy link

agrublev commented Apr 7, 2017

For those interested on ubuntu this is what you have to do (sad nobody added it yet, took me an hour)

Edit /etc/sudoers to add
www-data ALL = NOPASSWD: /usr/bin/git
Then add to the git pull in deploy.php
'sudo git pull'

Without this I could not pull.

@kiran-nani
Copy link

I have Nginx server, how can I do that..

@adrianogoncalves
Copy link

Great script! It's running like a charm with Bitbucket. Thank you for the help!

@talaikis
Copy link

talaikis commented Jun 5, 2017

Isn't it better to name things more appropriately?

https://github.com/[username]/[project]/admin/hooks

oinstead of:

https://github.com/oodavid/server.com/admin/hooks

@Kiodaddy
Copy link

Great script its working fine.

@scriptburn
Copy link

why not use this https://github.com/scriptburn/git-auto-deploy and save typing all these commands?

@jackdeguest
Copy link

If the purpose is to publish a git repository to a live server, then just cut the middle man.
This explains how to use git to publish from the local host to the remote server directly and using different possible branch like beta, live, etc to which one can associate sub domain for validation for example.
https://www.digitalocean.com/community/tutorials/how-to-set-up-automatic-deployment-with-git-with-a-vps

@adnanh
Copy link

adnanh commented Sep 24, 2017

@mckaycr
Copy link

mckaycr commented Sep 25, 2017

This was easy to integrate, thanks. I use 1and1 shared webservice (or whatever its call). Bottomline it doesn't support ssh keys, only passwords unless I upgrade to the next package. Upgrading is probably not an option for my non-profit group. Is there a way to implement this so that the server doesn't need a key to pull changes from my (custom) gitlab server?

@nate-red
Copy link

nate-red commented Oct 23, 2017

This Script is vulnerable. DO NOT USE IT.

Exploitation: https://www.exploit-db.com/ghdb/4593/

@pdulapalli
Copy link

Quick question, is this script easily adapted for Nginx servers?

@waleCloud
Copy link

Quick question please i don't understand the URL to your deployment script - http://server.com/deploy.php

What's my URL to the deploy script?

@udezekene
Copy link

@waleCloud. You are supposed to upload the deploy.php script included here to your server. http://server.com/deploy.php represents the path to the deploy.php file on your own server/domain

@olayinkaokewale
Copy link

This doesn't work if safe_mode is disabled

@azazqadir
Copy link

The PaaS I am using has integrated github feature. This means I only have to connect server with my github repo and i can deploy php application from github to my server automatically. No need for any scripts or command line.

@lagharihelpful
Copy link

Tips For Re-skinning App Source Code
Application re-skinning is a developing pattern. With numerous incredible application source codes out there, it enables you to make extraordinary applications with a low spending plan. To take advantage of your next application re skinning venture we have made a rundown of extraordinary tipsMore Read

@lagharihelpful
Copy link

Application re-skinning is a developing pattern. With numerous incredible application source codes out there, it enables you to make extraordinary applications with a low spending plan. To take advantage of your next application re skinning venture we have made a rundown of extraordinary tips.;Tips For Re-skinning App Source Code from laghari helpfull to my server automatically. No need for any scripts or command line.

@pepebm
Copy link

pepebm commented May 7, 2019

I need assistance.
I am trying to implement a rather similar deploy script where I use git reset --hard HEAD instead of git submodule. The webhook does send the request when committed (I verified this by creating a log.txt) the thing is, every time the script executes it git doesn't update to the latest version and it outputs that the HEAD is up to date, which is always 1 commit behind.

@azhar25git
Copy link

Here is an Update [August 2020] (only github part is updated):
https://gist.github.com/azhar25git/13d1311bf7acde0326f694587dcd9678

@geraldnwanze
Copy link

how would one run extra scripts after pulling?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment