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:
- Connect a local project directory with a remote Bitbucket repo
- Clone the repo to a project directory on a remote server
- Push changes from the local working directory to the Bitbucket remote repo
- 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.
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:
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.
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:
This won’t work with Bitbucket - the required verification causes the script to stall.
- Create a shell script and add this to /usr/local/bin on the remote server
- 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:
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:
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.
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