Header

How to automatically deploy from Bitbucket to an SSH/SFTP server

Tutorials

At the moment you may be updating your site via an SFTP client using a simple drag and drop method, or by logging into your server and running a Git command such as git fetch or git pull.

The latter of those two methods might be preferable for both speed and reliability, as it stops you from missing files that should be uploaded, and is faster than a drag-and-drop SFTP method. However, it's still a manual process and requires you to log in to the server.

Using DeployHQ

In order to use a service such as DeployHQ to streamline your deployment process, you'll need to have an SSH/SFTP server in place, and a Git repository.

This guide assumes you are using BitBucket for code hosting and version control management using Git.

Creating a new project in DeployHQ

Once you've signed up for a new DeployHQ account, you can click the New Project button at the top of the screen to get started.

You’ll be happy to hear that we have a generous free plan that lets you deploy 1 project up to 10 times a day!

New project

Enter a name for your project and choose Bitbucket. You can connect to a repository on any of the main hosting providers - Bitbucket, Codebase, GitHub and GitLab, but if you host a repository elsewhere you can configure it manually.

Connecting to your Bitbucket repository

Once you've clicked Create Project you'll be prompted to login to Bitbucket, then you can choose your account, after which you'll be presented with a list of repositories that you have access to.

Repository list

Select the repository, then DeployHQ will automatically connect to it and add an SSH public key to be able to access it. It's recommended that you keep the add a webhook option checked, as that will allow us to set up automatic deployments later in this guide.

Configuring your SSH/SFTP server

Once you’ve connected your Bitbucket repository, you'll be automatically directed to the New Server screen. Here, you'll be able to enter a name for your server, then choose SSH/SFTP as the protocol.

We've already gone ahead and provisioned an Ubuntu server and added a deployhq user, so we'll enter the IP address of the server and the username, then tick the option to authenticate using an SSH key.

SSH/SFTP details

Our unique SSH public key for the project is now displayed, which we'll copy to our clipboard, then paste onto a new line in ~/.ssh/authorized_keys after logging into the server as the deployhq user.

The deployment path is where files will be placed on the server. Normally, this will be a directory like /var/www or public_html. We're uploading our files to the deployhq user's home directory, so we'll leave this blank on this occasion.

Once those details have been added, simply click Create Server to finish.

Deploying for the first time

Now that we've configured our repository and server, we can run our first deployment to get started. Click the Deploy for the first time link to the top right of your project:

First deployment

Then, you can check the information before clicking the green Deploy button to the bottom right. You'll notice that the chosen server and branch are selected automatically, and the start revision says The very first commit. This is because no previous commits have been deployed. The end revision will be automatically selected as the latest found in the chosen branch.

Once you've clicked the Deploy button, the deployment will start. In this instance the process will be simple, all files will be transferred to your deployment directory.

First deployment complete

Once that's done, you can review the log and see a full list of which files were transferred across.

Push to upload changes from Bitbucket to SSH/SFTP

After your first deployment, you'll be able to push a new commit to repository, and see the changes uploaded. Additionally, we'll enable automatic deployments so that we don't need to login to DeployHQ to start a deployment each time.

Head to the Automatic Deployments page on the left hand side, then enable the option next to your server:

Auto deployment

After pushing a test commit, the deployment starts automatically and only the files changed in that commit are uploaded. Viewing the deployment log shows the single file uploaded, and that the deployment was automatic:

New deployment

After logging into our server and checking in the home directory, we can see that new files have now been uploaded:

-rw-rw-r-- 1 deployhq deployhq    0 Mar 21 15:56 testfile
-rw-rw-r-- 1 deployhq deployhq    0 Mar 21 15:56 testfile2

That's it! We've now improved our deployment process massively by setting up automatic deployments using DeployHQ. We no longer have to upload everything at once or worry about missing anything - only files that have changed will be uploaded.

Further reading

DeployHQ has a number of very useful features to help with your deployments in general that you might wish to find out more about:

  • Compiling assets and javascript with the Build pipeline
  • Running commands on your server with SSH commands
  • Stop certain files from being uploaded in deployments using Excluded files
  • Keep important files safely away from your repository using Config files
  • Keep your team in the loop and run other useful tasks during a deployment with our Integrations

A little bit about the author

I'm Carla and I work in the support team for Krystal Labs and Dial 9. When I'm not helping customers with their questions I enjoy spending time with my dog and cats.

Proudly powered by Katapult. Running on 100% renewable energy.