What we want to achieve:
- one click create a working version of rails project on DigitalOcean
- set up a local working version of the project
- set up remote & local git repo so we could push to update remote rails project folder
- and deploy automatically with push
Step 1: One Click Create Droplet
On digitalocean’s create droplet page, go to the Applications tab, select Ruby on Rails on 14.04 (Postgres, Nginx, Unicorn), and hit Create.
Step 2: Server Side Git Setup
# ssh log onto your droplet and go to the rails root folder: $ ssh email@example.com $ cd /home/rails/ # configure git $ git config --global user.email "Email@Address.com" $ git config --global user.name "YourUserName" # create an empty folder and git initialise it as a bare repo $ mkdir rails_project.git $ git init --bare rails_project.git # git initialise the existing rails folder as a normal repo $ cd rails_project $ git init $ git add . $ git commit -m "init commit" # link the repos together, use bare repo to track upstream one $ git remote add origin ../rails_project.git $ git push -u origin master
Step 3: Server Auto Deploy Setup
Set up auto deploy with git hook. We’re using the post-receive hook, which runs after the entire update process is completed. It can also be used to update other services, like running auto installs.
$ cat > /home/rails/rails_project.git/hooks/post-receive
Paste these lines in, and then press Control+D to exit.
#!/bin/sh echo "-----------------------------------" echo "Post receive hook: Updating website" export GIT_WORK_TREE=/home/rails/rails_project export GIT_DIR=/home/rails/rails_project.git cd $GIT_WORK_TREE sudo -u root git pull sudo -u root git checkout master -f
Then make it executable by:
$ chmod +x /home/rails/rails_project.git/hooks/post-receive
Step 4: Local Git Setup
If you don’t have Ruby or Rails locally yet, follow this and have Ruby as well as Rails set up. After which,
# pull from your remote git repo with: $ git clone ssh://firstname.lastname@example.org/home/rails/rails_project.git # install gems $ cd rails_project $ bundle install
Step 5: Configure Postgres database
If you haven’t already got postgres installed,
# install postgres $ brew install postgres # create database $ initdb /usr/local/var/postgres # auto launch on login $ mkdir -p ~/Library/LaunchAgents $ ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents $ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
Now that you have postgres installed, go to rails_project/config/database.yml and edit the following line:
# in the first block, change the username to your own username on mac. # note that case matters here. default: &default ... username: your_username_on_mac
# create and migrate local database $ rake db:create:all $ rake db:migrate
Final Step: Enjoy.
# finally, run rails run! $ rails server