Skip to content

Instantly share code, notes, and snippets.

@necojackarc
Last active April 24, 2017 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save necojackarc/189ecfb953dfd338b8aa635a1295f066 to your computer and use it in GitHub Desktop.
Save necojackarc/189ecfb953dfd338b8aa635a1295f066 to your computer and use it in GitHub Desktop.
How to Deploy Hubot with OpsWorks
#!/bin/sh
set -e
yarn install
yarn add forever
export PATH="node_modules/.bin:node_modules/hubot/node_modules/.bin:$PATH"
[ -f ../../shared/app.env ] && . ../../shared/app.env
forever stopall
forever start -c coffee node_modules/.bin/hubot -n <%= @bot_name %> -a <%= @bot_adapter %>

Procedure

  1. Create a new custom cookbook for Hubot
  2. Create a Node.js stack and an app on OpsWorks
  3. Set the recipe in "Deploy" section

Custom Cookbook for Hubot

Put two files as below:

  • hubot/recipes/deploy.rb
  • hubot/templates/default/bin_hubot.erb

hubot is the name of cookbook for example. See deploy.rb and bin_hubot.erb in this Gist for the details.

Usage

Set the following environment variables:

  • BOT_NAME
  • BOT_ADAPTER

[Appx.] Use New Version of Node.js

OpsWorks built-in recipe installs very old version of Node.js as of Apr. 14th 2017. To use a new version of Node.js, set the following recipe on "Setup" section.

# _/_/_/_/ This recipe must be executed after `deploy::nodejs` built-in recipe _/_/_/_/
user = node[:deploy][:hubot][:user]
group = node[:deploy][:hubot][:group]
home = node[:deploy][:hubot][:home]
deploy_dir = node[:deploy][:hubot][:deploy_to]
environment_variables = node[:deploy][:hubot][:environment_variables]
execute "delete_monitjc_created_by_opsworks_built-in_recipe" do
command "rm -f #{node.default[:monit][:conf_dir]}/node_web_app*"
end
execute "install yarn" do
command "curl -o- -L https://yarnpkg.com/install.sh | bash"
user user
group group
not_if "which yarn"
retries 3
retry_delay 10
environment(
"HOME" => home,
"USER" => user,
)
end
template "#{deploy_dir}/current/bin/hubot" do
source "bin_hubot.erb"
owner user
group group
mode "0755"
variables(
deploy_dir: deploy_dir,
bot_adapter: environment_variables[:BOT_ADAPTER],
bot_name: environment_variables[:BOT_NAME],
)
end
execute "start_hubot" do
cwd "#{deploy_dir}/current"
user user
group group
command "bin/hubot"
environment(
"HOME" => home,
"USER" => user,
"PATH" => "#{home}/.yarn/bin:#{ENV['PATH']}",
)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment