Effortless Deployment: Automate Your Workflow with Git and Bitbucket Hooks

Ovilash JaluiOvilash Jalui
4 min read

Introduction

Git has long been hailed as one of the most powerful version control systems. Pair it with Bitbucket, which offers unlimited free private repositories, and you’ve got a strong foundation for collaborative development. But pushing your changes and manually updating your server every time? That’s outdated. Enter Bitbucket Hooks, the simple solution for automating deployment.

With Bitbucket Hooks, every time you push to your repository, your server updates automatically—no manual uploads, no logging in, no hassle.


What Are Bitbucket Hooks?

Bitbucket Hooks provide an easy way to trigger scripts after each push. The one we need is a POST hook.

A POST hook sends a JSON payload containing commit and repository data to a specified URL every time a push occurs. This means we can set up a script to receive this payload, validate it, and deploy the latest changes.

Steps to Set Up Deployment

  1. Create a deployment script on your server.

  2. Set up a Bitbucket POST hook to call this script.

  3. Configure SSH access so the script can pull from Bitbucket without requiring a password.

  4. Automate the process so every push updates your server instantly.


Setting Up the Deployment Script

1. Create the Deployment Script

First, create a new PHP script in your server’s deployment folder. Use an obscure filename for security, like deploy-xyz123.php.

<?php
  $payload = '';
  if (isset($_POST['payload'])) {
    $payload = json_decode($_POST['payload']);
  } else {
    return false;
  }

  $repo = $payload->repository;

  exec('git init && git remote add origin git@bitbucket.org:' . $repo->absolute_url . '.git . && git pull origin master');

  file_put_contents('bitbucket-deployment.log', 'Last run on: ' . date('m/d/Y h:i:s a'), FILE_APPEND);
?>

This script does the following:

  • Decodes the JSON payload sent by Bitbucket.

  • Extracts repository details.

  • Pulls the latest changes from Bitbucket using git pull.

  • Logs each deployment to bitbucket-deployment.log.

Save this script on your server where it can be accessed via a URL (but isn’t publicly known).


2. Configure a Bitbucket POST Hook

To make Bitbucket execute your script:

  1. Go to your repository in Bitbucket.

  2. Navigate to Repository Settings > Webhooks.

  3. Click "Add webhook".

  4. Set the URL to your deployment script (e.g., https://yourdomain.com/deploy-xyz123.php).

  5. Save the webhook.

Now, every time you push changes, Bitbucket will call your script, automatically pulling the latest updates.


Setting Up SSH Access

1. Identify Your PHP User

To ensure your server has access to your repository, determine which user PHP runs as:

<?php echo exec('whoami'); ?>

Depending on your configuration, it might be apache, www-data, or something else. We’ll use www-data in this example.

2. Create an SSH Key for Your PHP User

Run the following command to generate an SSH key:

sudo -u www-data ssh-keygen -t rsa
  • Leave the passphrase blank.

  • The key will be stored in ~/.ssh/id_rsa-git (adjust path as needed).

3. Add Bitbucket to the SSH Config

Create an SSH config file:

sudo nano /var/www/.ssh/config

Add the following lines:

Host bitbucket.org
    IdentityFile /var/www/.ssh/id_rsa-git

Save the file and set permissions:

sudo chmod 600 /var/www/.ssh/config

Now, your server can access Bitbucket without requiring a password.


Debugging the Deployment

If something goes wrong, here are a few debugging techniques:

1. Log the Payload Data

Modify your script to save the payload:

<?php
  if (isset($_POST['payload'])) {
    file_put_contents('payload.log', $_POST['payload']);
  }
?>

This allows you to inspect the actual data Bitbucket is sending.

2. Output Shell Command Results

Change your execution command to display output:

<?php
echo exec('git pull origin master 2>&1');
?>
  • 2>&1 redirects errors to standard output.

  • echo prints the output to the browser for easy debugging.


Wrapping Up

With this setup, you’ve fully automated your deployment process. Every push triggers an update, eliminating the need for manual uploads. No more FTP, no more forgotten changes—just seamless, effortless deployments.

Take it a step further by integrating this with CI/CD pipelines or adding rollback mechanisms for even greater reliability.


Further Reading

  • Automated Git Deployments from Bitbucket

  • Using Bitbucket for Automated Deployments

Now go forth and code fearlessly!

10
Subscribe to my newsletter

Read articles from Ovilash Jalui directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ovilash Jalui
Ovilash Jalui

My name is Ovilash Jalui, and I am a Full Stack Developer. I create web apps that are simple, user friendly, and help businesses grow online. My main focus is building websites for businesses and individuals who want to stand out and connect with more people. Whether it’s bringing a startup’s idea to life with apps and digital products or using AI to make them smarter, I use the latest technology and strategies to boost online presence and help businesses grow faster.