rundoc 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,443 @@
1
+ ```
2
+ :::- rundoc
3
+ email = ENV['HEROKU_EMAIL'] || `heroku auth:whoami`
4
+
5
+ Rundoc.configure do |config|
6
+ config.project_root = "myapp"
7
+ config.filter_sensitive(email => "developer@example.com")
8
+ end
9
+ ```
10
+ <!--
11
+ rundoc src:
12
+ https://github.com/schneems/rundoc/blob/master/test/fixtures/rails_5/rundoc.md
13
+
14
+ Command:
15
+ $ bin/rundoc build --path test/fixtures/rails_5/rundoc.md
16
+ -->
17
+
18
+ Ruby on Rails is a popular web framework written in [Ruby](http://www.ruby-lang.org/). This guide covers using Rails 5 on Heroku. For information about running previous versions of Rails on Heroku, see [Getting Started with Rails 4.x on Heroku](getting-started-with-rails4) or [Getting Started with Rails 3.x on Heroku](getting-started-with-rails3).
19
+
20
+ For this guide you will need:
21
+
22
+ - Basic Ruby/Rails knowledge.
23
+ - A locally installed version of Ruby 2.2.0+, Rubygems, Bundler, and Rails 5+.
24
+ - Basic Git knowledge.
25
+ - A Heroku user account: [Signup is free and instant](https://signup.heroku.com/devcenter).
26
+
27
+ ## Local workstation setup
28
+
29
+ Install the [Heroku Toolbelt](https://toolbelt.heroku.com/) on your local workstation. This ensures that you have access to the [Heroku command-line client](/categories/command-line), Heroku Local, and the Git revision control system. You will also need [Ruby and Rails installed](http://guides.railsgirls.com/install).
30
+
31
+ Once installed, you'll have access to the `$ heroku` command from your command shell. Log in using the email address and password you used when creating your Heroku account:
32
+
33
+
34
+ > callout Note that `$` symbol before commands indicates they should be run on the command line, prompt, or terminal with appropriate permissions. Do not copy the `$` symbol.
35
+
36
+ ```term
37
+ $ heroku login
38
+ Enter your Heroku credentials.
39
+ Email: schneems@example.com
40
+ Password:
41
+ Could not find an existing public key.
42
+ Would you like to generate one? [Yn]
43
+ Generating new SSH public key.
44
+ Uploading ssh public key /Users/adam/.ssh/id_rsa.pub
45
+ ```
46
+
47
+ Press enter at the prompt to upload your existing `ssh` key or create a new one, used for pushing code later on.
48
+
49
+ ## Write your app
50
+
51
+ > callout To run on Heroku, your app must be configured to use the Postgres database, have all dependencies declared in your `Gemfile`.
52
+
53
+
54
+ If you are starting from an existing app, [upgrade to Rails 5](http://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html#upgrading-from-rails-4-2-to-rails-5-0) before continuing. If not, a vanilla Rails 5 app will serve as a suitable sample app. To build a new app make sure that you're using the Rails 5.x using `$ rails -v`. You can get the new version of rails by running,
55
+
56
+ ```term
57
+ :::>> $ gem install rails --no-ri --no-rdoc
58
+ ```
59
+
60
+ Then create a new app:
61
+
62
+ ```term
63
+ :::> $ rails new myapp --database=postgresql
64
+ ```
65
+
66
+ Then move into your application directory.
67
+
68
+ ```term
69
+ :::> $ cd myapp
70
+ ```
71
+
72
+ > callout If you experience problems or get stuck with this tutorial, your questions may be answered in a later part of this document. If you experience a problem, try reading through the entire document and then go back to your issue. It can also be useful to review your previous steps to ensure they all executed correctly.
73
+
74
+ ## Database
75
+
76
+ If you already have an app that was created without specifying `--database=postgresql` you will need to add the `pg` gem to your Rails project. Edit your `Gemfile` and change this line:
77
+
78
+ ```ruby
79
+ gem 'sqlite3'
80
+ ```
81
+
82
+ To this:
83
+
84
+ ```ruby
85
+ gem 'pg'
86
+ ```
87
+
88
+ > callout We highly recommend using PostgreSQL during development. Maintaining [parity between your development](http://www.12factor.net/dev-prod-parity) and deployment environments prevents subtle bugs from being introduced because of differences between your environments. [Install Postgres locally](https://devcenter.heroku.com/articles/heroku-postgresql#local-setup) now if it is not already on your system.
89
+
90
+ Now re-install your dependencies (to generate a new `Gemfile.lock`):
91
+
92
+ ```ruby
93
+ $ bundle install
94
+ ```
95
+
96
+ To get more information on why this change is needed and how to configure your app to run Postgres locally, see [why you cannot use Sqlite3 on Heroku](https://devcenter.heroku.com/articles/sqlite3).
97
+
98
+ In addition to using the `pg` gem, you'll also need to ensure the `config/database.yml` is using the `postgresql` adapter.
99
+
100
+ The development section of your `config/database.yml` file should look something like this:
101
+
102
+ ```term
103
+ :::>> $ cat config/database.yml
104
+ ```
105
+
106
+ Be careful here. If you omit the `sql` at the end of `postgresql` in the `adapter` section, your application will not work.
107
+
108
+ ## Welcome page
109
+
110
+ Rails 5 no longer has a static index page in production. When you're using a new app, there will not be a root page in production, so we need to create one. We will first create a controller called `welcome` for our home page to live:
111
+
112
+ ```term
113
+ :::> $ rails generate controller welcome
114
+ ```
115
+
116
+ Next we'll add an index page.
117
+
118
+ ```html
119
+ :::>> file.write app/views/welcome/index.html.erb
120
+ <h2>Hello World</h2>
121
+ <p>
122
+ The time is now: <%= Time.now %>
123
+ </p>
124
+ ```
125
+
126
+ Now we need to make Rails route to this action. We'll edit `config/routes.rb` to set the index page to our new method:
127
+
128
+ ```ruby
129
+ :::>> file.append config/routes.rb#2
130
+ root 'welcome#index'
131
+ ```
132
+
133
+ You can verify that the page is there by running your server:
134
+
135
+ ```term
136
+ $ rails server
137
+ ```
138
+
139
+ And visiting [http://localhost:3000](http://localhost:3000) in your browser. If you do not see the page, [use the logs](#view-the-logs) that are output to your server to debug.
140
+
141
+ ## Heroku gems
142
+
143
+ Previous versions of Rails required you to add a gem to your project [rails_12factor](https://github.com/heroku/rails_12factor) to enable static asset serving and logging on Heroku. If you are deploying a new application this gem is not needed. If you are upgrading an existing application you can remove this gem provided you have the apprpriate configuration in your `config/environments/production.rb` file:
144
+
145
+ ```ruby
146
+ # config/environments/production.rb
147
+ config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
148
+
149
+ if ENV["RAILS_LOG_TO_STDOUT"].present?
150
+ logger = ActiveSupport::Logger.new(STDOUT)
151
+ logger.formatter = config.log_formatter
152
+ config.logger = ActiveSupport::TaggedLogging.new(logger)
153
+ end
154
+ ```
155
+
156
+ ## Specify Ruby version in app
157
+
158
+ Rails 5 requires Ruby 2.2.0 or above. Heroku has a recent version of Ruby installed by default, however you can specify an exact version by using the `ruby` DSL in your `Gemfile`.
159
+
160
+ ```ruby
161
+ :::>> file.append Gemfile
162
+ ruby "2.4.1"
163
+ ```
164
+
165
+ You should also be running the same version of Ruby locally. You can check this by running `$ ruby -v`. You can get more information on [specifying your Ruby version on Heroku here](https://devcenter.heroku.com/articles/ruby-versions).
166
+
167
+ ## Store your app in Git
168
+
169
+ Heroku relies on [Git](http://git-scm.com/), a distributed source control management tool, for deploying your project. If your project is not already in Git, first verify that `git` is on your system:
170
+
171
+ ```term
172
+ :::> $ git --help
173
+ :::>> | $ head -n 5
174
+ ```
175
+
176
+ If you don't see any output or get `command not found` you will need to install it on your system. Verify that the [Heroku toolbelt](https://toolbelt.heroku.com/) is installed.
177
+
178
+ Once you've verified that Git works, first make sure you are in your Rails app directory by running `$ ls`:
179
+
180
+ The output should look like this:
181
+
182
+ ```term
183
+ :::>> $ ls
184
+ ```
185
+
186
+ Now run these commands in your Rails app directory to initialize and commit your code to Git:
187
+
188
+ ```term
189
+ :::> $ git init
190
+ :::> $ git add .
191
+ :::> $ git commit -m "init"
192
+ ```
193
+
194
+ You can verify everything was committed correctly by running:
195
+
196
+ ```term
197
+ :::>> $ git status
198
+ ```
199
+
200
+ Now that your application is committed to Git you can deploy to Heroku.
201
+
202
+ ## Deploy your application to Heroku
203
+
204
+ Make sure you are in the directory that contains your Rails app, then create an app on Heroku:
205
+
206
+ ```term
207
+ :::>> $ heroku create
208
+ ```
209
+
210
+ You can verify that the remote was added to your project by running:
211
+
212
+ ```term
213
+ :::>> $ git config --list | grep heroku
214
+ ```
215
+
216
+ If you see `fatal: not in a git directory` then you are likely not in the correct directory. Otherwise you may deploy your code. After you deploy your code, you will need to migrate your database, make sure it is properly scaled, and use logs to debug any issues that come up.
217
+
218
+ Deploy your code:
219
+
220
+ ```term
221
+ :::>> $ git push heroku master
222
+ ```
223
+
224
+ It is always a good idea to check to see if there are any warnings or errors in the output. If everything went well you can migrate your database.
225
+
226
+ ## Migrate your database
227
+
228
+ If you are using the database in your application you need to manually migrate the database by running:
229
+
230
+ ```term
231
+ $ heroku run rake db:migrate
232
+ ```
233
+
234
+ Any commands after the `heroku run` will be executed on a Heroku [dyno](dynos). You can obtain an interactive shell session by running `$ heroku run bash`.
235
+
236
+ ## Visit your application
237
+
238
+ You've deployed your code to Heroku. You can now instruct Heroku to execute a process type. Heroku does this by running the associated command in a [dyno](dynos), which is a lightweight container that is the basic unit of composition on Heroku.
239
+
240
+ Let's ensure we have one dyno running the `web` process type:
241
+
242
+ ```term
243
+ :::> $ heroku ps:scale web=1
244
+ ```
245
+
246
+ You can check the state of the app's dynos. The `heroku ps` command lists the running dynos of your application:
247
+
248
+ ```term
249
+ :::>> $ heroku ps
250
+ ```
251
+
252
+ Here, one dyno is running.
253
+
254
+ We can now visit the app in our browser with `heroku open`.
255
+
256
+ ```term
257
+ :::>> $ heroku open
258
+ ```
259
+
260
+ You should now see the "Hello World" text we inserted above.
261
+
262
+ Heroku gives you a default web URL for simplicity while you are developing. When you are ready to scale up and use Heroku for production you can add your own [custom domain](https://devcenter.heroku.com/articles/custom-domains).
263
+
264
+ ## View the logs
265
+
266
+ If you run into any problems getting your app to perform properly, you will need to check the logs.
267
+
268
+ You can view information about your running app using one of the [logging commands](logging), `heroku logs`:
269
+
270
+ ```term
271
+ :::>> $ heroku logs
272
+ ```
273
+
274
+ You can also get the full stream of logs by running the logs command with the `--tail` flag option like this:
275
+
276
+ ```term
277
+ $ heroku logs --tail
278
+ ```
279
+
280
+ ## Dyno sleeping and scaling
281
+
282
+ By default, new applications are deployed to a free dyno. Free apps will "sleep" to conserve resources. You can find more information about this behavior by reading about [free dyno behavior](https://devcenter.heroku.com/articles/free-dyno-hours).
283
+
284
+ To avoid dyno sleeping, you can upgrade to a hobby or professional dyno type as described in the [Dyno Types](dyno-types) article. For example, if you migrate your app to a professional dyno, you can easily scale it by running a command telling Heroku to execute a specific number of dynos, each running your web process type.
285
+
286
+ ## Rails console
287
+
288
+ Heroku allows you to run commands in a [one-off dyno](one-off-dynos) - scripts and applications that only need to be executed when needed - using the `heroku run` command. Use this to launch a Rails console process attached to your local terminal for experimenting in your app's environment:
289
+
290
+ ```term
291
+ $ heroku run rails console
292
+ irb(main):001:0> puts 1+1
293
+ 2
294
+ ```
295
+
296
+ Another useful command for debugging is `$ heroku run bash` which will spin up a new dyno and give you access to a bash session.
297
+
298
+ ## Rake
299
+
300
+ Rake can be run as an attached process exactly like the console:
301
+
302
+ ```term
303
+ $ heroku run rake db:migrate
304
+ ```
305
+
306
+ ## Webserver
307
+
308
+ By default, your app's web process runs `rails server`, which uses Puma in Rails 5. If you are upgrading an app you'll need to add `puma` to your application `Gemfile`:
309
+
310
+ ```ruby
311
+ gem 'puma'
312
+ ```
313
+
314
+ Then run
315
+
316
+ ```term
317
+ :::> $ bundle install
318
+ ```
319
+
320
+ Now you are ready to configure your app to use Puma. For this tutorial we will use the default `config/puma.rb` of that ships with Rails 5, but we recommend reading more about configuring your application for maximum performance by [reading the Puma documentation](https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server).
321
+
322
+ Finally you will need to tell Heroku how to run your Rails app by creating a `Procfile` in the root of your application directory.
323
+
324
+ ### Procfile
325
+
326
+ Change the command used to launch your web process by creating a file called [Procfile](procfile) and entering this:
327
+
328
+ ```
329
+ :::>> file.write Procfile
330
+ web: bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development}
331
+ ```
332
+
333
+ > Note: The case of `Procfile` matters, the first letter must be uppercase.
334
+
335
+ We recommend generating a Puma config file based on [our Puma documentation](https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server) for maximum performance.
336
+
337
+ To use the Procfile locally you can use `heroku local`.
338
+
339
+ In addition to running commands in your `Procfile` `heroku local` can also help you manage environment variables locally through a `.env` file. Set the local `RACK_ENV` to development in your environment and a `PORT` to connect to. Before pushing to Heroku you'll want to test with the `RACK_ENV` set to production since this is the environment your Heroku app will run in.
340
+
341
+ ```term
342
+ :::>> $ echo "RACK_ENV=development" >>.env
343
+ :::>> $ echo "PORT=3000" >> .env
344
+ ```
345
+
346
+ > Note: Another alternative to using environment variables locally with a `.env` file is the [dotenv](https://github.com/bkeepers/dotenv) gem.
347
+
348
+ You'll also want to add `.env` to your `.gitignore` since this is for local environment setup.
349
+
350
+ ```term
351
+ :::> $ echo ".env" >> .gitignore
352
+ :::> $ git add .gitignore
353
+ :::> $ git commit -m "add .env to .gitignore"
354
+ ```
355
+
356
+ Test your Procfile locally using Foreman. You can now start your web server by running:
357
+
358
+ ```term
359
+ $ heroku local
360
+ [OKAY] Loaded ENV .env File as KEY=VALUE Format
361
+ 11:06:35 AM web.1 | [18878] Puma starting in cluster mode...
362
+ 11:06:35 AM web.1 | [18878] * Version 3.8.2 (ruby 2.4.1-p111), codename: Sassy Salamander
363
+ 11:06:35 AM web.1 | [18878] * Min threads: 5, max threads: 5
364
+ 11:06:35 AM web.1 | [18878] * Environment: development
365
+ 11:06:35 AM web.1 | [18878] * Process workers: 2
366
+ 11:06:35 AM web.1 | [18878] * Preloading application
367
+ ```
368
+
369
+ Looks good, so press `Ctrl+C` to exit and you can deploy your changes to Heroku:
370
+
371
+ ```term
372
+ :::> $ git add .
373
+ :::> $ git commit -m "use puma via procfile"
374
+ :::> $ git push heroku master
375
+ ```
376
+
377
+ Check `ps`. You'll see that the web process uses your new command specifying Puma as the web server.
378
+
379
+ ```term
380
+ :::>> $ heroku ps
381
+ ```
382
+
383
+ The logs also reflect that we are now using Puma.
384
+
385
+ ```term
386
+ $ heroku logs
387
+ ```
388
+
389
+ ## Rails asset pipeline
390
+
391
+ There are several options for invoking the [Rails asset pipeline](http://guides.rubyonrails.org/asset_pipeline.html) when deploying to Heroku. For general information on the asset pipeline please see the [Rails 3.1+ Asset Pipeline on Heroku Cedar](rails-asset-pipeline) article.
392
+
393
+ The `config.assets.initialize_on_precompile` option has been removed is and not needed for Rails 5. Also, any failure in asset compilation will now cause the push to fail. For Rails 5 asset pipeline support see the [Ruby Support](https://devcenter.heroku.com/articles/ruby-support#rails-5-x-applications) page.
394
+
395
+ ## Troubleshooting
396
+
397
+ If you push up your app and it crashes (`heroku ps` shows state `crashed`), check your logs to find out what went wrong. Here are some common problems.
398
+
399
+ ### Runtime dependencies on development/test gems
400
+
401
+ If you're missing a gem when you deploy, check your Bundler groups. Heroku builds your app without the `development` or `test` groups, and if your app depends on a gem from one of these groups to run, you should move it out of the group.
402
+
403
+ One common example is using the RSpec tasks in your `Rakefile`. If you see this in your Heroku deploy:
404
+
405
+ ```term
406
+ $ heroku run rake -T
407
+ Running `bundle exec rake -T` attached to terminal... up, ps.3
408
+ rake aborted!
409
+ no such file to load -- rspec/core/rake_task
410
+ ```
411
+ Then you've hit this problem. First, duplicate the problem locally:
412
+
413
+ ```term
414
+ $ bundle install --without development:test
415
+
416
+ $ bundle exec rake -T
417
+ rake aborted!
418
+ no such file to load -- rspec/core/rake_task
419
+ ```
420
+
421
+ > Note: The `--without` option on bundler is sticky. You can get rid of this option by running `bundle config --delete without`.
422
+
423
+ Now you can fix it by making these Rake tasks conditional on the gem load. For example:
424
+
425
+ ```ruby
426
+ begin
427
+ require "rspec/core/rake_task"
428
+
429
+ desc "Run all examples"
430
+
431
+ RSpec::Core::RakeTask.new(:spec) do |t|
432
+ t.rspec_opts = %w[--color]
433
+ t.pattern = 'spec/**/*_spec.rb'
434
+ end
435
+ rescue LoadError
436
+ end
437
+ ```
438
+
439
+ Confirm it works locally, then push to Heroku.
440
+
441
+ ## Done
442
+
443
+ You have deployed your first application to Heroku. The next step is to deploy your own application. You can read more about Ruby on Heroku at the [Dev Center](https://devcenter.heroku.com/categories/ruby).