How do I trigger hooks before and after a deployment?

When it comes to deployments, some environments require extra tasks to be executed before or after files are uploaded. Beanstalk supports two types of deployment hooks: pre- and post-deploy web hooks and a post-deploy shell command hook.

Pre- and Post-deploy web hooks

Deployment web hooks are supported for all three deployment server types: FTP, SFTP and SSH. They require that you have a web server running on your remote server that can be accessed by Beanstalk whenever a web hook is triggered. When the hook is triggered, Beanstalk will connect to your web server at the URL you specify and submit information about the triggered deployment in JSON format. You can setup the URL that your web hook is listening to on the server setup page.

Make sure your web server is always returning a 2XX HTTP response to Beanstalk in order for it to be considered a success. If a pre-deployment hook returns anything other than a 2XX response, the deployment will not proceed and no files will be uploaded. If the post-deployment hook failed, Beanstalk will mark the entire deployment as failed to alert you of the issue.

If you would like to protect your web hook with a password, you can use basic HTTP authentification by providing the username and password in the web hook URL, like this:

Web Hooks

Please note, if you have a firewall on your server you’ll need to whitelist Beanstalk’s IPs in order to receive the hook.

Post-deploy shell commands

The shell command hooks are available only for SFTP servers. Beanstalk will execute any shell commands on your remote server after the deployment is finished. Make sure that all of the specified shell commands return an exit status 0 in order for them to be considered a success. Otherwise the deployment will be marked as failed so you can see that there was a problem.

Sample of JSON data posted by Beanstalk

  // Username of the author
  "author":"author username",

  // Repository from which data was or will be deployed

  // Full name of the author
  "author_name":"John Smith",

  // Commit message

  // Email of the author

  // Server to which data was deployed or will be deployed
  "server":"server example",

  // Environment from which data was or will be deployed

  // Revision to which the deployment will be updated

  // Time when deployment happened - timezone is included, will be null for pre hook
  "deployed_at":"deployed at date",

  // Default source control URL of the repository (legacy attribute, may return Git or HTTPS URL)

  // HTTPS URL of the repository (supports both Git and SVN)

  // Git SSH URL of the repository (Git only)

  // Identifier of the payload, in case you consume JSON from many vendors

PHP users please note that in order to get contents of a POST request in PHP you need to use the following code snippet:


Content-Type header

For historical reasons Beanstalk will send a Content-Type header with value "application/x-www-form-urlencoded" with each hook.

Still need help? Contact Us Contact Us