A further step in automation: postdeploy hooks

October 11, 2016 - 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 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.

postdeploy: command to run

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

postdeploy: bundle exec rake db:migrate

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

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=scalingo

Run a custom script

postdeploy: ./scripts/postdeploy.sh

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

postdeploy: ./postdeploy-step1.sh && ./postdeploy-step2.sh

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:

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

Share the article
Yann Klis
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.

Try Scalingo for free

30-day free trial / No credit card required / Hosted in Europe