rundoc 0.0.1 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/check_changelog.yml +13 -0
  3. data/.gitignore +9 -0
  4. data/.travis.yml +8 -0
  5. data/CHANGELOG.md +22 -0
  6. data/Dockerfile +24 -0
  7. data/Gemfile +1 -0
  8. data/README.md +276 -74
  9. data/bin/rundoc +4 -52
  10. data/lib/rundoc.rb +15 -1
  11. data/lib/rundoc/cli.rb +84 -0
  12. data/lib/rundoc/code_command.rb +25 -6
  13. data/lib/rundoc/code_command/background.rb +9 -0
  14. data/lib/rundoc/code_command/background/log/clear.rb +17 -0
  15. data/lib/rundoc/code_command/background/log/read.rb +16 -0
  16. data/lib/rundoc/code_command/background/process_spawn.rb +96 -0
  17. data/lib/rundoc/code_command/background/start.rb +38 -0
  18. data/lib/rundoc/code_command/background/stop.rb +17 -0
  19. data/lib/rundoc/code_command/background/wait.rb +19 -0
  20. data/lib/rundoc/code_command/bash.rb +1 -1
  21. data/lib/rundoc/code_command/bash/cd.rb +21 -3
  22. data/lib/rundoc/code_command/file_command/append.rb +16 -11
  23. data/lib/rundoc/code_command/file_command/remove.rb +6 -5
  24. data/lib/rundoc/code_command/no_such_command.rb +4 -0
  25. data/lib/rundoc/code_command/pipe.rb +18 -5
  26. data/lib/rundoc/code_command/raw.rb +18 -0
  27. data/lib/rundoc/code_command/rundoc/depend_on.rb +37 -0
  28. data/lib/rundoc/code_command/rundoc/require.rb +41 -0
  29. data/lib/rundoc/code_command/rundoc_command.rb +6 -2
  30. data/lib/rundoc/code_command/website.rb +7 -0
  31. data/lib/rundoc/code_command/website/driver.rb +111 -0
  32. data/lib/rundoc/code_command/website/navigate.rb +29 -0
  33. data/lib/rundoc/code_command/website/screenshot.rb +28 -0
  34. data/lib/rundoc/code_command/website/visit.rb +47 -0
  35. data/lib/rundoc/code_command/write.rb +22 -7
  36. data/lib/rundoc/code_section.rb +41 -66
  37. data/lib/rundoc/parser.rb +5 -4
  38. data/lib/rundoc/peg_parser.rb +282 -0
  39. data/lib/rundoc/version.rb +2 -2
  40. data/rundoc.gemspec +9 -3
  41. data/test/fixtures/build_logs/rundoc.md +56 -0
  42. data/test/fixtures/depend_on/dependency/rundoc.md +5 -0
  43. data/test/fixtures/depend_on/main/rundoc.md +10 -0
  44. data/test/fixtures/java/rundoc.md +9 -0
  45. data/test/fixtures/rails_4/rundoc.md +151 -188
  46. data/test/fixtures/rails_5/rundoc.md +445 -0
  47. data/test/fixtures/rails_6/rundoc.md +451 -0
  48. data/test/fixtures/require/dependency/rundoc.md +5 -0
  49. data/test/fixtures/require/main/rundoc.md +10 -0
  50. data/test/fixtures/screenshot/rundoc.md +10 -0
  51. data/test/rundoc/code_commands/append_file_test.rb +33 -6
  52. data/test/rundoc/code_commands/background_test.rb +69 -0
  53. data/test/rundoc/code_commands/bash_test.rb +1 -1
  54. data/test/rundoc/code_commands/pipe_test.rb +1 -1
  55. data/test/rundoc/code_commands/remove_contents_test.rb +3 -4
  56. data/test/rundoc/code_section_test.rb +95 -2
  57. data/test/rundoc/parser_test.rb +7 -13
  58. data/test/rundoc/peg_parser_test.rb +381 -0
  59. data/test/rundoc/regex_test.rb +6 -6
  60. data/test/rundoc/test_parse_java.rb +1 -1
  61. data/test/test_helper.rb +1 -3
  62. metadata +143 -18
  63. data/Gemfile.lock +0 -38
