Skip to content

Instantly share code, notes, and snippets.

@rodrigoea
Last active October 19, 2015 01:32
Show Gist options
  • Save rodrigoea/e12a2bf8a7309d2fd65e to your computer and use it in GitHub Desktop.
Save rodrigoea/e12a2bf8a7309d2fd65e to your computer and use it in GitHub Desktop.
How to use the simple and perfect Deployment Workflow with Git and SSH ~ Automated Deploy (Bitbucket)

#Learn how to use the simple and perfect Deployment Workflow with Git and SSH

###Requirements

  • Bitbucket Account (You can also use Github or other GIT service).
  • SSH access to the publishing server
  • Have tool git installed on the local environment and server

ro ##Step 1 - Create a local git repo

$ cd path/to/project
$ git init
$ git add .
$ git commit -m 'First commit'

Those familiar with Git should feel right at home. We're creating a Git repo, adding all the files to the staging area, and are then creating our first commit.


##Step 2 - Uploading to Bitbucket

In this step we will upload the project to Bitbucket. That way, we can easily call a git pull to download this project from any computer/server we wish.

Begin by creating a new Git repository.

Create a new repo

Next, you'll need to fill in some details about your project as owner, name, description, etc.. That's simple:

And finally, since we're already working with an existing Git repo, we only need to run:

$ git remote add origin [email protected]:username/test.git
$ git push -u origin master

With that out of the way, our project is now available on Bitbucket. That was easy!


##Step 3 - SSH

Now we need to clone the repository on the production server. I'm assuming you've already set up SSH access.

Let us access the server via SSH as follows:

$ ssh [email protected]
$ <enter your host password>

Now we are connected!

Next, we cd to the directory of where we wish to store the project.
For example, this will be:

$ cd path/to/project

Modify this according to your own directory structure.

####Git clone Let's clone the Bitbucket repo now.

$ git clone [email protected]:username/repo.git

##Step 4 - Creating a Connection

if we update our source files on our local machine, and then push the changes to Bitbucket, These changes will certainly not be reflected on our server.

Let's create a hook that sends a POST request to the specified page every time you make a git push to your Bitbucket repo. This is exactly what we need!

To make this work, we'll need to create a file that will handle the process of performing the git pull. Create a new file called GitPull.php (or anything you wish - preferably more vague).

<?php `git pull`; ?>

Pay close attention cause the single quotes above are back-ticks. When you wrap a sequence in back-ticks, in PHP, it'll be treated as a Bash script. In fact, it's identical to using the bash_exec function.

Save that file, and upload it to the project directory on your server. The recommended is to do a git push with the inclusion of the script and manually via SSH do the git pull. Do not even think about doing FTP. \o/

When finished, go to repo Settings on Bitbucket then click in Hooks. Choose POST and click Add Hook. Copy the url to that file, and paste it into the textbox. For example: http://example.com/project/GitPull.php

Creating Hook

When done, every single time you make a git push to your Bitbucket repo, that file will be called, and the project directory on your server will auto-update, without having to do anything else. It's like magic!


Ps.: Alternatively, If you don't want it to update the live server on every commit, but still want a quick way for the live server to pull, just add a link to the bash script to your bookmarks bar.


##Enjoy ;)

Created by Rodrigo Antinarelli \o/

Thanks to TutsPlus for giving this tutorial.

@samwx
Copy link

samwx commented Sep 2, 2014

Great!

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