Deploy hooks - syntax & commands

What are deploy hooks?

Deploy hooks are scripts that allow you to automate actions at various points during the deployment process for your applications. If you’ve never used hooks before, we have a tutorial that walks you through the basics. You can also use our examples at the bottom of this page.

Hook points

The deployment process is divided into a number of steps, and hook points allow you to intervene at various points during this process.

Hook point Description
first_thing The first thing (after after_checkout) that will happen on the server. A common use-case for this hook is to install packages to run your application
after_checkout When we create your server, your code is pulled directly from Git. Use this hook if you want to make a change to your code after it is pulled (but before anything else). Happens during the code deployment of your application
before_ x This hook will run before a server component is installed on your server. Accepted values for x: redis, mysql, postgresql, mongodb
after_ x This hook will run after a server component is installed on your server. Accepted values for x: redis, mysql, postgresql, mongodb
after_symlink Runs after the symbolic link to your current code folder has been created. Happens during the code deployment of your application.
custom_server This hook will run on your custom servers
before_agent This hook will run before the Cloud 66 agent is installed on your server
after_agent This hook will run after the Cloud 66 agent is installed on your server
last_thing This hook will run as the last thing that happens on your server

Hook fields

There are three types of deploy hooks, and the fields available (and required) vary by type:

  1. Snippets: use pre-existing scripts to install common packages. These snippets are open source and are created by Cloud 66 or third parties.
  2. Commands: run your own commands.
  3. Inline Scripts: use your own inline scripts for more comprehensive procedures

Hook type: snippets

Field Description
HOOK TYPE: Snippets
snippet Snippet to be used - runs in /tmp/deploy_hooks by default
target Target server(s), with accepted values any, rails, mysql, postgresql, mongodb, redis, sinatra, padrino, custom
Note: Please make sure you read the run_on part, if your target is not any
execute
(false)
Set to true for the code to be executed during deployment
executable
(false)
Set to true for the code to be made executable on the target. Defaults to true if execute is true
apply_during
(all)
Specify when you want the deploy hook action to take place. Accepted values are build_only, deploy_only or all. The build step occurs the first time an application is deployed, and will re-occur until the application has been successfully deployed at least once. After this subsequent deployments are deploy steps
env_vars Hash of values that will be set when running this specific deploy hook. Only applies to deploy hooks that have execute = true If the application already contains this env var it will be overridden with the value specified here.
halt_on_error
(true)
Specify whether the execution should continue or halt in the event of an error
run_on
(single server)
If you have multiple servers in the same group (eg. scaled-up Rails servers), you can specify whether you want the deploy hook action to occur just once or once against each server in that group. Valid values are: single_server or all_servers. If you've specified target: any above, this will apply to all servers
run_as
(server user)
If you execute a file on your target server, specify which user you would like the file to be executed as Note: you can't specify both this and sudo
sudo
(false)
If you are executing the file on your target server, specify whether you want that execution to be sudo-ed? Note: you can't specify both this and run_as

Hook type: commands

Field Description
HOOK TYPE: Commands
command Command to be used - run in /tmp/deploy_hooks by default
target Target server(s), with accepted values any, rails, docker, mysql, postgresql, mongodb, redis, sinatra, padrino, custom
Note: Please make sure you read the run_on part, if your target is not any
execute
(true)
Set to true for the code to be executed during deployment
executable
(false)
Set to true for the code to be made executable on the target. Defaults to true if execute is true
apply_during
(all)
Specify when you want the deploy hook action to take place. Accepted values are build_only, deploy_only or all. The build step occurs the first time a application is deployed, and will re-occur until the application has been successfully deployed at least once. After this subsequent deployments are deploy steps
env_vars Hash of values that will be set when running this specific deploy hook. Only applies to deploy hooks that have execute = true If the application already contains this env var it will be overridden with the value specified here.
halt_on_error
(true)
Specify whether the execution should continue or halt in the event of an error
run_on
(single server)
If you have multiple servers in the same group (eg. scaled-up Rails servers), you can specify whether you want the deploy hook action to occur just once or once against each server in that group. Valid values are: single_server or all_servers. If you've specified target: any above, this will apply to all servers
run_as
(server user)
If you execute a file on your target server, specify which user you would like the file to be executed as Note: you can't specify both this and sudo
sudo
(false)
If you are executing the file on your target server, specify whether you want that execution to be sudo-ed? Note: you can't specify both this and run_as

