04.07
- 4.7 Script a deployment
- Learning Objectives
- EXERCISE: Modify Capistrano script.
- EXERCISE: Modify Capistrano Script (OPTIONAL: Ruby)
4.7 Script a deployment
Learning Objectives
By the end of this lesson you will be able to:
- Write a deployment script in Capistrano.
EXERCISE: Modify Capistrano script.
Go to the deployment/config directory.
cd /home/ec2-user/continuous_integration_example/software/deployment/config
Create a new file called mydeploy.rb
“` $ sudo vim mydeploy.rb
“`
Insert the contents of the code example below into the mydeploy.rb. Be sure to replace the 29 in cd29 with your userid.
“` require ‘rubygems’ require ‘aws-sdk’ load ‘/usr/share/tomcat6/scripts/config/aws.config’
sdb = AWS::SimpleDB.new
set :sdb_domain, “cd29”
set :domain do item = sdb.domains[“#{sdb_domain}”].items[‘parameters’] item.attributes[‘ip_address’].values[0].to_s end
set :artifact_bucket do item = sdb.domains[“#{sdb_domain}”].items[‘parameters’] item.attributes[‘artifact_bucket’].values[0].to_s end
set :ip_address do item = sdb.domains[“#{sdb_domain}”].items[‘parameters’] item.attributes[‘ip_address’].values[0].to_s end
set :user, “ec2-user” set :use_sudo, false set :deploy_to, “/usr/share/tomcat6/webapps” set :artifact, “brewery.war” set :artifact_url, “https://s3.amazonaws.com/#{artifact_bucket}/#{artifact}” set :ssh_options, { :forward_agent => true }
set :application, domain
role :web, domain role :app, domain role :db, domain, :primary => true
set :deploy_via, :remote_cache
def get_binding binding # So that everything can be used in templates generated for the servers end
def from_template(file) require ‘erb’ template = File.read(File.join(File.dirname(FILE), “..”, file)) result = ERB.new(template).result(self.get_binding) end
namespace :deploy do
task :setup do run “sudo chown -R tomcat:tomcat #{deploy_to}” run “sudo service tomcat6 stop” end
task :deploy do run “cd #{deploy_to} && sudo rm -rf brewery* && sudo wget #{artifact_url}” end
desc “Restart Application” task :restart, :roles => :app do run “sudo service tomcat6 restart” end
after “deploy:setup”, “deploy:deploy” after “deploy:deploy”, “deploy:restart” end “`
Save and close the mydeploy.rb file
Add the file in Git
“` $ git add .
“`
Commit the file to Git
“` $ git commit -m “Created deployment script”
“`
Push it to the Git master.
“` $ git push
“`
Modify your Jenkins Deploy job to run your version: mydeploy.rb. Run a Build job, Environment job and the Deploy job. Verify the application is deployed to the environment.
EXERCISE: Modify Capistrano Script (OPTIONAL: Ruby)
- From the AWSCLI instance, change directory to devopsinthecloud.
Open the file as shown below.
“` $ sudo vim deploy.rb
“`
A list of some of the deploy tasks is provided below.
“` deploy:setup deploy:deploy deploy:bundle_install deploy:db_migrate deploy:restart
“`
Capify is run in the jenkins.template
- You can also find these files by going to https://raw.github.com/stelligent/continuous_integration_example/master/deployment/config/deploy.rb.
- Review the Deployment Jenkins job.
- Make a modification to the contents of the file and save it.
Add the file in Git
“` $ git add .
“`
Commit the file to Git
“` $ git commit -m “Modified deployment script”
“`
Push it to the Git master.
“` $ git push
“`