Dev Notes

Software Development Resources by David Egan.

Syncing with Bitbucket

BASH, Linux, Sysadmin, Tools, WordPress, git
David Egan


Bitbucket is a great resource - it provides free multi-user version control for groups of up to five developers.

It can also be a useful way of deploying to a development (or production) server.

The work flow is as follows:

  1. Connect a local project directory with a remote Bitbucket repo
  2. Clone the repo to a project directory on a remote server
  3. Push changes from the local working directory to the Bitbucket remote repo
  4. Pull changes from the remote Bitbucket repo to the remote development server

This allows all changes to be properly managed (for example, by means of git), and allows different developers to work on different branches. It has great tools for reviewing and merging changes.

It’s a great set-up for small teams, and is free.

Easy Pushing

This can be partially automated. Firstly, set up SSH keys to allow communication between:

  • Your local machine and Bitbucket
  • The remote server and Bitbucket

There are really good instructions for setting up SSH keys here.

Next, we’ll create an alias - “dir-push” that will manage the push to the repo.

We’ll put this alias in ~/.bashrc so that it is globally accessible.

Open ~/.bashrc and add the following lines:

#Push theme to Bitbucket
alias dir-push="cd /var/www/path/to/development/dir; add -A; git commit; git push origin master"

Close the terminal and reopen.

Now when you type “dir-push” on the command line, you’ll be prompted for a git commit message.

This is quite a painless way to manage the push to the repo - you just need to type a single command and your commit message.

I advise including a passphrase when you set up SSH keys - in case someone maliciously accesses your development machine - in which case you’ll also be prompted for the passphrase during the push.

Easy Pulling

The idea here is to connect to a remote server by means of SSH, and pull the latest version of the Bitbucket repo to the relevant directory on the development server. This is a bit more complicated.

It can be achieved easily with a GitHub repo, since you aren’t prompted for authentication when pulling. I’ve achieved this like so:


# Pull the repo from GitHub to the development server
# The <<EOF part tells the shell that you're going to enter multi-lines until the ending EOF tag
ssh username@ <<EOF
cd /var/www/path/to/development/directory
git pull
echo "You should be logged out of the VPS"

This won’t work with Bitbucket - the required verification causes the script to stall.

The solution:

  1. Create a shell script and add this to /usr/local/bin on the remote server
  2. Create a shell script on the local machine (again, in /usr/local/bin) which starts an interactive SSH session and calls the remote script.

The Local Script

Create a suitably named file in the local /usr/local/bin:

sudo nano /usr/local/bin/pull-project

Add the following code:

# Pull the Bitbucket repo to the development server
# Open an SSH session and run the shell script "remote-project-pull"
ssh username@ -t "remote-project-pull"

The Remote Script

Create a suitably named file in the remote /usr/local/bin:

sudo nano /usr/local/bin/remote-project-pull

Add the following code:

cd /var/www/path/to/development/directory
git pull
echo "End Script - logging out of VPS"
echo "Logged out of VPS"

Now, when you’re working on your local development machine, you just enter the command “pull-project”. A SSH session will be opened, and the repo will be pulled from Bitbucket.

Again, if you have set up a passphrase for the SSH connection between the remote server and Bitbucket (recommended), you’ll be prompted for the passphrase.

Other Options

There are other options that automate the deployment from Bitbucket to the development server. This method looks interesting.

I can see how that would be pretty useful in some circumstances - but I think this method is simple, leaves you with lots of control, and it’s just a couple of extra commands in your workflow. You can commit to the Bitbucket repo without clients necessarily seeing your work-in-progress.

comments powered by Disqus