How to create a staging environment heroku

1 Comment

Short version

$ heroku fork --from stark-eyrie-15516 --to automated-tester-staging
$ heroku info -a automated-tester-staging
Git URL:       https://git.heroku.com/automated-tester-staging.git
$ git push staging

Long version

Sometimes it’s nice to have a staging environment for your heroku app (i.e. staging, prod etc).

To create a fresh staging environment for your app we first need to fork it, and then create a git remote name like ‘staging’ and point it to your newly forked app.

1. Fork app

> heroku fork -a appname myapp-staging
...
Fork complete, view it at http://myapp-staging.herokuapp.com (i.e. http://nutshell-staging.herokuapp.com)
  • appname is the name of the heroku app you want to fork. If your’ve already created a heroku app, it will have a name like ‘warm-wave-1832’.
  • to see a list of your heroku apps type:
> heroku apps
  • yourapp-staging is the name of the staging app you want to create (must be unique to heroku, so something like myapp-staging)

2. Create git repos name and point it to your newly created staging environment.

Before you can add a remote to your staging repos, you need the git url.
You can get that by going:

> heroku info -a myapp-staging
Git URL: git@heroku.com:myapp-staging.git

Now simple create a new git remote and point it to your newly created staging URL above:

> git remote add staging git@heroku.com:myapp-staging.git

3. Test.

> git remote -v

heroku git@heroku.com:myapp.git
origin https://github.com/xxx/pss.git
staging git@heroku.com:myapp-staging.git (fetch)

You should now see x3 git remotes.
One for your original ‘heroku’ app.
One for your github repos ‘origin’.
And your new one – ‘staging’

If you want to get rid of the ‘heroku’ git remote and replace it with ‘prod’ do this:

> git remote rm heroku
> git remote add prod git@heroku.com:myapp.git

Now the fun starts! You should now be able to do stuff like this:

> git push prod
> git push staging

And each will go to their own respective heroku instances.

How to create a named app in heroku

4 Comments

Follow these instructions if you want to create a customer named app in Heroku:

http://myapp.herokuapp.com

over

http://tranquil-beach-9125.herokuapp.com

Note: This is different than mapping tranquil-beach-9125 to a real prod domain (i.e. nutshell.com).
To do that you must first create a named app, then point that to your prod domain.

http://myapp.herokuapp.com -> http://www.nutshell.com

Short version
> heroku apps:create myapp
> git remote rm heroku
> git remote add herkou git@heroku.com:myapp.git

heroku-named-app

Long version

Here’s how to create a named app in Heroku assuming you’ve already got one deployed on Heroku with a name like tranquil-beach-9125.

1. Create your named app.

> heroku apps:create myapp
Creating myapp… done, stack is cedar
http://myapp.herokuapp.com/ | git@heroku.com:myapp.git

To see your list of apps:

> heroku apps
myapp

2. Point the git remote ‘heroku’ to your newly named app.

> git remote -v
heroku git@heroku.com:tranquil-beach-9126.git (fetch)
heroku git@heroku.com:tranquil-beach-9126.git (push)
origin https://github.com/xxx/pss.git (fetch)
origin https://github.com/xxx/pss.git (push)

This shows where your git remote heroku is currently pointing (to that hard to remember name heroku created for you when you first created your app).

We are going to remove that git remote name, and then point it back to the newly created named instance we just created.

> git remote rm heroku
> git remote add herkou git@heroku.com:myapp.git

To add prod as a remote to this repos also go:
> git remote add prod git@heroku.com:myapp.git

Voila! We are done. When you now check your git remotes, you should now see heroku pointing to your nicely named repos with origin still pointing to your github instance..

> git remote -v
herkou git@heroku.com:myapp.git (fetch)
herkou git@heroku.com:myapp.git (push)
origin https://github.com/xxx/pss.git (fetch)
origin https://github.com/xxx/pss.git (push)

3. Test it.

To test make a change to your local repos and check it in. Then push to new repos.

> git push heroku
> heroku run rake db:migrate

This will push your repos to your newly created app.

How to import heroku postgres locally from prod rails

1 Comment

Step 1: Setup Postgres locally and get working with your app.

Heroku db:pull only works if you have postgres setup locally on your machine.

Go here for some tips on how to do that.

Step 2: Find out your postgres database name.

Before you can run the db:pull command, you need to know what postgres calls your prod database.

Get the name of your app
$ heroku apps

Then get the name of your postgres instance
$ heroku addons –app yourappname | grep POSTGRES
heroku-postgresql:dev HEROKU_POSTGRESQL_ORANGE

Step 3: Export db from prod.

You are now ready to pull the db from prod. Note this is going to create a new local database (to ensure you don’t accidentally blow over anything).

$ heroku pg:pull HEROKU_POSTGRESQL_ORANGE –app

Step 4: Import locally.

Now you just need to point your local database.yaml file to the newly generated postgres instance just created.