@@ -0,0 +1,445 @@
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 on running previous versions of Rails on Heroku, see the tutorial for [Rails 4.x](getting-started-with-rails4) or [Rails 3.x](getting-started-with-rails3).
19
+
20
+ ```
21
+ :::-- $ ruby -e "exit 1 unless RUBY_VERSION == '2.6.3'"
22
+ ```
23
+
24
+ For this guide you will need:
25
+
26
+ - Basic familiarity with Ruby/Rails and Git
27
+ - A locally installed version of Ruby 2.2.0+, Rubygems, Bundler, and Rails 5+
28
+ - A Heroku user account: [Signup is free and instant](https://signup.heroku.com/devcenter).
29
+
30
+ ## Local setup
31
+
32
+ Install the [Heroku CLI](heroku-cli#download-and-install) on your development machine.
33
+
34
+ Once installed, the `heroku` command is available from your terminal. Log in using your Heroku account's email address and password:
35
+
36
+ ```term
37
+ $ heroku login
38
+ heroku: 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
+ ## Create a new Rails app (or upgrade an existing one)
50
+
51
+ If you are starting with an existing app that uses a previous version of Rails, [upgrade it to Rails 5](http://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html#upgrading-from-rails-4-2-to-rails-5-0) before continuing. If you're not starting from an existing app at all, a vanilla Rails 5 app works great as a sample app.
52
+
53
+ To create a new app, first make sure that you're using Rails 5.x by running `rails -v`. If necessary, you can get the new version of rails by running the following:
54
+
55
+ ```term
56
+ :::>> $ gem install rails --no-document
57
+ ```
58
+
59
+ Then create a new app and move into its root directory:
60
+
61
+ ```term
62
+ :::>- $ rails new myapp --database=postgresql
63
+ ```
64
+
65
+ Then move into your application directory.
66
+
67
+ ```term
68
+ :::>- $ cd myapp
69
+ ```
70
+
71
+ Create a database locally:
72
+
73
+ ```
74
+ :::>> $ bin/rake db:create
75
+ ```
76
+
77
+ ## Add the pg gem
78
+
79
+ If you're using an existing app that was created without specifying `--database=postgresql`, you need to add the `pg` gem to your Rails project. Edit your `Gemfile` and change this line:
80
+
81
+ ```ruby
82
+ gem 'sqlite3'
83
+ ```
84
+
85
+ To this:
86
+
87
+ ```ruby
88
+ gem 'pg'
89
+ ```
90
+
91
+ > 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](heroku-postgresql#local-setup) now if it is not already on your system.
92
+
93
+ Now re-install your dependencies (to generate a new `Gemfile.lock`):
94
+
95
+ ```ruby
96
+ $ bundle install
97
+ ```
98
+
99
+ For more information on why Postgres is recommended instead of Sqlite3, see [why you cannot use Sqlite3 on Heroku](sqlite3).
100
+
101
+ In addition to using the `pg` gem, ensure that your `config/database.yml` file is using the `postgresql` adapter. The development section of your `config/database.yml` file should look something like this:
102
+
103
+ ```term
104
+ :::>> $ cat config/database.yml
105
+ ```
106
+
107
+ Be careful here. If you omit the `sql` at the end of `postgresql` in the `adapter` section, your application will not work.
108
+
109
+ ## Create a welcome page
110
+
111
+ Rails 5 no longer has a static index page in production by default. 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:
112
+
113
+ ```term
114
+ :::>- $ rails generate controller welcome
115
+ ```
116
+
117
+ Next we'll add an index page:
118
+
119
+ ```html
120
+ :::>> file.write app/views/welcome/index.html.erb
121
+ <h2>Hello World</h2>
122
+ <p>
123
+ The time is now: <%= Time.now %>
124
+ </p>
125
+ ```
126
+
127
+ 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:
128
+
129
+ ```ruby
130
+ :::>> file.append config/routes.rb#2
131
+ root 'welcome#index'
132
+ ```
133
+
134
+ You can verify that the page is there by running your server:
135
+
136
+ ```term
137
+ :::>> background.start("rails server", name: "server")
138
+ :::-- background.stop(name: "server")
139
+ ```
140
+
141
+ 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.
142
+
143
+ ## Heroku gems
144
+
145
+ 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:
146
+
147
+
148
+ ```ruby
149
+ # config/environments/production.rb
150
+ config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
151
+
152
+ if ENV["RAILS_LOG_TO_STDOUT"].present?
153
+ logger = ActiveSupport::Logger.new(STDOUT)
154
+ logger.formatter = config.log_formatter
155
+ config.logger = ActiveSupport::TaggedLogging.new(logger)
156
+ end
157
+ ```
158
+
159
+ ## Specify your Ruby version
160
+
161
+ 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`. Depending on your version of Ruby that you are currently running it might look like this:
162
+
163
+
164
+ ```ruby
165
+ :::-- $ sed -i'' -e '/^ruby/d' ./Gemfile
166
+ :::-> file.append Gemfile#4
167
+ ruby "2.6.3"
168
+ ```
169
+
170
+ 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).
171
+
172
+ ## Store your app in Git
173
+
174
+ 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:
175
+
176
+ ```term
177
+ :::>- $ git --help
178
+ :::>> | $ head -n 5
179
+ ```
180
+
181
+ If you don't see any output or get `command not found` you need to install Git on your system.
182
+
183
+ Once you've verified that Git works, first make sure you are in your Rails app directory by running `$ ls`:
184
+
185
+ The output should look like this:
186
+
187
+ ```term
188
+ :::>> $ ls
189
+ ```
190
+
191
+ Now run these commands in your Rails app directory to initialize and commit your code to Git:
192
+
193
+ ```term
194
+ :::>- $ git init
195
+ :::>- $ git add .
196
+ :::>- $ git commit -m "init"
197
+ ```
198
+
199
+ You can verify everything was committed correctly by running:
200
+
201
+ ```term
202
+ :::>> $ git status
203
+ ```
204
+
205
+ Now that your application is committed to Git you can deploy to Heroku.
206
+
207
+ ## Deploy your application to Heroku
208
+
209
+ Make sure you are in the directory that contains your Rails app, then create an app on Heroku:
210
+
211
+ ```term
212
+ :::>> $ heroku create
213
+ ```
214
+
215
+ You can verify that the remote was added to your project by running:
216
+
217
+ ```term
218
+ :::>> $ git config --list | grep heroku
219
+ ```
220
+
221
+ If you see `fatal: not in a git directory` then you are likely not in the correct directory. Otherwise you can deploy your code. After you deploy your code, you need to migrate your database, make sure it is properly scaled, and use logs to debug any issues that come up.
222
+
223
+ Deploy your code:
224
+
225
+ ```term
226
+ :::>> $ git push heroku master
227
+ ```
228
+
229
+ 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.
230
+
231
+ ## Migrate your database
232
+
233
+ If you are using the database in your application, you need to manually migrate the database by running:
234
+
235
+ ```term
236
+ $ heroku run rake db:migrate
237
+ ```
238
+
239
+ Any commands after the `heroku run` are executed on a Heroku [dyno](dynos). You can obtain an interactive shell session by running `$ heroku run bash`.
240
+
241
+ ## Visit your application
242
+
243
+ 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.
244
+
245
+ Let's ensure we have one dyno running the `web` process type:
246
+
247
+ ```term
248
+ :::>- $ heroku ps:scale web=1
249
+ ```
250
+
251
+ You can check the state of the app's dynos. The `heroku ps` command lists the running dynos of your application:
252
+
253
+ ```term
254
+ :::>> $ heroku ps
255
+ ```
256
+
257
+ Here, one dyno is running.
258
+
259
+ We can now visit the app in our browser with `heroku open`.
260
+
261
+ ```term
262
+ :::>> $ heroku open
263
+ ```
264
+
265
+ You should now see the "Hello World" text we inserted above.
266
+
267
+ 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).
268
+
269
+ ## View logs
270
+
271
+ If you run into any problems getting your app to perform properly, you will need to check the logs.
272
+
273
+ You can view information about your running app using one of the [logging commands](logging), `heroku logs`:
274
+
275
+ ```term
276
+ :::>> $ heroku logs
277
+ ```
278
+
279
+ You can also get the full stream of logs by running the logs command with the `--tail` flag option like this:
280
+
281
+ ```term
282
+ $ heroku logs --tail
283
+ ```
284
+
285
+ ## Dyno sleeping and scaling
286
+
287
+ 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](free-dyno-hours).
288
+
289
+ 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.
290
+
291
+ ## Run the Rails console
292
+
293
+ 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:
294
+
295
+ ```term
296
+ $ heroku run rails console
297
+ irb(main):001:0> puts 1+1
298
+ 2
299
+ ```
300
+
301
+ Another useful command for debugging is `$ heroku run bash` which will spin up a new dyno and give you access to a bash session.
302
+
303
+ ## Run Rake commands
304
+
305
+ Rake can be run as an attached process exactly like the console:
306
+
307
+ ```term
308
+ $ heroku run rake db:migrate
309
+ ```
310
+
311
+ ## Configure your webserver
312
+
313
+ 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`:
314
+
315
+ ```ruby
316
+ gem 'puma'
317
+ ```
318
+
319
+ Then run
320
+
321
+ ```term
322
+ :::>- $ bundle install
323
+ ```
324
+
325
+ 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).
326
+
327
+ Finally you will need to tell Heroku how to run your Rails app by creating a `Procfile` in the root of your application directory.
328
+
329
+ ### Create a Procfile
330
+
331
+ Change the command used to launch your web process by creating a file called [Procfile](procfile) and entering this:
332
+
333
+ ```
334
+ :::>> file.write Procfile
335
+ web: bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development}
336
+ ```
337
+
338
+ > Note: This file must be named `Procfile` exactly.
339
+
340
+ 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.
341
+
342
+ To use the Procfile locally, you can use `heroku local`.
343
+
344
+ 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.
345
+
346
+ ```term
347
+ :::>> $ echo "RACK_ENV=development" >>.env
348
+ :::>> $ echo "PORT=3000" >> .env
349
+ ```
350
+
351
+ > Note: Another alternative to using environment variables locally with a `.env` file is the [dotenv](https://github.com/bkeepers/dotenv) gem.
352
+
353
+ You'll also want to add `.env` to your `.gitignore` since this is for local environment setup.
354
+
355
+ ```term
356
+ :::>- $ echo ".env" >> .gitignore
357
+ :::>- $ git add .gitignore
358
+ :::>- $ git commit -m "add .env to .gitignore"
359
+ ```
360
+
361
+ Test your Procfile locally using Foreman. You can now start your web server by running:
362
+
363
+ ```term
364
+ :::>> background.start("heroku local", name: "local", wait: "Ctrl-C to stop")
365
+ :::-- background.stop(name: "local")
366
+ ```
367
+
368
+ Looks good, so press `Ctrl+C` to exit and you can deploy your changes to Heroku:
369
+
370
+ ```term
371
+ :::>- $ git add .
372
+ :::>- $ git commit -m "use puma via procfile"
373
+ :::>- $ git push heroku master
374
+ ```
375
+
376
+ Check `ps`. You'll see that the web process uses your new command specifying Puma as the web server.
377
+
378
+ ```term
379
+ :::>> $ heroku ps
380
+ ```
381
+
382
+ The logs also reflect that we are now using Puma.
383
+
384
+ ```term
385
+ $ heroku logs
386
+ ```
387
+
388
+ ## Rails asset pipeline
389
+
390
+ 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.
391
+
392
+ 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](ruby-support#rails-5-x-applications) page.
393
+
394
+ ## Troubleshooting
395
+
396
+ 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.
397
+
398
+ ### Runtime dependencies on development/test gems
399
+
400
+ 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.
401
+
402
+ One common example is using the RSpec tasks in your `Rakefile`. If you see this in your Heroku deploy:
403
+
404
+ ```term
405
+ $ heroku run rake -T
406
+ Running `bundle exec rake -T` attached to terminal... up, ps.3
407
+ rake aborted!
408
+ no such file to load -- rspec/core/rake_task
409
+ ```
410
+ Then you've hit this problem. First, duplicate the problem locally:
411
+
412
+ ```term
413
+ $ bundle install --without development:test
414
+
415
+ $ bundle exec rake -T
416
+ rake aborted!
417
+ no such file to load -- rspec/core/rake_task
418
+ ```
419
+
420
+ > Note: The `--without` option on bundler is sticky. You can get rid of this option by running `bundle config --delete without`.
421
+
422
+ Now you can fix it by making these Rake tasks conditional on the gem load. For example:
423
+
424
+ ```ruby
425
+ begin
426
+ require "rspec/core/rake_task"
427
+
428
+ desc "Run all examples"
429
+
430
+ RSpec::Core::RakeTask.new(:spec) do |t|
431
+ t.rspec_opts = %w[--color]
432
+ t.pattern = 'spec/**/*_spec.rb'
433
+ end
434
+ rescue LoadError
435
+ end
436
+ ```
437
+
438
+ Confirm it works locally, then push to Heroku.
439
+
440
+ ## Next steps
441
+
442
+ Congratulations! You have deployed your first Rails 5 application to Heroku. Here's some recommended reading:
443
+
444
+ * Visit the [Ruby support category](/categories/ruby-support) to learn more about using Ruby and Rails on Heroku.
445
+ * The [Deployment category](/categories/deployment) provides a variety of powerful integrations and features to help streamline and simplify your deployments.
@@ -0,0 +1,451 @@
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
+ <!--
12
+ rundoc src:
13
+ https://github.com/schneems/rundoc/blob/master/test/fixtures/rails_6/rundoc.md
14
+
15
+ Command:
16
+ $ bin/rundoc build --path test/fixtures/rails_6/rundoc.md
17
+ -->
18
+
19
+ Ruby on Rails is a popular web framework written in [Ruby](http://www.ruby-lang.org/). This guide covers using Rails 6 on Heroku. For information on running previous versions of Rails on Heroku, see the tutorial for [Rails 5.x](getting-started-with-rails5) or [Rails 4.x](getting-started-with-rails4).
20
+
21
+ ```
22
+ :::-- $ ruby -e "exit 1 unless RUBY_VERSION == '2.6.5'"
23
+ ```
24
+
25
+ For this guide you will need:
26
+
27
+ - Basic familiarity with Ruby/Rails and Git
28
+ - A locally installed version of Ruby 2.5.0+, Rubygems, Bundler, and Rails 6+
29
+ - A Heroku user account: [Signup is free and instant](https://signup.heroku.com/devcenter).
30
+
31
+ ## Local setup
32
+
33
+ Install the [Heroku CLI](heroku-cli#download-and-install) on your development machine.
34
+
35
+ Once installed, the `heroku` command is available from your terminal. Log in using your Heroku account's email address and password:
36
+
37
+ ```term
38
+ $ heroku login
39
+ heroku: Enter your Heroku credentials
40
+ Email: schneems@example.com
41
+ Password:
42
+ Could not find an existing public key.
43
+ Would you like to generate one? [Yn]
44
+ Generating new SSH public key.
45
+ Uploading ssh public key /Users/adam/.ssh/id_rsa.pub
46
+ ```
47
+
48
+ Press Enter at the prompt to upload your existing `ssh` key or create a new one, used for pushing code later on.
49
+
50
+ ## Create a new Rails app (or upgrade an existing one)
51
+
52
+ To create a new app, first make sure that you're using Rails 6.x by running `rails -v`. If necessary, you can get the new version of rails by running the following:
53
+
54
+ ```term
55
+ :::>> $ gem install rails --no-document
56
+ ```
57
+
58
+ Then create a new app and move into its root directory:
59
+
60
+ ```term
61
+ :::>- $ rails new myapp --database=postgresql
62
+ ```
63
+
64
+ Then move into your application directory.
65
+
66
+ ```term
67
+ :::>- $ cd myapp
68
+ ```
69
+
70
+ Create a database locally:
71
+
72
+ ```
73
+ :::>> $ bin/rails db:create
74
+ ```
75
+
76
+ ## Add the pg gem
77
+
78
+ If you're using an existing app that was created without specifying `--database=postgresql`, you need to add the `pg` gem to your Rails project. Edit your `Gemfile` and change this line:
79
+
80
+ ```ruby
81
+ gem 'sqlite3'
82
+ ```
83
+
84
+ To this:
85
+
86
+ ```ruby
87
+ gem 'pg'
88
+ ```
89
+
90
+ > 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](heroku-postgresql#local-setup) now if it is not already on your system.
91
+
92
+ Now re-install your dependencies (to generate a new `Gemfile.lock`):
93
+
94
+ ```ruby
95
+ $ bundle install
96
+ ```
97
+
98
+ For more information on why Postgres is recommended instead of Sqlite3, see [why you cannot use Sqlite3 on Heroku](sqlite3).
99
+
100
+ In addition to using the `pg` gem, ensure that your `config/database.yml` file is using the `postgresql` adapter. 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
+ ## Create a welcome page
109
+
110
+ Rails 6 no longer has a static index page in production by default. 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
+ :::>> background.start("rails server", name: "server")
137
+ :::-- background.stop(name: "server")
138
+ ```
139
+
140
+ And visiting [http://localhost:3000](http://localhost:3000) in your browser. If you do not see the page, [use the logs](#view-logs) that are output to your server to debug.
141
+
142
+ ## Heroku gems
143
+
144
+ 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 appropriate configuration in your `config/environments/production.rb` file:
145
+
146
+
147
+ ```ruby
148
+ # config/environments/production.rb
149
+ config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
150
+
151
+ if ENV["RAILS_LOG_TO_STDOUT"].present?
152
+ logger = ActiveSupport::Logger.new(STDOUT)
153
+ logger.formatter = config.log_formatter
154
+ config.logger = ActiveSupport::TaggedLogging.new(logger)
155
+ end
156
+ ```
157
+
158
+ ## Specify your Ruby version
159
+
160
+ Rails 6 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`. Depending on your version of Ruby that you are currently running it might look like this:
161
+
162
+
163
+ ```ruby
164
+ :::-- $ sed -i'' -e '/^ruby/d' ./Gemfile
165
+ :::-> file.append Gemfile#4
166
+ ruby "2.6.5"
167
+ ```
168
+
169
+ 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).
170
+
171
+ ## Store your app in Git
172
+
173
+ 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:
174
+
175
+ ```term
176
+ :::>- $ git --help
177
+ :::>> | $ head -n 5
178
+ ```
179
+
180
+ If you don't see any output or get `command not found` you need to install Git on your system.
181
+
182
+ Once you've verified that Git works, first make sure you are in your Rails app directory by running `$ ls`:
183
+
184
+ The output should look like this:
185
+
186
+ ```term
187
+ :::>> $ ls
188
+ ```
189
+
190
+ Now run these commands in your Rails app directory to initialize and commit your code to Git:
191
+
192
+ ```term
193
+ :::>- $ git init
194
+ :::>- $ git add .
195
+ :::>- $ git commit -m "init"
196
+ ```
197
+
198
+ You can verify everything was committed correctly by running:
199
+
200
+ ```term
201
+ :::>> $ git status
202
+ ```
203
+
204
+ Now that your application is committed to Git you can deploy to Heroku.
205
+
206
+ ## Deploy your application to Heroku
207
+
208
+ Make sure you are in the directory that contains your Rails app, then create an app on Heroku:
209
+
210
+ ```term
211
+ :::>> $ heroku create
212
+ ```
213
+
214
+ You can verify that the remote was added to your project by running:
215
+
216
+ ```term
217
+ :::>> $ git config --list | grep heroku
218
+ ```
219
+
220
+ If you see `fatal: not in a git directory` then you are likely not in the correct directory. Otherwise, you can deploy your code. After you deploy your code, you need to migrate your database, make sure it is properly scaled, and use logs to debug any issues that come up.
221
+
222
+ Deploy your code:
223
+
224
+ ```term
225
+ :::>> $ git push heroku master
226
+ ```
227
+
228
+ 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.
229
+
230
+ ## Migrate your database
231
+
232
+ If you are using the database in your application, you need to manually migrate the database by running:
233
+
234
+ ```term
235
+ $ heroku run rake db:migrate
236
+ ```
237
+
238
+ Any commands after the `heroku run` are executed on a Heroku [dyno](dynos). You can obtain an interactive shell session by running `$ heroku run bash`.
239
+
240
+ ## Visit your application
241
+
242
+ 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.
243
+
244
+ Let's ensure we have one dyno running the `web` process type:
245
+
246
+ ```term
247
+ :::>- $ heroku ps:scale web=1
248
+ ```
249
+
250
+ You can check the state of the app's dynos. The `heroku ps` command lists the running dynos of your application:
251
+
252
+ ```term
253
+ :::>> $ heroku ps
254
+ ```
255
+
256
+ Here, one dyno is running.
257
+
258
+ We can now visit the app in our browser with `heroku open`.
259
+
260
+ ```term
261
+ :::>> $ heroku open
262
+ ```
263
+
264
+ You should now see the "Hello World" text we inserted above.
265
+
266
+ 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).
267
+
268
+ ## View logs
269
+
270
+ If you run into any problems getting your app to perform properly, you will need to check the logs.
271
+
272
+ You can view information about your running app using one of the [logging commands](logging), `heroku logs`:
273
+
274
+ ```term
275
+ :::>> $ heroku logs
276
+ ```
277
+
278
+ You can also get the full stream of logs by running the logs command with the `--tail` flag option like this:
279
+
280
+ ```term
281
+ $ heroku logs --tail
282
+ ```
283
+
284
+ ## Dyno sleeping and scaling
285
+
286
+ 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](free-dyno-hours).
287
+
288
+ 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.
289
+
290
+ ## Run the Rails console
291
+
292
+ 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:
293
+
294
+ ```term
295
+ $ heroku run rails console
296
+ irb(main):001:0> puts 1+1
297
+ 2
298
+ ```
299
+
300
+ Another useful command for debugging is `$ heroku run bash` which will spin up a new dyno and give you access to a bash session.
301
+
302
+ ## Run Rake commands
303
+
304
+ Rake can be run as an attached process exactly like the console:
305
+
306
+ ```term
307
+ $ heroku run rake db:migrate
308
+ ```
309
+
310
+ ## Configure your webserver
311
+
312
+ By default, your app's web process runs `rails server`, which uses Puma in Rails 6. If you are upgrading an app you'll need to add `puma` to your application `Gemfile`:
313
+
314
+ ```ruby
315
+ gem 'puma'
316
+ ```
317
+
318
+ Then run
319
+
320
+ ```term
321
+ :::>- $ bundle install
322
+ ```
323
+
324
+ 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 6, 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).
325
+
326
+ Finally you will need to tell Heroku how to run your Rails app by creating a `Procfile` in the root of your application directory.
327
+
328
+ ### Create a Procfile
329
+
330
+ Change the command used to launch your web process by creating a file called [Procfile](procfile) and entering this:
331
+
332
+ ```
333
+ :::>> file.write Procfile
334
+ web: bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development}
335
+ ```
336
+
337
+ > Note: This file must be named `Procfile` exactly.
338
+
339
+ 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.
340
+
341
+ To use the Procfile locally, you can use `heroku local`.
342
+
343
+ 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.
344
+
345
+ ```term
346
+ :::>> $ echo "RACK_ENV=development" >>.env
347
+ :::>> $ echo "PORT=3000" >> .env
348
+ ```
349
+
350
+ > Note: Another alternative to using environment variables locally with a `.env` file is the [dotenv](https://github.com/bkeepers/dotenv) gem.
351
+
352
+ You'll also want to add `.env` to your `.gitignore` since this is for local environment setup.
353
+
354
+ ```term
355
+ :::>- $ echo ".env" >> .gitignore
356
+ :::>- $ git add .gitignore
357
+ :::>- $ git commit -m "add .env to .gitignore"
358
+ ```
359
+
360
+ If you're running Rails 6.0.0 you'll need to create a `tmp/pids` directory:
361
+
362
+ ```term
363
+ :::>> $ mkdir -p tmp/pids
364
+ :::>> $ touch tmp/pids/.gitkeep
365
+ ```
366
+
367
+ Test your Procfile locally using Foreman. You can now start your web server by running:
368
+
369
+ ```term
370
+ :::>> background.start("heroku local", name: "local", wait: "Ctrl-C to stop", timeout: 15)
371
+ :::-- background.stop(name: "local")
372
+ ```
373
+
374
+ Looks good, so press `Ctrl+C` to exit and you can deploy your changes to Heroku:
375
+
376
+ ```term
377
+ :::>- $ git add .
378
+ :::>- $ git commit -m "use puma via procfile"
379
+ :::>- $ git push heroku master
380
+ ```
381
+
382
+ Check `ps`. You'll see that the web process uses your new command specifying Puma as the web server.
383
+
384
+ ```term
385
+ :::>> $ heroku ps
386
+ ```
387
+
388
+ The logs also reflect that we are now using Puma.
389
+
390
+ ```term
391
+ $ heroku logs
392
+ ```
393
+
394
+ ## Rails asset pipeline
395
+
396
+ 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.
397
+
398
+ The `config.assets.initialize_on_precompile` option has been removed is and not needed for Rails 6. Also, any failure in asset compilation will now cause the push to fail. For Rails 6 asset pipeline support see the [Ruby Support](ruby-support#rails-5-x-applications) page.
399
+
400
+ ## Troubleshooting
401
+
402
+ 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.
403
+
404
+ ### Runtime dependencies on development/test gems
405
+
406
+ 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.
407
+
408
+ One common example is using the RSpec tasks in your `Rakefile`. If you see this in your Heroku deploy:
409
+
410
+ ```term
411
+ $ heroku run rake -T
412
+ Running `bundle exec rake -T` attached to terminal... up, ps.3
413
+ rake aborted!
414
+ no such file to load -- rspec/core/rake_task
415
+ ```
416
+ Then you've hit this problem. First, duplicate the problem locally:
417
+
418
+ ```term
419
+ $ bundle install --without development:test
420
+
421
+ $ bundle exec rake -T
422
+ rake aborted!
423
+ no such file to load -- rspec/core/rake_task
424
+ ```
425
+
426
+ > Note: The `--without` option on bundler is sticky. You can get rid of this option by running `bundle config --delete without`.
427
+
428
+ Now you can fix it by making these Rake tasks conditional on the gem load. For example:
429
+
430
+ ```ruby
431
+ begin
432
+ require "rspec/core/rake_task"
433
+
434
+ desc "Run all examples"
435
+
436
+ RSpec::Core::RakeTask.new(:spec) do |t|
437
+ t.rspec_opts = %w[--color]
438
+ t.pattern = 'spec/**/*_spec.rb'
439
+ end
440
+ rescue LoadError
441
+ end
442
+ ```
443
+
444
+ Confirm it works locally, then push to Heroku.
445
+
446
+ ## Next steps
447
+
448
+ Congratulations! You have deployed your first Rails 6 application to Heroku. Here's some recommended reading:
449
+
450
+ * Visit the [Ruby support category](/categories/ruby-support) to learn more about using Ruby and Rails on Heroku.
451
+ * The [Deployment category](/categories/deployment) provides a variety of powerful integrations and features to help streamline and simplify your deployments.