Blog

A further step in automation: postdeploy hooks

Chargement...

10 min read

A further step in automation: postdeploy hooks

When you deploy your application, you may want to trigger custom actions after the deployment succeeded automatically. So far you had to run custom commands by hand which was sensible to human-keyboard interface errors. This hook is then exactly what you need. It will automatically start the configured command at the e

When you deploy your application, you may want to trigger custom actions after the deployment succeeded automatically. So far you had to run custom commands by hand which was sensible to human-keyboard interface errors. This hook is then exactly what you need. It will automatically start the configured command at the end of your deployment.

Adding a postdeploy hook to your Application

To add a postdeploy hook to your application , you just have to add to your Procfile (or create a Procfile file), a new type named postdeploy. The command specified for this type of container will be executed in a one-off container at the end of each deployment.

The command will be executed synchronously after the deployment. If the hook fails, the deployment will also fail, the old version of your application will be kept until a new successful attempt of deployment. Here is an example of logs you can get when a postdeploy hook has been set:

Examples

The postdeploy hook is executed in a one-off container and has exactly the same environment, which means you’ll have access to your the environment variables of your application, and variables from the runtime environment (APP, CONTAINER, CONTAINER_VERSION). You can use these variables directly in the postdeploy line of the Procfile, or in a script which is called by it.

Applying Rails migrations

Keep in mind that migrations may fail, design them knowing that, you will find an article from Codeship explaining this process.

Notifying a third-party service

Newrelic

postdeploy: newrelic deployments --revision=$CONTAINER_VERSION
postdeploy: newrelic deployments --revision=$CONTAINER_VERSION
postdeploy: newrelic deployments --revision=$CONTAINER_VERSION
postdeploy: newrelic deployments --revision=$CONTAINER_VERSION

Rollbar

postdeploy: curl https://api.rollbar.com/api/1/deploy/ -F access_token=$ROLLBAR_TOKEN -F environment=$RAILS_ENV -F revision=$CONTAINER_VERSION -F local_username
postdeploy: curl https://api.rollbar.com/api/1/deploy/ -F access_token=$ROLLBAR_TOKEN -F environment=$RAILS_ENV -F revision=$CONTAINER_VERSION -F local_username
postdeploy: curl https://api.rollbar.com/api/1/deploy/ -F access_token=$ROLLBAR_TOKEN -F environment=$RAILS_ENV -F revision=$CONTAINER_VERSION -F local_username
postdeploy: curl https://api.rollbar.com/api/1/deploy/ -F access_token=$ROLLBAR_TOKEN -F environment=$RAILS_ENV -F revision=$CONTAINER_VERSION -F local_username

Run a custom script

You can event chain taks by using logical operators like && or ||

One-click Deployments

A while ago, we released our way to add a button on your open-source project website or Github page. Thanks to this hook a lot of new possibilities are available as you can achieve operations to initialize the database or anything required by your application in an automated fashion.

Note that to ease the process, if you were already using this feature from Heroku, we’ve added a compatibility layer to use the scripts.postdeploy attribute of the JSON app manifest as a standard postdeploy hook on Scalingo.

What you should not do

  • Building assets : The postdeploy hook is not the right place to build your assets. Keep in mind that the action is executed in a dedicated one-off container, the other containers from your application won’t be impacted by any file change. All operation relative to assets should be done in the build process.

Conclusion

You’ll find more information about the way this hook is being executed in our documentation. This is a first stone to a bigger picture, next step will be related to Github:

Raise your hand if you want to test the new Github integration! #PaaS #Github pic.twitter.com/8MK6IM9KtE

— Scalingo (@ScalingoHQ) October 7, 2016

Don’t hesitate to contact us if you’ve any feedback on the feature, or if you want to beta-test the Github integration.

Yann Klis

Yann Klis co-founded Scalingo. Scalingo's vision is to offer a european sovereign cloud housting platform for developers. Today Scalingo hosts thousands of web applications from customers located all over the world. Before that Yann founded Novelys a web studio specialised in Ruby on Rails technology.

Stay Updated

Get articles and platform updates in your inbox.

Ready to Deploy with Confidence?

Experience zero-downtime deployments, intelligent auto-scaling, and fully managed infrastructure. Start deploying your applications on Scalingo today.

No credit card required • Deploy in minutes • Cancel anytime

Deploy your first app or database

Let's start building together

Join developers who chose a platform built for fast delivery and calm production, with European values and human support.

Deploy your first app or database

Let's start building together

Join developers who chose a platform built for fast delivery and calm production, with European values and human support.

Deploy your first app or database

Let's start building together

Join developers who chose a platform built for fast delivery and calm production, with European values and human support.