development:
  adapter: postgresql
  host: localhost
  encoding: unicode
  database: <newdatabase>
  pool: 5
  username: jrasmusson
  password:

Step 5: Voila! You are done.

Restart your web server, fire up your app, and you should now see prod data locally running on your box.

Have fun!

Links that help:

https://devcenter.heroku.com/articles/heroku-postgresql#pg-push-and-pg-pull

heroku db:pull no longer working – workaround

Leave a comment

I was disappointed when I discovered that the very handy heroku db:pull stop working for me after upgrading to Ruby 2.0 and Rails 4.0.

I got it going again – by basically reverting back to Ruby 1.9.3 and Rails 3.1.0.

Here are the steps on how to revert back with rvm.

$ rvm install 1.9.3
$ ruby -v
$ rvm use 1.9.3-p286
$ rvm gemset create rails310
$ rvm 1.9.3-p286@rails310
$ rails -v
$ gem install rails -v 3.1.0
$ rvm gemset list
$ heroku db:pull –app targetapp
$ gem install taps
$ gem install sqlite3
$ heroku db:pull –app targetapp

Voila! Done.

How to add a git remote to a heroku app

3 Comments

There are two concepts at play when adding a user friendly heroku name to a git repos.

There is the remote (git) and the app (heroku).

git remote

Git remotes are just aliases for git repositories. You can see what alias are currently setup for your git repos by typing:

> git remote -v

heroku git@heroku.com:.git (fetch)
heroku git@heroku.com:.git (push)
origin git@github.com:/.git (fetch)
origin git@github.com:/.git (push)

origin and heroku are the remotes – and their corresponding git repos.

origin is my git hub repos.
heroku is my remote name for my heroku git repos.

When I type:

> git push heroku

It pushes my code to the heroku git repos using the ‘heroku’ git remote name.

heroku apps

When you create an instance of your code base on Heroku, it gives your app a name. You can see your app names by typing:

> heroku apps

=== My Apps
fathomless-dusk-xxx
secret-waters-xxxx

And you can get more info (like it’s git url) by typing:

> heroku info -a secret-waters-xxxx

=== secret-waters-7537
Addons: heroku-postgresql:dev
Git URL: git@heroku.com:secret-waters-xxxx.git
Owner Email: user@example.com
Region: us
Repo Size: 7M
Slug Size: 33M
Stack: cedar
Tier: Legacy
Web URL: http://secret-waters-xxxx.herokuapp.com/

What we want to do is tie these things together.

Tying a remote to an app

Say I created a staging environment, and I want to replace the heroku generated name it gave me (like secret-water) with a nicer name like ‘staging’. Here is how I would do it.

> git remote add staging git@heroku.com:secret-waters-xxxx.git

Now when I list my apps I should see staging there:

> git remote -v


staging git@heroku.com:secret-waters-xxxx.git (fetch)
staging git@heroku.com:secret-waters-xxxx.git (push)

And I should be able to push to my staging environment with this:

> git push staging

If you make a mistake you can remove a remote with

> git remote rm staging

For more examples of these and other commands feel free to checkout my heroku cheat sheet:

https://agilewarrior.wordpress.com/cheat-sheets/heroku-deploy/

How to upgrade to Rails4 with Heroku

1 Comment

Rails migrations are never easy. But the community does it’s best to help you out. Here are some tips that helped me migrate my app:

Watch this video by Ryan Bates – Upgrading to Rails4

Check out these links

https://devcenter.heroku.com/articles/rails4-getting-started
https://devcenter.heroku.com/articles/multiple-environments

And then see if you run into any other problems like these:

Asset pipeline fails when deploying to Heroku Rails4

If you get this kind of error message

Preparing app for Rails asset pipeline
Running: rake assets:precompile
rake aborted!
could not connect to server: Connection refused
Is the server running on host “127.0.0.1” and accepting

Try this from the command line:

> heroku labs:enable user-env-compile -a

You can get the name of your app be typing:

> heroku apps

http://stackoverflow.com/questions/16432825/heroku-precompile-assets-failed-on-rail-4-app

Images not showing up

If you deploy but no images show up try this:

production.rb

config.cache_classes = true
config.serve_static_assets = true
config.assets.compile = true
config.assets.digest = true

http://stackoverflow.com/questions/16271696/cant-get-css-working-on-heroku-using-rails-4-with-bootstrap-saas-gem

Misc

Other things to try

SampleApp::Application.configure do
  config.serve_static_assets = true
end

http://stackoverflow.com/questions/15354539/heroku-does-not-compile-files-under-assets-piplines-in-rails-4

Links that help

https://devcenter.heroku.com/articles/rails-asset-pipeline

How to change heroku git remote

6 Comments

If case you ever need to map your git remote back to an existing url do this:

> git remote -v

