How can I set up continuous deployment of a Ruby on Rails application from GitHub to Heroku?
-
1http://...http://stackoverflow.com/questions/25032095/cannot-connect-heroku-when-writing-heroku-update-ruby-on-rails?noredirect=1 i am trying to connect heroku i downloaded heroku toolbelt and installed it . then from the command prompt i type : heroku update just to setup a connection that doesn't need a password. i get the message: ! Unable to connect to Heroku API, please check internet connectivity and try again. i saw some answers that talked about the remote option so i did that: heroku git:remote -a my-app-name and i got the same response. the internet connection is ok because the git program is able to push to github. any idea why and how to fix????
-
Answer:
I have done this, by setting up a small Amazon EC2 instance running Hudson. It listens to a GitHub post-commit hook, checks out the code, runs Cucumber features (with Selenium and all) and RSpec. If everything is green, it deploys the code to Heroku. Works very well, and is super-duper cool! Setting this up was a little tricky, but it's certainly not nuclear surgery if I can do it. This guide assumes knowledge of Rails and Heroku, some basic unix skills and some basic knowledge about EC2. You also need to have the EC2 tools installed on your local machine (http://paulstamatiou.com/how-to-getting-started-with-amazon-ec2). Feel free to suggest improvements, and do comment with questions if you are unclear on stuff. Launch the instance EC2 can run a multitude of Linux distributions, but I ended up with Ubuntu 10.10 Maverick EBS boot from http://alestic.com/. The reason I went with Ubuntu is because it had a simple package manager and ran Firefox well. Pick an image and copy the AMI id for it from http://alestic.com for your region (I picked 10.10 Maverick EBS in es-east, which is AMI ID ami-ccf405a5). Head over to the AWS Management Console and launch a new new micro instance of your chosen image. Note the Public DNS of the booted instance, and then download the key pair file (.pem) to a suitable folder on your computer, and fire up the terminal. ssh -i yourkeypairfilehere.pem \ public.dns.of.your.instance.here Installing Ruby 1.9.2 Now that you are logged into your new shiny instance, the first order of business is to get Ruby on it. # Install lots of common Ruby dependencies that we are going to need sudo apt-get update sudo apt-get install ruby-full zlib1g-dev libopenssl-ruby libssl-dev libncurses5-dev libreadline5-dev ruby1.8-dev libxslt-dev libxml2-dev libsqlite3-dev git make # Install rvm bash < <(curl -s https://rvm.beginrescueend.com/install/rvm) echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.profile Now exit your SSH session and reconnect, or RVM will not work. # Install Ruby. This will take about four years. # Get a cup of coffee. rvm install 1.9.2 # Set 1.9.2 as the default Ruby. rvm --default use 1.9.2@global # Use rubygems 1.6.2 since 1.7.1 (version as of writing) # has some annoying incompatibilities with Rails gem update --system 1.6.2 gem install bundler # I found ruby-debug19 to require this permission. # It will require write access to some # stupid locations, which will cause permission errors. sudo chmod -R 775 /usr/local/rvm Set up Firefox and Xvfb Since we want to do headless Selenium integration tests, we want to run Firefox in Xvfb, which performs all graphical operations in memory, without using up processing power to actually render the output. sudo apt-get install firefox xvfb x11-apps imagemagick Now, we want to run Firefox once so that it generates a profile directory for our user (because we want to edit some settings): # Run Xvfb Xvfb :99 -ac -screen 0 1024x768x16 Now that Xvfb is running, create a new, second terminal window, create a second SSH session to your instance, and run: DISPLAY=:99.0 firefox http://example.org # For kicks, you can make a screenshot to see it working xwd -root -display :99.0 -out screenshot.xwd convert screenshot.wxd ~/screenshot.jpg # Exit SSH session and download the # screenshot to your local computer via SCP: exit scp -i yourkeypairfile.pem \ [email protected]:screenshot.jpg screenshot.jpg Before we continue we want to configure our browser profile so that it doesnât throw dialogs in the face of our Selenium tests after crashes (this will break Selenium). Create this file: ~/.mozilla/firefox/*.default/user.js user_pref("browser.sessionstore.enabled", false); user_pref("browser.sessionstore.resume_from_crash", false); And finally, make sure Xvfb runs on startup: /etc/init.d/xvfb I've had to link this externally, Quoras code parser could not handle it, for some reason: https://gist.github.com/973580 Make the script executable, or it won't run: sudo chmod 755 /etc/init.d/xvfb Install Java Ok, this is where Java comes in, and where Java comes in, things get tricky. You will temporarily have to upgrade your instance to a medium while installing Java, because the installer will eat up massive amounts of memory. I know, I know, but bear with me: ec2-modify-instance-attribute i-12345678 -t c1.medium Where i-12345678 is your instance ID. Also note that this will give your instance a new public DNS! sudo add-apt-repository "deb http://archive.canonical.com/ lucid partner" sudo apt-get update sudo apt-get install sun-java6-jre sun-java6-plugin sudo update-alternatives --config java Downgrade back to micro. Again, remember that your instance will get a new public DNS: ec2-modify-instance-attribute i-12345678 -t t1.micro Install Hudson Now, it's time to install Hudson! sudo mkdir /usr/share/hudson cd /usr/share/hudson sudo wget -O hudson.war \ http://java.net/projects/hudson/downloads/download/war/hudson-1.398.war # Replace the URL above with the latest from http://hudson-ci.org/ sudo chmod 755 hudson.war Create Hudson startup script We'll want Hudson to run on startup. Youâll need to create three script files first: /usr/local/bin/http://start-hudson.sh #!/bin/bash HUDSON_WAR=/usr/share/hudson/hudson.war HUDSON_LOG=/tmp/hudson.log nohup nice java -jar $HUDSON_WAR > $HUDSON_LOG 2>&1 & /usr/local/bin/http://stop-hudson.sh #!/bin/bash kill `ps -ef | grep hudson.war | grep -v grep | awk '{ print $2 }'` /etc/init.d/hudson #! /bin/bash # # hudson Start/Stop the Hudson Continuous Integration server. # # chkconfig: 345 91 10 # description: Hudson is a Continuous Integration server. \ # It monitors a source code repository and triggers builds \ # when it detects any changes. See https://hudson.dev.java.net/ \ # for more details. # processname: hudson # pidfile: /var/run/hudson.pid startup=/usr/local/bin/start-hudson.sh shutdown=/usr/local/bin/stop-hudson.sh HUDSON_USER=ubuntu start(){ echo -n $"Starting Hudson service: " su - $HUDSON_USER -c $startup RETVAL=$? echo } stop(){ action $"Stopping Hudson service: " su - $HUDSON_USER -c $shutdown RETVAL=$? echo } status(){ numproc=`ps -ef | grep hudson.war | grep -v "grep hudson.war" | wc -l` if [ $numproc -gt 0 ]; then echo "Hudson is running..." else echo "Hudson is stopped..." fi } restart(){ stop start } # See how we were called. case "$1" in start) start ;; stop) stop ;; status) status ;; restart) restart ;; *) echo $"Usage: $0 {start|stop|status|restart}" exit 1 esac exit 0 Make the three scripts you created above executable: sudo chmod 755 /usr/local/bin/start-hudson.sh sudo chmod 755 /usr/local/bin/stop-hudson.sh sudo chmod 755 /etc/init.d/hudson Add the main script to startup: sudo update-rc.d hudson defaults And reboot! sudo reboot Give your Hudson instance access to Github and Heroku To give Hudson access to your private Github repository, we first need to get Hudson itâs very own account on Github. Create a user (or whatever you feel suitable) and then add that user as a collaborator to your repository. Since it is the ubuntu user on the EC2 box doing the pushing to Heroku and pulling from Github, that user is going to need its own ssh key. ssh-keygen # Press enter for all questions cat /home/ubuntu/.ssh/id_rsa.pub #outputs the generated key, copy to clipboard Log in as hudsonyourapp on GitHub and add the key to https://github.com/account/ssh Make sure that GitHub and Heroku can recognize hudson: git config --global user.email "[email protected]" git config --global user.name "Hudson" Just like with Github, Hudson will need itâs very own Heroku account to push your app into production. Set up a user account on Heroku dedicated to Hudson (I used ), and then add it as a collaborator to your Heroku app. When this is done, run the following on your EC2 instance: gem install heroku heroku keys:add The above commands will install heroku, then it will ask for your username and password - give it the details from the dedicated Heroku account you created above (). This will upload the ssh key we created earlier to Heroku, so that Hudson can push to Heroku. Now, whenever you push to a git host the first time, youâll have to go through this question: The authenticity of host 'http://heroku.com (50.16.233.102)' can't be established. RSA key fingerprint is 8b:48:5e:67:0e:c9:16:47:32:f2:87:0c:1f:c8:60:ad. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'http://heroku.com,50.16.233.102' (RSA) to the list of known hosts. This question will cause your build scripts to break, because they cannot deal with prompts. To work around this, I pulled the repository from GitHub and then pushed it to Heroku, to make sure that both http://github.com and http://heroku.com are added to the known hosts list on the Hudson server: git clone [email protected]:yourcompany/yourproject.git cd yourproject git push [email protected]:your-app-name.git master You can delete the yourproject directory afterwards, we don't need it. Configure Hudson Go to manage plugins. If no plugins are available, go to advanced and click "check now". Install Ruby, Git, Github, Rake and Post build task plugins. Restart hudson. Go to configure system. Enable security, pick "Hudson's own user database â Enter administrator email (your own email) Save configuration. Click sign up in the upper right corner, and create an "admin" account and a "github" account. Go back to the configuration, select matrix authentication, add full access to the admin user and âOverall: Readâ and âJob: Readâ permissions to the "github" user. Remove all access from the anonymous user. Sign in as admin. Create a new job. Set github url to your project url eg http://github.com/rails/rails/ Select git as version control, add your repository, like so: [email protected]:rails/rails.git Check "Trigger builds remotely (e.g., from scripts)" and fill in a long random string there. Copy this string, you'll need this later. Click add build step > Execute shell to add build steps. Mine are: # Switch to correct ruby and gemset rvm use 1.9.2@global # Install bundles bundle install # Prepare database rake db:migrate rake db:test:prepare # Run specs rake spec #!/bin/bash # Starting xvfb to run Firefox and Selenium ... export DISPLAY=:99 /etc/init.d/xvfb start rake cucumber RESULT=$? # Shutting down xvfb /etc/init.d/xvfb stop exit $RESULT Add two post build tasks: git push [email protected]:yourherokuappname.git master heroku rake db:migrate --app yourherokuappname Set both of them to "Run script only if all previous steps were successful". That should be it, you now have the basics of your Continuous Integration setup in place!
Mattias Petter Johansson at Quora Visit the source
Other answers
Or you can give https://www.codeship.io/ a spin and see if it works for you (I am one of the founders) Cloud based Continuous Integration and Deployment (with special Heroku support) You can test any Ruby code with any test framework and deploy anywhere (as you have full control over your deploy commands) Additionally we have implemented Heroku support so you can set up continuous deployment to Heroku in less than a minute. If you have any questions send me an email to http://flo@http://codeship.io
Florian Motlik
I haven't had the opportunity to use it, but try giving http://docs.snap-ci.com/ a shot. It plugs into rails projects on github and does deployment on heroku. http://blog.snap-ci.com/blog/2012/11/27/enter-snap/
Pamela Ocampo
Unbiased answer: use http://CodeShip.io They've been amazing for us.
Matt Goldman
The first answer is utterly insane. Who has time for that? If you use GitHub, there's a better solution. With https://circleci.com, you'll have your Continous Integration set up in about 20 seconds. We automatically set up your projects, dependencies, databases, so there is no work for you to do. Once it works, it takes about a minute to set up Continuous Deployment to Heroku. In addition, you'll be able to run many different pushes at the same time, so your tests won't queue up if they take a long time. Also, we run your tests much faster than on EC2 (and also much faster than others who have answered here ;)). I'm happy to answer any questions you have: .
Paul Biggar
https://snap-ci.com allows you to set up CI for your Rails app within seconds through a full deployment pipeline. You can then sequence deployments to multiple Heroku deployments in your pipeline and have Snap deploy to them - either automatically or on-demand. We offer the ability to track the history of your deployments to any number of apps. The few minutes it takes to get this going, and the simplicity are similar to the others who have responded here- but it gives you a whole lot more visibility & control into your various deployments. More than happy to help out with any questions you may have. My email is - and in the interest of complete transparency, I am the product owner for Snap.
Badrinath Janakiraman
You can use Shippable (http://www.shippable.com) to set up continuous integration and deployment for your github projects in less than 20 seconds. Just sign up with github credentials and with one click, CI is set up for your public or private repositories. We can automatically deploy to heroku as well. Shippable is built on docker and runs your builds and tests faster than other services. And it's absolutely FREE for public and private Github repositories.
Manisha Sahasrabudhe
If you are looking for enterprise CI then I would suggest Jenkins or Teamcity. At my last company we were moving from TeamCity to Jenkins because Jenkins felt more flexible and would work with our ever growing list of rails applications. There are lots of tutorials and documents on how to connect github and heroku to Jenkins or Teamcity. Jenkins - http://thediscoblog.com/blog/2014/01/24/continuous-delivery-for-heroku-with-jenkins/ Teamcity- http://blog.carbonfive.com/2010/08/06/deploying-to-heroku-from-teamcity/ There is also a jenkins plugin https://github.com/heroku/heroku-jenkins-plugin/blob/master/README.md
Joshua White
Related Q & A:
- How can I set music as a ringtone on my iPhone?Best solution by Yahoo! Answers
- How can I set a wireless camera to record on my computer?Best solution by Yahoo! Answers
- How can I set up my computer to alert me when I get an incoming email?Best solution by Yahoo! Answers
- How can I set up a website for free?Best solution by Yahoo! Answers
- How can I set up 2 monitors on a pc?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.