How to deploy from Bitbucket to Amazon S3

Tips & Tricks and Tutorials

As a large hosting provider, Amazon has a number of different methods for storing your website or app. One popular protocol is AWS S3, which can be connected to using a number of popular drag-and-drop clients for Mac and Windows, as well as Amazon's own CLI client. However, doing so to update your site requires a lot of manual work, especially if you're regularly making updates.

Setting up deployments to an S3 Bucket

A service such as DeployHQ saves time and helps to eliminate such problems by streamlining the process. You can set up a repository and a server to be deployed to automatically when a push is received in your repository host, or manually via a couple of clicks in the DeployHQ interface.

This guide assumes you are already 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 also connect repositories hosted by any of the other main repository hosting providers, like GitHub and GitLab.

Connecting to your Bitbucket repository

Once you've clicked Create Project you'll be prompted to login to the repository host, 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 S3 Bucket

Once you've connected to 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 S3 as the protocol.

S3 Bucket config

Next, enter the Bucket name, Access Key ID and Secret, then enter a Path Prefix. The Path prefix is the directory that files will be placed on the Bucket. Normally, this will be a directory like htdocs or public_html.

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

Deploying for the first time

Now that you've configured your 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 Amazon S3

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 Bucket and checking in the home directory, we can see that new files have now been uploaded:

2019-03-26 15:12:30          0 testfile
2019-03-26 15:12:30          0 testfile2
2019-03-26 15:12:30          0 testfile3

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

Using pipelines to compile code when uploading changes from Bitbucket to S3

One limitation of deploying via S3 is the inability to run SSH commands. This is particularly frustrating because most modern projects use technologies like Sass, TypeScript or Webpacker which must be compiled before being used in production.

Developers often get around this issue by committing the compiled assets into Git, however, this can lead to nasty merge conflicts and bloated diffs.

DeployHQ’s Build Pipelines feature lets you specify a series of commands to run in an isolated environment which comes preinstalled with Node, Ruby, PHP and many more languages.

Build commands

Once the commands have been run, DeployHQ will upload the compiled assets to your S3 server. Check out our guides for more information on how you can set up the build pipeline to deploy popular frameworks from Bitbucket to your S3 server.

If you have any questions about this or any other aspect of the DeployHQ service, please get in touch.

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.