heroku git@heroku.com:protected-reef-4420.git (fetch)
heroku git@heroku.com:protected-reef-4420.git (push)
origin git@github.com:rasmus4200/agilenutshell.git (fetch)
origin git@github.com:rasmus4200/agilenutshell.git (push)

Right now heroku is pointed at protected-reef. But I want it to point to:

hidden-ridge-8790.herokuapp.com

To do that is use this command:

> git remote add heroku git@heroku.com:hidden-ridge-8790.git

> git remote -v
heroku git@heroku.com:hidden-ridge-8790.git (fetch)
heroku git@heroku.com:hidden-ridge-8790.git (push)
origin git@github.com:rasmus4200/agilenutshell.git (fetch)
origin git@github.com:rasmus4200/agilenutshell.git (push)

Done

http://git-scm.com/book/ch2-5.html

Update: If you don’t have your heroku remote created, do this:

> heroku create
> git remote set-url heroku git@heroku.com:hidden-ridge-8790.git

Heroku domains with Google App Engine

1 Comment

These are some notes to remind me how to spin up a new rails app, point it to a URL I bought from Google App Engine, and configure the DNS so my Heroku app points to my www domain.

1. Build your app.

2. Deploy it.

> heroku create –stack cedar
> git push heroku master
> heroku run rake db:migrate

Note the URL of the deployed app in Heroku

—–> Launching… done, v7
http://foo-bar-000.herokuapp.com deployed to Heroku

3. Tell Heroku to point to your domain.

$ heroku domains:add http://www.yourapp.com
Adding http://www.yourapp.com to myapp… done

4. Login into your Google Apps Cpanel domain

google.com/a/yourapp.com

5. Point to your heroku instance.

Domain settings -> Domain names -> Advanced DNS settings

google-domain-settings
Note your URL and password and log into GoDaddy to make DNS config changes.

-> Sign into DNS console

sign-in-dns-console2
The modify the www setting to point to heroku instance.

modify-www-entry-dns

That’s it! You are done.

Tips for sending email via Rails

2 Comments

Some reminders for how to configure email via a rails application.

Watch this:
http://railscasts.com/episodes/274-remember-me-reset-password

Add this:

production.rb

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.smtp_settings = {
    :address              => "smtp.gmail.com",
    :port                 => 587,
    :domain               => 'foo.com',
    :user_name            => 'foo@bar.com',
    :password             => 'xxx',
    :authentication       => 'plain',
    :enable_starttls_auto => true
  }

  config.action_mailer.default_url_options = { :host => "foo.com" }

And ignore the Heroku blog post about emailing via gmail.

Links that help

http://guides.rubyonrails.org/action_mailer_basics.html#action-mailer-configuration-for-gmail

Tips
– restart prod server

Git GitHub Heroku RubyOnRails How to

1 Comment

This is a github cheat sheet I created after going through Michael Hartl’s excellent getting start with git tutorial as part of his also excellent online RubyOnRails book.

For a more thorough walk through go here. For a quick fix read on.

Create new git repository

Assuming you’ve installed rails, git, created a github account, and have a rails app you want to put under version control, navigate to the root of your rails app and create a new git repository as follows:

$ git init

Then create an ignore file at the root (in this case using TextMate) and add the following

$ mate .gitignore

log/*.log
tmp/*
tmp/**/*
doc/api
doc/app
db/*.sqlite3
*.swp
*~
.DS_Store

Add your files to the repository recursively

$ git add .

Do your initial commit

$ git commit -m "Initial commit"

Create your new repository in github using the github UI.

Then push (save) your application to github.

$ git remote add origin git@github.com:<username>/first_app.git
$ git push origin master

There. Your repository is setup!

Branch, edit, commit, merge

Create a new branch

$ git checkout -b myChanges

Make some changes (in this case rename a file)

$ git mv README README2

If you’ve made a mistake, roll it back.

$ git checkout -f

Commit the change to your branch

$ git commit -a -m "Moved the README file"

Merge with master branch

$ git checkout master
$ git merge myChanges

Then push to git server

$ git push

Deploy Heroku

For details on Heroku setup go here.

To create an instance of your app on Heroku and deploy go:

$ heroku create
$ git push heroku master

Then push, deploy, and view your app regularly with:

$ git push
$ git push heroku
$ heroku open

Voila! You’re done!

Other handy git commands

status shows you what branch you are on in case you forget

$ git status
# On branch homepage
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)

log shows you a list of your commit messages

$ git log
commit 69197e9cb7c9b3bd8b8f53bf2bfb65419027c201
Author: foo <bar@gmail.com>
Date:   Tue Sep 7 05:41:20 2010 -0600

     Initial commit

branch shows you what branch you are currently on

$ git branch
  master
* tutorial

You can delete old two ways. The first deletes the branch after you’ve committed your changes. The second abandons the branch and ignores any changes:

$ git branch -d topic-branch
$ git branch -D topic-branch

%d bloggers like this: