Deploy hooks are Ruby scripts that you write which are executed at designated points in the deployment process. This allows you to customize the deployment of your application to meet its particular needs.
For example, if your application uses Resque, then deploy hooks provide a way for you to restart your Resque workers when you deploy a new version of your application. Rollbar users can use deploy hooks to notify Rollbar of a deploy.
Deploy hooks live in the APP_ROOT/deploy
directory of your application. The order in which they run is specified in the documentation for the ey deploy
command.
Important: When you add a new instance, Engine Yard automatically runs the deploy hooks. input_ref
will not be available and the deploy hooks will fail if you use it. This is a known issue and we are working on a fix. In the meantime, confirm that input_ref
is not nil and repo
is nil.
Best Practices
Engine Yard recommends that you use deploy hooks to run rake tasks or scripts that perform the necessary tasks, rather than including all the logic in the hook itself. You should use deploy hooks mostly to trigger shell commands whenever possible. Using deploy hooks in this way allows you to write tests for your rake tasks and scripts, then the hook simply triggers these tasks and scripts.
You should avoid using deploy hooks as part of your application code because they are instance_eval'd
into a running copy of the deploy system. This means if you you try to use hooks as part of your code, you will be battling with all the context in the deploy code.
Structure and sequence
To use deploy hooks, create an APP_ROOT/deploy
directory in your application and save named hook files in this directory which will be triggered at the appropriate times during the deployment process. The files are defined as follows and run in the order listed:
APP_ROOT/
deploy/
before_bundle.rb
after_bundle.rb
before_compile_assets.rb
after_compile_assets.rb
before_migrate.rb
after_migrate.rb
before_symlink.rb
after_symlink.rb
before_restart.rb
after_restart.rb
Remember that, in order for migrations to run, your entire environment is loaded. So if you have any symlinks that need to be created in order for the application to start properly, put them in before_migrate.rb
instead of before_symlink.rb
, because before_symlink.rb
runs after the migration.
Any deploy hooks that you have defined are called, even if they are hooking into a step that is not necessary for the deployment. For example, after_migrate
is called even if there are no new migrations in your deployment.
Shell commands
Besides the usual ruby syntax, you also have access to run
, run!
, sudo
, and sudo!
to run commands. See Deploy Hooks API.
Command | Runs as | Exit zero | Non-zero exit |
---|---|---|---|
run | deploy user | returns true | returns false |
run! | deploy user | returns true | aborts deploy |
sudo | root | returns true | returns false |
sudo! | root | returns true | aborts deploy |
For example:
run "echo ‘config.release_path: #{config.release_path}’ >> #{config.shared_path}/logs.log"
run "ln -nfs #{config.shared_path}/config/foo.yml #{config.release_path}/config/foo.yml"
sudo "echo ‘sudo works’ >> /root/sudo.log"
Calling git commands
Here’s an example where you can call a git command from a deploy hook:
run "exec ssh-agent bash -c 'ssh-add /home/deploy/.ssh/<app>-deploy-key && git clone git@github.com:foo/bar.git #{current_path}/tmp/foo'"
Replace <app>
with the name of your app and change the path for your git repository.
Logging
Deploy hooks can output to STDERR, which will be written to the deploy log and you can use the warning command do this.
For example, this line on a deploy hook:
warning "Hello there, STDERR!"
will produce this output in the log:
+ 00s !> WARNING: Hello there, STDERR!
Another option which you might prefer is to run an echo command that outputs to STDERR:
run "echo 'Hello from run, STDERR!' 1>&2"
The above code in a deploy hook will output:
+ 00s Hello from run, STDERR!
which is a litter more appropriate because it does not include the WARNING string.
Deploy hooks API
Engine Yard has deprecated the use of the @configuration
variable directly. Instead,use the config
object for compatibility with future releases.
The following methods are Ruby specific and should be accessed through the config
object: For non-Ruby specific deploy hook variables, see Use non-Ruby Deploy Hooks
-
config.account_name
The name of the account that owns the environment where the application is deploying: e.g.
myaccount
-
config.all_releases
Array of paths to the deployments on this instance, ordered from oldest to newest.
-
config.app
This is the name of the application: e.g.
myapp
-
config.current_name
On a utility instance, the name of the utility instance. On non-utility instances, nil.
-
config.current_path
Path to the current deployment. This is the deployment pointed to by the
current
symlink, so the value ofcurrent_path()
will change after thesymlink
step is executed. -
config.current_role
The role of the current instance. Possible values:
solo
: the sole instance for the environmentapp_master
: the application masterapp
: a non-master application serverutil
: a utility server. Thecurrent_name
method will allow you to distinguish between different types of utility servers.
-
config.deployed_by
The name of the user who triggered the deployment: e.g.
John Smith
. Not available on rollback. -
config.framework_env
The value of the
RAILS_ENV
,RACK_ENV
, andMERB_ENV
environment variable. -
config.environment_name
This is the name of the environment that the application is deploying on: e.g.
myenvironment
ormyapp_staging
. -
config.input_ref
The name of the branch that was given as input to the deploy: e.g.
master
orbranch
. Not available on rollback.Note: Roll back is ideal for simple deployments with no database migrations or complex deploy hooks. When a data transformation has been deployed, rolling forward by deploying a corrected release may be best.
-
config.latest_release
Path to the most recent deployment.
-
config.migrate?
True if migrations are being run in this deployment, false otherwise.
-
config.node
Various information about the current instance and other instances in the environment. See
/etc/chef/dna.json
on an instance for an example of whatnode
will return. If the information you require is available via some other method, it is preferable to use the other method to get it.node
is just here as a catch-all.
Note: There were some changes in the dna.json structure on V5. Most of the keys have been moved under the 'dna' key. For example, on V4 you refer to the environment asconfig.node[:environment][:name]. F
or V5 you should change that toconfig.node[:dna][:environment][:name]
. In this case, it is better to useconfig.environment_name
instead, because that works for both V4 and V5. -
config.previous_release(current=latest_release)
Path to the deployment prior to
current
. If nil, thencurrent
is the newest deployment. -
config.oldest_release
Path to the oldest deployment
-
config.release_dir
Path to the directory containing the deployments
-
config.release_path
Path to the deployment that is currently being deployed: e.g.
/data/appname/releases/12345678
-
config.repo
The URL of this application’s git repository.
-
config.repository_cache
The path to the local clone of this application’s git repository.
-
config.active_revision
The SHA of the commit currently being deployed.
-
config.shared_path
Path to the shared dir: e.g.
/data/appname/shared
-
config.stack
The web server stack for this environment. Possible values:
nginx_mongrel
: Nginx web server and Mongrel application servernginx_passenger
: Nginx web server and Passenger application servernginx_unicorn
: Nginx web server and Unicorn application server
Helper Methods
The following are helper methods in the deploy hook:
-
debug()
Logs messages to STDOUT and allows you to add log-in capabilities to your deploy hooks. Only shows when verbose is true.
-
info()
Logs messages to STDOUT and allows you to add log-in capabilities to your deploy hooks.
-
on_app_master(&block)
Executes
block
only on the environment’s application master or single instance. -
on_app_servers(&block)
Executes
block
only on the environment’s application servers (master and slaves) or single instance. -
on_app_servers_and_utilities(&block)
Executes
block
only on the environment’s application servers and utility servers -
on_utilities(*utility_names, &block)
Executes
block
on utility servers whose names are included inutility_names
.utility_names
can be passed as an array or as multiple arguments.- If called like
on_utilities() { ... }
, the block will be executed on all utility servers. on_utilities("alpha") { ... }
executes the block on all utility servers named “alpha”.on_utilities("a", "b") { ... }
is equivalent toon_utilities(%w[a b]) { ... }
.
- If called like
NOTE: Use strings, not symbols, to specify the utility instance names, otherwise your on_utilities block will not be executed. For example: on_utilities(:alpha) will not be executed on a utility instance named "alpha".
-
run(command)
Executes
command
as a nonprivileged user (deploy
by default). -
run!(command)
Executes
command
as a nonprivileged user. Aborts the deployment if it fails.
Note: This command can only be used with the CLI and the engineyard gem 2.0. Deployment fails if you try to use run! when deploying your application from the dashboard. -
sudo(command)
Executes
command
as root. -
sudo!(command)
Executescommand
as root. Aborts the deployment if it fails.
Note: This command can only be used with the CLI and the engineyard gem 2.0. Deployment fails if you try to use sudo! when deploying your application from the dashboard. -
warning()
Logs messages to STDERR and allows you to add log-in capabilities to your deploy hooks.
More Information
These other resources might help you:
For more information about ... | See ... |
---|---|
Customizing your deployment | Customize your Deployment |
If you have feedback or questions about this page, add a comment below. If you need help, submit a ticket with Engine Yard Support.
I'm using deploy hooks to stop my Resque processes before_migrate and start them up again before_restart. I just ran in to an issue where my migration failed and the deploy was halted however my workers were not ever restarted, this is somewhat obvious when you think about it but at the time wasn't expected.
Is there a deploy hook which runs on failure or runs always regarless of the outcome of the deployment like ensure in a begin/rescue block?
Thanks
Hi Hugh,
The only deploy hooks are the ones that are listed above and there is not one that runs on failure. I'd suggest using a conditional or something similar to check the status of the workers and then run the code if some condition is true.
If you have any questions about your application specifically, then please go ahead and put in a support ticket.
Thanks
(Being too busy-lazy to dig around in code at the moment...)
Is there an exposed method for writing output to the
ey deploy
stream, to give informational messages about what your custom hooks are doing?Ches: I have used standard error with success to output my warning messages. Maybe standard out will work as well for your information messages.
Thanks Petteri, I've tried simply using
run %(echo 'message')
in the past but it doesn't seem to work. I'll try standard error but that feels wrong when I'm outputting information messages, not errors.current\_name
does not seem to work, at least not in engineyard-serverside 1.6.3: https://gist.github.com/4c81179a3545f20ad49fUnless it's coming from method_missing somewhere, I can't see that it's defined on the configuration class in the library, and I don't see it in 2.0 either which appears to have made efforts to be a lot more explicit with method_missing. The block form with
on\_utilities(\*names, &blk)
would be fine, but unfortunately my staging env doesn't mirror production perfectly so I was tryingif environment == 'staging' or current\_name == 'workers'
. I can usenode
instead, but please fix the docs, unless I've missed something and am wrong here...By the way, it's still potentially being called in library code too: https://github.com/engineyard/engineyard-serverside/blob/9c15fbdbcdf069cfb22f9ee4c839c85cf6ae283e/lib/engineyard-serverside/deploy_hook.rb#L110
I would like to get at the name of my instance, so that I can do something depending on whether I just deployed to production vs. deploying to a clone of production.
Based on the documentation above, my initial thought was to use the current_name() method to get the name of the instance, as that method name seems appropriate, however the description of the method doesn't feel like it will give me what I want.
I then decided that I'll need to grab the instance name from the node json. From the example in the documentation above, It would appear that node['environment']['name'] will do the trick, however I wanted to verify that by looking at the contents of my /etc/chef/dna.json. Unfortunately, that file is owned by root, and I can't view it as the deploy user.
Finally, based on the documentation above, it seems that I can use either the "node" variable or the node() method to access this information. Assuming either works, having a node() method seems a bit redundant to me, and made to wonder which to use.
run! and sudo! were added in engineyard-serverside-adapter-2.0.0, which the web dashboard is not yet using. In other words, deploying can work on the command line but not in the dashboard if you use run! or sudo! in your deploy scripts. EY staff reports that this is actively being worked on.
Does on_app_servers_and_utilities(&block) run the app_master as well as app instances? If so is there a method to only run on app instances?
I think I answered my own question:
if current_role == 'app'
@Hugh Kelsey: how about on_app_servers(&block)?
Looking for information about testing these deploy steps. In capistrano, I can make a task to restart resque and run just that task against the servers. Once it works, I can add it to the deploy hooks. Is there a similar scenario on EY?
Edward Anderson: The dashboard is now using
engineyard-serverside
version 2.x and so the new methods should work. Note that this also means thatinfo
has been changed in favor ofshell.info
Matt Scilipoti: In terms of testing deploy hooks, currently the only way to do it is to deploy to an environment. This would be a good use case for EY-Local which you can find more about and request a feature at http://ey.io/ey-local
Evan: Not so much specifically for testing as Matt asked about, but ad-hoc Cap tasks are one of the biggest things I miss with PaaS deployment systems these days. I know that's probably a big can of worms, and in some senses it engrains bad habits for situations where Chef automation, proper monitoring, etc. should be used, but ad-hoc tasks are still often really handy. Giving a command to
ey ssh
can sometimes fill a need. Anyway, probably better forums for this kind of feature request discussion.Big thanks for the engineyard-local pointer though, I've bugged EY folks since forever for a vagrant box for testing Chef recipes, and I completely missed the release of this thing.
Shouldn't input_ref be listed in this document?
Hi William, Yes; we need to update this doc with the new variables:
environment_name: name of the environment
account_name: name of the account the environment belongs to
deployed_by: name of the Engine Yard Cloud user that triggered the deploy
input_ref: the branch name that was given as input to the deploy (instead of the commit SHA that it was reduced to for the deploy)
Thanks for the reminder. kjm
Can the code within a deploy hook access command line switch values? In other words, i have a deploy hook that needs to run almost every time I deploy. On the occasions when I don't want to the hook to run, I would like to pass a switch (or option, similar to --migrate) that my deploy hook could look for and exit without running. I didn't see any mention of the parameters passed to the command line "ey deploy" being accessible, but hoped they might be.
Turns out that many of these are undefined when you rollback. Broke rollback functionality for us to use them. Suggestions?
@Tom Hoen: You can use the verbose <code>--extra-deploy-hook-options</code> param -- see <code>ey help deploy</code>. Docs seem scattered on this, but you use it like <code>ey deploy --extra-deploy-hook-options=skip_foo:true</code>, and then you can access <code>@configuration[:skip_foo]</code> as a hash key in deploy hook scripts. According to <a href="https://github.com/engineyard/engineyard/pull/144">this ticket</a> it may work as <code>config[:skip_foo]</code> now, but I have old code with the ugly ivar reference that still works last I checked.
[God knows what kind of markup is supported in comments on this knowledgebase, rich-text editor with no preview function... Hoping for plain HTML]
Markup fail. Engine Yard staff, please consider optimizing the comment system for a technical audience and content.
@Ches Martin - That will work. Thanks for the tip!
On code blocks, since EY is using the TinyMCE editor, they could add a button for the built-in "code" format. http://www.tinymce.com/wiki.php/configuration:formats
Hi, using my non-EY testing login to see how TinyMCE editor looks to customers. I see what you mean, Ches. No HTML button! (which is how I control the code/pre tags)
<code>testing a code line</code>
<pre>testing a code line</pre>
Will look into our alternatives with Zendesk. thanks, kjm
Logged a ticked for Zendesk so we can explore alternatives with this TinyMCE editor. thanks! kjm
[ZD-106]
@Tom Hoen, an additional note from the developer on your original question ...
The
--config
switch is for that purpose.ey deploy --config custom:value
@Steve Hull I updated the ticket you have open with us about the variables not being around on rollback - feel free to update the ticket if you have any questions and thanks for pointing it out.
Is there a public GitHub issue regarding the undefined vars on rollback, please? You can imagine how unpleasant it is to discover that your rollback is broken when... you need to roll back.
@ches I don't believe there is a public GH issue regarding rollback. And yes, I know first-hand how unpleasant it is to discover rollback is broken. Capistrano designed rollbacks in a way that should be nearly instantaneous for a good reason. Sometimes you need to quickly abort and cutting a hot-fix branch and redeploying feels like time wasted. I'm very familiar. In my (private) support ticket with EY, it was explained that they basically don't support the rollback command at all and recommend that customers not try to use it.
To which I answer -- then take that command out of your tool. With the command still in there, it feels like a (trollface) => (fuuuu) kind of moment when shit is hitting the fan.
HI Ches. Steve is right, it's not a Github issue at all. Our documentation failed to mention that two variables, input_ref and deployed_by, do not work when using the engineyard gem's rollback command.
I would like to clarify one part - we do support the rollback command, but it's important to understand the right use case for it and that rollback really only change a symlink to the previous release. More information about the command is available in the Readme.
For simple deployments with no database migrations or complex deployhooks it works great to rollback a flawed release. However, it will not fix data transformation and deploying another release that corrects the data is often the best route.
There were a number of missteps in Steve's ticket and we left the impression that the command was not supported. Further our documentation didn't account for the variables not being available. Even more, it's clear we need to better document what the rollback command actually does and when it may be appropriate to use it versus deploying a corrected release. We're working on a large overhaul of the entire deployment documentation that will cover it. We think you'll like it.
Sorry for all the confusion guys.
John Yerhot, US Support Manager.
Thanks for the forthcoming response John, appreciated. I do understand that rollback is something of a myth and agree that best practice should guide people toward rolling forward instead. This is thankfully not something we've needed often, but unfortunately in just such a situation that a symlink switch was an appropriate and swift course of action, we found out the hard way about this variable situation. I hope this can be addressed in a way that is less prone to catching people by surprise at the worst of times.
config.revision is not, in fact, the current revision for us. It points to a commit a year or so old.