Hook type: inline scripts

Field Description
HOOK TYPE: Inline Scripts
source This specifies the source location of your deploy hook file within your repository
target Target server(s), with accepted values any, rails, mysql, postgresql, mongodb, redis, sinatra, padrino, custom
Note: Please make sure you read the run_on part, if your target is not any
env_vars Hash of values that will be set when running this specific deploy hook. Only applies to deploy hooks that have execute = true If the application already contains this env var it will be overridden with the value specified here.
execute
(false)
Set to true for the code to be executed during deployment
executable
(false)
Set to true for the snippet to be made executable on the target. Defaults to true if execute is true
destination The destination path on your target server. You can also specify environment variables in your destination field; <%= ENV['APP_PATH'] %> for example
apply_during
(all)
Specify when you want the deploy hook action to take place. Accepted values are build_only, deploy_only or all. The build step occurs the first time a application is deployed, and will re-occur until the application has been successfully deployed at least once. After this subsequent deployments are deploy steps
halt_on_error
(true)
Specify whether the execution should continue or halt in the event of an error
run_on
(single server)
If you have multiple servers in the same group (eg. scaled-up Rails servers), you can specify whether you want the deploy hook action to occur just once or once against each server in that group. Valid values are: single_server or all_servers. If you've specified target: any above, this will apply to all servers
run_as
(server user)
If you execute a file on your target server, specify which user you would like the file to be executed as Note: you can't specify both this and sudo
sudo
(false)
If you are executing the file on your target server, specify whether you want that execution to be sudo-ed? Note: you can't specify both this and run_as
parse
(true)
Specifies whether the file being transferred should be parsed for environment variables. Using this you can embed <%= ENV['ENV_VAR'] %> for example in your source file, and have it resolved during the deploy hook action
owner
(your server user)
Ownership permissions for the file (and destination folder) on the target server. An example could be user:group

Debugging hooks

Automating deploy hooks can sometimes be tricky. To avoid issues, it’s good practice to run each of your commands manually to see that they complete successfully.

If a specific command doesn’t show any output, you can use the echo $? command after issuing your command to see its exit code. If it returns a zero, your command was successful, whereas a one means it has failed.

Usage and examples

A deploy hook needs, at a minimum:

A bare-bones hook looks like this:

production: # Environment
    first_thing: # Hook point
      - command: apt-get install nmap -y # Hook type
        target: any # Hook fields
        execute: true

You can nest different hooks for the same hook point:

production: # Environment
    first_thing: # Hook point
      - command: apt-get install curl -y # Hook type
        target: any # Hook fields
        execute: true
      - command: apt-get install ncdu -y # Hook type
        target: any # Hook fields
        execute: true

You can also run several snippets at the same hook point:

production: # Environment
    first_thing: # Hook point
      - snippet: cloud66/imagemagick # Hook type
        target: any # Hook fields
        execute: true
      - snippet: cloud66/ffmpeg
        target: any
        execute: true

This example shows how to use the env_vars parameter.

before_nginx:
   snippet: cloud66/download
   target: docker
   execute: true
   apply_during: build_only
   run_on: all_servers
   env_vars:
     SOURCE_URL: "https://github.com/openresty/headers-more-nginx-module/archive/v0.33.tar.gz"
     TARGET_PATH: /usr/local/build/nginx-modules/headers-more-nginx-module
     UNTAR: true