Automatic deployment for bitbucket/github web-based projects

Based on «Automated git deployment» script by Jonathan Nicoal. See also BitBucket Sync by alixandru.

Some fragments of this manual was taken from Jonathan Nicoal's documentation page.

  • Version: 0.3.0
  • Last changes: 2019.01.25
  • See Changelog

Documentation is in progress.


  • Fixed new webhooks interface (stream instead of POST). See discussion on another synchronizator bitbucket-sync by alixandru.
  • Added support for multiple projects. See array $PROJECTS in config.sample.php.
  • Optional fetching or cloning repositiories demand on their presence.
  • Project and repository folders automaticly creating if they're not exists. (You need no to create empty folders before operations and can to reset and initiate full reload by simply removing entire repository/project folders.)
  • Post hook command execution.


  • PHP 5.3+;
  • Git installed;
  • Shell access;
  • PHP exec function;
  • SSH key pair for bitbucket/github created with empty passphrase;


For your server to connect securely to Bitbucket without a password prompt, it needs to use an SSH key.

On your server navigate to the ~/.ssh directory of the user that PHP runs under. You will need to create the user's .ssh directory if it doesn't exist. At a shell prompt type:

cd ~/.ssh
ssh-keygen -t rsa

When prompted either accept the default key name (id_rsa) or give your key a unique name. Press enter when asked for a passphrase, which will generate a passwordless key. Usually this isn't recommended, but we need our script to be able to connect to Bitbucket without a passphrase.

A public and private key pair will be generated. Copy your public key — the one with a .pub extension — to the clipboard. On the Bitbucket website navigate to Account > SSH Keys, and choose to add a new key. Paste in your public key and save it.

Back on your server, edit your ~/.ssh/config file to add or as a host. This ensures that the correct key is used when connecting by SSH to target host. You'll need to create the config file if it doesn't exist:

    IdentityFile ~/.ssh/<your_private_key_file>

Whenever you do a git fetch host will verify your identity automatically, without prompting you for a password.

On the git server site navigate to your repository's Administration > Webhooks screen and add a new webhook, pointed to http://<domain>/<path>/webhook.php.

For the github repository, you must specify the application/json value for the Content-type parameter.

See also