rundoc 2.0.1 → 3.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +10 -7
- data/CHANGELOG.md +15 -0
- data/CONTRIBUTING.md +25 -0
- data/README.md +35 -48
- data/Rakefile +5 -1
- data/bin/rundoc +4 -17
- data/lib/rundoc/cli.rb +208 -49
- data/lib/rundoc/cli_argument_parser.rb +140 -0
- data/lib/rundoc/code_command/bash.rb +8 -1
- data/lib/rundoc/code_command/rundoc/depend_on.rb +1 -24
- data/lib/rundoc/code_command/rundoc/require.rb +13 -7
- data/lib/rundoc/code_command/website/driver.rb +7 -5
- data/lib/rundoc/code_command/website/navigate.rb +1 -1
- data/lib/rundoc/code_command/website/screenshot.rb +7 -2
- data/lib/rundoc/code_command/website/visit.rb +4 -2
- data/lib/rundoc/code_section.rb +7 -7
- data/lib/rundoc/context/after_build.rb +14 -0
- data/lib/rundoc/context/execution.rb +22 -0
- data/lib/rundoc/parser.rb +8 -4
- data/lib/rundoc/version.rb +1 -1
- data/lib/rundoc.rb +13 -5
- data/rundoc.gemspec +1 -0
- data/test/fixtures/cnb/ruby/download.md +22 -0
- data/test/fixtures/cnb/ruby/image_structure.md +34 -0
- data/test/fixtures/cnb/ruby/intro.md +5 -0
- data/test/fixtures/cnb/ruby/multiple_langs.md +43 -0
- data/test/fixtures/cnb/ruby/rundoc.md +48 -0
- data/test/fixtures/cnb/ruby/what_is_pack_build.md +18 -0
- data/test/fixtures/cnb/shared/call_to_action.md +11 -0
- data/test/fixtures/cnb/shared/configure_builder.md +23 -0
- data/test/fixtures/cnb/shared/install_pack.md +14 -0
- data/test/fixtures/cnb/shared/pack_build.md +20 -0
- data/test/fixtures/cnb/shared/procfile.md +13 -0
- data/test/fixtures/cnb/shared/use_the_image.md +52 -0
- data/test/fixtures/cnb/shared/what_is_a_builder.md +18 -0
- data/test/fixtures/rails_4/rundoc.md +1 -1
- data/test/fixtures/rails_5/rundoc.md +1 -1
- data/test/fixtures/rails_7/rundoc.md +0 -1
- data/test/fixtures/rails_8/rundoc.md +481 -0
- data/test/fixtures/simple_git/rundoc.md +13 -0
- data/test/integration/after_build_test.rb +62 -0
- data/test/integration/print_test.rb +9 -9
- data/test/integration/require_test.rb +63 -0
- data/test/integration/website_test.rb +35 -0
- data/test/rundoc/cli_argument_parser_test.rb +118 -0
- data/test/rundoc/code_section_test.rb +40 -8
- data/test/rundoc/parser_test.rb +3 -3
- data/test/rundoc/peg_parser_test.rb +6 -6
- data/test/system/exe_cli_test.rb +231 -0
- data/test/test_helper.rb +74 -1
- metadata +41 -3
@@ -0,0 +1,23 @@
|
|
1
|
+
## Configure the default pack builder
|
2
|
+
|
3
|
+
Once `pack` is installed, the only configuration you'll need for this tutorial is to set a default builder:
|
4
|
+
|
5
|
+
```
|
6
|
+
:::>> $ pack config default-builder heroku/builder:22
|
7
|
+
```
|
8
|
+
|
9
|
+
You can view your default builder at any time:
|
10
|
+
|
11
|
+
```
|
12
|
+
:::>> $ pack config default-builder
|
13
|
+
```
|
14
|
+
|
15
|
+
The following tutorial is built on amd64 architecture (also known as x86). If you are building on a machine with different architecture (such as arm64/aarch64 for a Mac) you will need to tell Docker to use `linux/amd64` architecture. You can do this via a `--platform linux/amd64` flag or by exporting an environment variable:
|
16
|
+
|
17
|
+
```
|
18
|
+
$ export DOCKER_DEFAULT_PLATFORM=linux/amd64
|
19
|
+
:::-- rundoc.configure
|
20
|
+
# Needed because all `$` commands are run as separate isolated processes
|
21
|
+
|
22
|
+
ENV["DOCKER_DEFAULT_PLATFORM"] = "linux/amd64"
|
23
|
+
```
|
@@ -0,0 +1,14 @@
|
|
1
|
+
## Install the pack CLI
|
2
|
+
|
3
|
+
We assume you have [docker installed](https://docs.docker.com/engine/install/) and a working copy [of git](https://github.com/git-guides/install-git). Next, you will need to install the CLI tool for building CNBs, [pack CLI](https://buildpacks.io/docs/for-platform-operators/how-to/integrate-ci/pack/). If you're on a Mac you can install it via Homebrew:
|
4
|
+
|
5
|
+
```
|
6
|
+
:::-> print.text
|
7
|
+
$ brew install buildpacks/tap/pack
|
8
|
+
```
|
9
|
+
|
10
|
+
Ensure that `pack` is installed correctly:
|
11
|
+
|
12
|
+
```
|
13
|
+
:::>> $ pack --version
|
14
|
+
```
|
@@ -0,0 +1,20 @@
|
|
1
|
+
## Build the application image with the pack CLI
|
2
|
+
|
3
|
+
Now build an image named `my-image-name` by executing the heroku builder against the application by running the
|
4
|
+
`pack build` command:
|
5
|
+
|
6
|
+
```
|
7
|
+
$ pack build my-image-name --path .
|
8
|
+
:::-- $ docker rmi -f my-image-name
|
9
|
+
:::-- $ pack build my-image-name --path . 2>&1 | tee build_output.txt
|
10
|
+
:::-> $ cat build_output.txt
|
11
|
+
```
|
12
|
+
|
13
|
+
> [!NOTE]
|
14
|
+
> Your output may differ.
|
15
|
+
|
16
|
+
Verify that you see “Successfully built image my-image-name” at the end of the output. And verify that the image is present locally:
|
17
|
+
|
18
|
+
```
|
19
|
+
:::>> $ docker image ls --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}" | grep my-image-name
|
20
|
+
```
|
@@ -0,0 +1,13 @@
|
|
1
|
+
## Configuring your web process with the Procfile
|
2
|
+
|
3
|
+
Most buildpacks rely on existing community standards to allow you to configure your application declaratively. They can also implement custom logic based on file contents on disk or environment variables present at build time.
|
4
|
+
|
5
|
+
The `Procfile` is a configuration file format that was [introduced by Heroku in 2011](https://devcenter.heroku.com/articles/procfile), you can now use this behavior on your CNB-powered application via the `heroku/procfile`, which like the rest of the buildpacks in our builder [is open source](https://github.com/heroku/buildpacks-procfile). The `heroku/procfile` buildpack allows you to configure your web startup process.
|
6
|
+
|
7
|
+
This is the `Procfile` of the getting started guide:
|
8
|
+
|
9
|
+
```
|
10
|
+
:::-> $ cat Procfile
|
11
|
+
```
|
12
|
+
|
13
|
+
By including this file and using `heroku/procfile` buildpack, your application will receive a default web process. You can configure this behavior by changing the contents of that file.
|
@@ -0,0 +1,52 @@
|
|
1
|
+
## Use the image
|
2
|
+
|
3
|
+
Even though we used `pack` and CNBs to build our image, it can be run with your favorite tools like any other OCI image. We will be using the `docker` command line to run our image.
|
4
|
+
|
5
|
+
By default, images will be booted into a web server configuration. You can launch the app we just built by running:
|
6
|
+
|
7
|
+
```
|
8
|
+
$ docker run -it --rm --env PORT=9292 -p 9292:9292 my-image-name
|
9
|
+
:::-> background.start("docker run --rm --env PORT=9292 -p 9292:9292 my-image-name", name: "docker_server")
|
10
|
+
:::-- $ sleep 10
|
11
|
+
:::-> background.log.read(name: "docker_server")
|
12
|
+
```
|
13
|
+
|
14
|
+
Now when you visit [http://localhost:9292](http://localhost:9292) you should see a working web application:
|
15
|
+
|
16
|
+
```
|
17
|
+
:::>> website.visit(name: "localhost", url: "http://localhost:9292")
|
18
|
+
:::>> website.screenshot(name: "localhost")
|
19
|
+
```
|
20
|
+
|
21
|
+
Don't forget to stop the docker container when you're done.
|
22
|
+
|
23
|
+
```
|
24
|
+
:::-- $ docker stop $(docker ps -q --filter ancestor=my-image-name )
|
25
|
+
:::-- background.stop(name: "docker_server")
|
26
|
+
```
|
27
|
+
|
28
|
+
Here's a quick breakdown of that command we just ran:
|
29
|
+
|
30
|
+
- `docker run` Create and run a new container from an image.
|
31
|
+
- `-it` Makes the container interactive and allocates a TTY.
|
32
|
+
- `--rm` Automatically remove the container when it exits.
|
33
|
+
- `--env PORT=9292` Creates an environment variable named `PORT` and sets it to `9292` this is needed so the application inside the container knows what port to bind the web server.
|
34
|
+
- `-p 9292:9292` Publishes a container's port(s) to the host. This is what allows requests from your machine to be received by the container.
|
35
|
+
- `my-image-name` The name of the image you want to use for the application.
|
36
|
+
|
37
|
+
So far, we've downloaded an application via git and run a single command `pack build` to generate an image, and then we can use that image as if it was generated via a Dockerfile via the `docker run` command.
|
38
|
+
|
39
|
+
In addition to running the image as a web server, you can access the container's terminal interactively. In a new terminal window try running this command:
|
40
|
+
|
41
|
+
```
|
42
|
+
$ docker run -it --rm my-image-name bash
|
43
|
+
```
|
44
|
+
|
45
|
+
Now you can inspect the container interactively. For example, you can see the files on disk with `ls`:
|
46
|
+
|
47
|
+
```
|
48
|
+
$ ls
|
49
|
+
:::-> $ docker run --rm my-image-name ls
|
50
|
+
```
|
51
|
+
|
52
|
+
And anything else you would typically do via an interactive container session.
|
@@ -0,0 +1,18 @@
|
|
1
|
+
## What is a builder?
|
2
|
+
|
3
|
+
> [!NOTE]
|
4
|
+
> Skip ahead if you want to build the application first and get into the details later. You won't need to
|
5
|
+
> know about builders for the rest of this tutorial.
|
6
|
+
|
7
|
+
In short, a builder is a delivery mechanism for buildpacks. A builder contains references to base images and individual buildpacks. A base image contains the operating system and system dependencies. Buildpacks are the components that will configure an image to run your application, that’s where the bulk of the logic lives and why the project is called “Cloud Native Buildpacks” and not “Cloud Native Builders.”
|
8
|
+
|
9
|
+
You can view the contents of a builder via the command `pack builder inspect`. For example:
|
10
|
+
|
11
|
+
```
|
12
|
+
:::>> $ pack builder inspect heroku/builder:22 | grep Buildpacks: -m1 -A10
|
13
|
+
```
|
14
|
+
|
15
|
+
> [!NOTE]
|
16
|
+
> Your output version numbers may differ.
|
17
|
+
|
18
|
+
This output shows the various buildpacks that represent the different languages that are supported by this builder such as `heroku/go` and `heroku/nodejs-engine`.
|
@@ -19,7 +19,7 @@ end
|
|
19
19
|
> If you are starting a new application, we recommend you use the most recently released version of Rails.
|
20
20
|
|
21
21
|
>warning
|
22
|
-
>As of November 28th, 2022, free Heroku dynos, free Heroku Postgres and free Heroku Data for
|
22
|
+
>As of November 28th, 2022, free Heroku dynos, free Heroku Postgres and free Heroku Data for Key-Value Store plans are [no longer available](https://blog.heroku.com/next-chapter).
|
23
23
|
>
|
24
24
|
>We recommend using our [low-cost plans](https://blog.heroku.com/new-low-cost-plans) to complete this tutorial. Eligible students can apply for platform credits through our new [Heroku for GitHub Students program](https://blog.heroku.com/github-student-developer-program).
|
25
25
|
|
@@ -22,7 +22,7 @@ end
|
|
22
22
|
> If you are starting a new application, we recommend you use the most recently released version of Rails.
|
23
23
|
|
24
24
|
>warning
|
25
|
-
>As of November 28th, 2022, free Heroku dynos, free Heroku Postgres and free Heroku
|
25
|
+
>As of November 28th, 2022, free Heroku dynos, free Heroku Postgres and free Heroku Key-Value Store plans are [no longer available](https://blog.heroku.com/next-chapter).
|
26
26
|
>
|
27
27
|
>We recommend using our [low-cost plans](https://blog.heroku.com/new-low-cost-plans) to complete this tutorial. Eligible students can apply for platform credits through our new [Heroku for GitHub Students program](https://blog.heroku.com/github-student-developer-program).
|
28
28
|
|
@@ -0,0 +1,481 @@
|
|
1
|
+
```
|
2
|
+
:::-- rundoc
|
3
|
+
email = ENV['HEROKU_EMAIL'] || `heroku auth:whoami`
|
4
|
+
|
5
|
+
Rundoc.configure do |config|
|
6
|
+
config.filter_sensitive(email => "developer@example.com")
|
7
|
+
config.filter_sensitive(Dir.pwd => ".")
|
8
|
+
end
|
9
|
+
```
|
10
|
+
|
11
|
+
<!--
|
12
|
+
rundoc src:
|
13
|
+
https://github.com/schneems/rundoc/blob/main/test/fixtures/rails_8/rundoc.md
|
14
|
+
|
15
|
+
Command:
|
16
|
+
$ bin/rundoc test/fixtures/rails_8/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 8 on Heroku. For information on running previous versions of Rails on Heroku, see the tutorial for [Rails 7.x](getting-started-with-rails7) or [Rails 6.x](getting-started-with-rails6).
|
20
|
+
|
21
|
+
```
|
22
|
+
:::-- $ ruby -e "exit 1 unless RUBY_VERSION == '3.2.6'"
|
23
|
+
```
|
24
|
+
|
25
|
+
The tutorial assumes that you have:
|
26
|
+
|
27
|
+
- Basic familiarity with Ruby, Ruby on Rails, and Git
|
28
|
+
- A locally installed version of Ruby 3.2.0+, Rubygems, Bundler, and Rails 8+
|
29
|
+
- A locally installed version of the [Heroku CLI](heroku-cli#install-the-heroku-cli)
|
30
|
+
- A [verified Heroku Account](https://devcenter.heroku.com/articles/account-verification)
|
31
|
+
- A subscription to the [Eco dynos plan](eco-dyno-hours) (recommended)
|
32
|
+
|
33
|
+
>note
|
34
|
+
>Using dynos and databases to complete this tutorial counts towards your usage. We recommend using our [low-cost plans](https://blog.heroku.com/new-low-cost-plans) to complete this tutorial. Eligible students can apply for platform credits through our new [Heroku for GitHub Students program](https://blog.heroku.com/github-student-developer-program).
|
35
|
+
|
36
|
+
## Local Setup
|
37
|
+
|
38
|
+
After installing the [Heroku CLI](heroku-cli#install-the-heroku-cli), log in through your terminal:
|
39
|
+
|
40
|
+
```term
|
41
|
+
$ heroku login
|
42
|
+
heroku: Press any key to open up the browser to login or q to exit
|
43
|
+
› Warning: If browser does not open, visit
|
44
|
+
› https://cli-auth.heroku.com/auth/browser/***
|
45
|
+
heroku: Waiting for login...
|
46
|
+
Logging in... done
|
47
|
+
Logged in as developer@example.com
|
48
|
+
```
|
49
|
+
|
50
|
+
This command opens your web browser to the Heroku login page. If your browser is already logged in to Heroku, click the **`Log in`** button on the page.
|
51
|
+
|
52
|
+
This authentication is required for the `heroku` and `git` commands to work correctly.
|
53
|
+
|
54
|
+
>note
|
55
|
+
> If you're behind a firewall that uses a proxy to connect with external HTTP/HTTPS services, [set the `HTTP_PROXY` or `HTTPS_PROXY` environment variables](articles/using-the-cli#using-an-http-proxy) in your local development environment before running the `heroku` command.
|
56
|
+
|
57
|
+
## Create a New or Upgrade an Existing Rails App
|
58
|
+
|
59
|
+
Ensure you have Rails 8 installed by running `rails -v` before creating an app. If necessary, install Rails 8 with `gem install`:
|
60
|
+
|
61
|
+
```term
|
62
|
+
:::>> $ gem install rails --no-document
|
63
|
+
```
|
64
|
+
|
65
|
+
Create a Rails app:
|
66
|
+
|
67
|
+
```term
|
68
|
+
:::>- $ rails new myapp --database=postgresql
|
69
|
+
```
|
70
|
+
|
71
|
+
Move into the application directory
|
72
|
+
|
73
|
+
```term
|
74
|
+
:::>- $ cd myapp
|
75
|
+
:::>> $ ls -1
|
76
|
+
```
|
77
|
+
|
78
|
+
<!-- The `bundle install` after `bundle lock` can be removed after https://github.com/rails/rails/issues/48278 is closed -->
|
79
|
+
|
80
|
+
Create a local database:
|
81
|
+
|
82
|
+
```term
|
83
|
+
:::>> $ bin/rails db:create
|
84
|
+
```
|
85
|
+
|
86
|
+
## Add the pg Gem
|
87
|
+
|
88
|
+
For new or existing apps where `--database=postgresql` isn’t defined, confirm the `sqlite3` gem doesn’t exist in the `Gemfile`. Add the `pg` gem in its place.
|
89
|
+
|
90
|
+
Within the `Gemfile` remove:
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
gem 'sqlite3'
|
94
|
+
```
|
95
|
+
|
96
|
+
Replace it with:
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
gem 'pg'
|
100
|
+
```
|
101
|
+
|
102
|
+
> callout
|
103
|
+
>Heroku highly recommends using PostgreSQL locally during development. Maintaining [parity between development](http://www.12factor.net/dev-prod-parity) and deployment environments prevents introducing subtle bugs due to the differences in environments.
|
104
|
+
>
|
105
|
+
> [Install Postgres locally](heroku-postgresql#local-setup). For more information on why Postgres is recommended instead of Sqlite3, see [why Sqlite3 is not compatible with Heroku](sqlite3).
|
106
|
+
|
107
|
+
With the `Gemfile` updated, reinstall the dependencies:
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
$ bundle install
|
111
|
+
```
|
112
|
+
|
113
|
+
The installation also updates `Gemfile.lock` with the changes.
|
114
|
+
|
115
|
+
In addition to the `pg` gem, ensure that `config/database.yml` defines the `postgresql` adapter. The development section of `config/database.yml` file looks something like this:
|
116
|
+
|
117
|
+
```term
|
118
|
+
:::>> $ cat config/database.yml
|
119
|
+
```
|
120
|
+
|
121
|
+
Be careful here. If the value of `adapter` is `postgres` and not `postgresql`, the application won’t work.
|
122
|
+
|
123
|
+
## Create a Welcome Page
|
124
|
+
|
125
|
+
Rails 8 no longer has a static index page in production by default. Apps upgraded to Rails 8 keep their existing page configurations, but new Rails 8 apps don't automatically generate a welcome page. Create a `welcome` controller to hold the homepage:
|
126
|
+
|
127
|
+
```term
|
128
|
+
:::>- $ rails generate controller welcome
|
129
|
+
```
|
130
|
+
|
131
|
+
Create `app/views/welcome/index.html.erb` and add the following code:
|
132
|
+
|
133
|
+
```html
|
134
|
+
:::-> file.write app/views/welcome/index.html.erb
|
135
|
+
<h2>Hello World</h2>
|
136
|
+
<p>
|
137
|
+
The time is now: <%= Time.now %>
|
138
|
+
</p>
|
139
|
+
```
|
140
|
+
|
141
|
+
With a welcome page created, create a route to map to this action.
|
142
|
+
|
143
|
+
```ruby
|
144
|
+
:::>> file.append config/routes.rb#2
|
145
|
+
root 'welcome#index'
|
146
|
+
```
|
147
|
+
|
148
|
+
Verify the page is present by starting the Rails web server:
|
149
|
+
|
150
|
+
```term
|
151
|
+
:::>> background.start("rails server", name: "server")
|
152
|
+
:::-- background.stop(name: "server")
|
153
|
+
```
|
154
|
+
|
155
|
+
Visit [http://localhost:3000](http://localhost:3000) in a browser. If the page doesn’t display, [reference the logs](#view-application-logs) to debug the error. Rails outputs logs in the same terminal where `rails server` was started.
|
156
|
+
|
157
|
+
## Specify the Ruby Version
|
158
|
+
|
159
|
+
Rails 8 requires Ruby 3.2.0 or above. Heroku installs a recent version of Ruby by default. Specify an exact version with the `ruby` DSL in `Gemfile`. For example:
|
160
|
+
|
161
|
+
```ruby
|
162
|
+
:::-- $ sed -i'' -e '/^ruby/d' ./Gemfile
|
163
|
+
:::-> file.append Gemfile#4
|
164
|
+
ruby "3.2.6"
|
165
|
+
```
|
166
|
+
|
167
|
+
Update the `RUBY VERSION` in the `Gemfile.lock` by running:
|
168
|
+
|
169
|
+
```term
|
170
|
+
:::>> $ bundle update --ruby
|
171
|
+
```
|
172
|
+
|
173
|
+
Verify the change:
|
174
|
+
|
175
|
+
```term
|
176
|
+
:::>> $ cat Gemfile.lock | grep RUBY -a1
|
177
|
+
```
|
178
|
+
|
179
|
+
Always use the same version of Ruby locally. Confirm the local version of ruby with `ruby -v`. Refer to the [Ruby Versions](ruby-versions) article for more details on defining a specific ruby version.
|
180
|
+
|
181
|
+
## Create a Procfile
|
182
|
+
|
183
|
+
Use a [Procfile](procfile), a text file in the root directory of your application, to explicitly declare what command to execute to start your app.
|
184
|
+
|
185
|
+
This Procfile declares a single process type, `web`, and the command needed to run it. The name `web` is important here. It declares that this process type is attached to Heroku's [HTTP routing](http-routing) stack and receives web traffic when deployed.
|
186
|
+
|
187
|
+
By default, a Rails app’s web process runs `rails server`, which uses Puma in Rails 8. When you deploy a Rails 8 application without a `Procfile`, this command executes. However, we recommend explicitly declaring how to boot your server process via a `Procfile`. For example:
|
188
|
+
|
189
|
+
```
|
190
|
+
:::-> file.write Procfile
|
191
|
+
web: bundle exec puma -C config/puma.rb
|
192
|
+
```
|
193
|
+
|
194
|
+
>note
|
195
|
+
>The `Procfile` filename is case sensitive. There is no file extension.
|
196
|
+
|
197
|
+
If `config/puma.rb` doesn’t exist, create one using [Heroku’s Puma documentation](https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server) for maximum performance.
|
198
|
+
|
199
|
+
A Procfile can contain additional process types. For example, you can declare a [background worker process](background-jobs-queueing#process-model) that processes items off a queue.
|
200
|
+
|
201
|
+
## Store The App in Git
|
202
|
+
|
203
|
+
Heroku relies on [Git](http://git-scm.com/), a distributed source control management tool, for deploying applications. If the application is not already in Git, first verify that `git` is on the system with `git --help`:
|
204
|
+
|
205
|
+
```term
|
206
|
+
:::>- $ git --help
|
207
|
+
:::>> | $ head -n 5
|
208
|
+
```
|
209
|
+
|
210
|
+
If the command produces no output or `command not found`, [install Git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git).
|
211
|
+
|
212
|
+
Navigate to the root directory of the Rails app. Use the `ls` command to see its contents:
|
213
|
+
|
214
|
+
```term
|
215
|
+
:::>> $ ls
|
216
|
+
```
|
217
|
+
|
218
|
+
Within the Rails app directly, initialize a local empty Git repository and commit the app’s code:
|
219
|
+
|
220
|
+
```term
|
221
|
+
:::>- $ git init
|
222
|
+
:::>- $ git add .
|
223
|
+
:::>- $ git commit -m "init"
|
224
|
+
```
|
225
|
+
|
226
|
+
Verify everything committed correctly with `git status`:
|
227
|
+
|
228
|
+
```term
|
229
|
+
:::>> $ git status
|
230
|
+
```
|
231
|
+
|
232
|
+
With the application committed to Git, it's ready to deploy to Heroku.
|
233
|
+
|
234
|
+
## Create a Heroku App
|
235
|
+
|
236
|
+
>warning
|
237
|
+
>Using a dyno and a database to complete this tutorial counts towards your usage. [Delete your app](https://devcenter.heroku.com/articles/heroku-cli-commands#heroku-apps-destroy), and [database](https://devcenter.heroku.com/articles/heroku-postgresql#removing-the-add-on) as soon as you're done to control costs.
|
238
|
+
|
239
|
+
To create an app on Heroku, use the Heroku CLI Inside the Rails app’s root directory:
|
240
|
+
|
241
|
+
```term
|
242
|
+
:::>> $ heroku apps:create
|
243
|
+
```
|
244
|
+
|
245
|
+
When you create an app, a git remote called `heroku` is also created and associated with your local git repository. Git remotes are versions of your repository that live on other servers. You deploy your app by pushing its code to that special Heroku-hosted remote associated with your app. Verify the remote is set with `git config`:
|
246
|
+
|
247
|
+
```term
|
248
|
+
:::>> $ git config --list --local | grep heroku
|
249
|
+
```
|
250
|
+
|
251
|
+
If the current directory is incorrect or Git isn't [initialized](#store-the-app-in-git), Git returns `fatal: not in a git directory`. If Git returns a list of remotes, it's ready to deploy.
|
252
|
+
|
253
|
+
>note
|
254
|
+
>Following changes in the industry, Heroku [updated the default branch name](https://devcenter.heroku.com/changelog-items/1829) to `main`. If the project uses `master` as its default branch name, use `git push heroku master`.
|
255
|
+
|
256
|
+
## Provision a Database
|
257
|
+
|
258
|
+
Provision a [Heroku Postgres](https://devcenter.heroku.com/articles/heroku-postgresql) database, one of the add-ons available through the [Elements Marketplace](https://www.heroku.com/elements/addons). Add-ons are cloud services that provide out-of-the-box additional services for your application, such as logging, monitoring, databases, and more.
|
259
|
+
|
260
|
+
>note
|
261
|
+
>A `mini` Postgres size costs [$5 a month, prorated to the minute](https://elements.heroku.com/addons/heroku-postgresql). At the end of this tutorial, we prompt you to [delete your database](https://devcenter.heroku.com/articles/heroku-postgresql#removing-the-add-on) to minimize costs.
|
262
|
+
|
263
|
+
```term
|
264
|
+
:::>> $ heroku addons:create heroku-postgresql:essential-0
|
265
|
+
```
|
266
|
+
|
267
|
+
Your Heroku app can now access this Postgres database. The `DATABASE_URL` environment variable stores your credentials, which Rails connects to by convention.
|
268
|
+
|
269
|
+
## Deploy the App to Heroku
|
270
|
+
|
271
|
+
>warning
|
272
|
+
>Using a dyno to complete this tutorial counts towards your usage. [Delete your app](https://devcenter.heroku.com/articles/heroku-cli-commands#heroku-apps-destroy) as soon as you're done to control costs.
|
273
|
+
|
274
|
+
Deploy your code. This command pushes the `main` branch of the sample repo to your `heroku` remote, which then deploys to Heroku:
|
275
|
+
|
276
|
+
```term
|
277
|
+
:::>> $ git push heroku main
|
278
|
+
```
|
279
|
+
|
280
|
+
If the output displays warnings or error messages, check the output and make adjustments.
|
281
|
+
|
282
|
+
After a successful deployment, complete these tasks as necessary:
|
283
|
+
|
284
|
+
* Database migrations
|
285
|
+
* Scale your dynos
|
286
|
+
* Check the app’s logs if issues arise
|
287
|
+
|
288
|
+
## Migrate The Database
|
289
|
+
|
290
|
+
If you're using a database in your application, trigger a migration by using the Heroku CLI to start a one-off [dyno](dynos). You can run commands, typically scripts and applications that are part of your app, in one-off dynos using the `heroku run` command. You can trigger a database migration with this command:
|
291
|
+
|
292
|
+
```term
|
293
|
+
$ heroku run rake db:migrate
|
294
|
+
```
|
295
|
+
|
296
|
+
To use an interactive shell session instead, you can execute `heroku run bash`.
|
297
|
+
|
298
|
+
## Scale and Access the Application
|
299
|
+
|
300
|
+
Heroku runs application code using defined processes and [process types](procfile). New applications don't have a process type active by default. The following command scales your app up to one dyno, running the `web` process:
|
301
|
+
|
302
|
+
```term
|
303
|
+
:::>- $ heroku ps:scale web=1
|
304
|
+
```
|
305
|
+
|
306
|
+
Use the Heroku CLI’s `ps` command to display the state of all app dynos in the terminal:
|
307
|
+
|
308
|
+
```term
|
309
|
+
:::>> $ heroku ps
|
310
|
+
```
|
311
|
+
|
312
|
+
In this example, a single `web` process is running.
|
313
|
+
|
314
|
+
By default, apps use Eco dynos if you're subscribed to [Eco](eco-dyno-hours). Otherwise, it defaults to Basic dynos. The Eco dynos plan is shared across all Eco dynos in your account and is recommended if you plan on deploying many small apps to Heroku. Eco dynos sleep if they don't receive any traffic for half an hour. This sleep behavior causes a few seconds delay for the first request upon waking. Eco dynos consume from a monthly, account-level quota of [eco dyno hours](eco-dyno-hours). As long as you haven't exhausted the quota, your apps can continue to run.
|
315
|
+
|
316
|
+
To avoid dyno sleeping, upgrade to a Basic or higher dyno type as described in the [Dyno Types](dyno-types) article. Upgrading to at least Standard dynos also allows you to scale up to multiple dynos per process type.
|
317
|
+
|
318
|
+
To launch the app in the browser, run `heroku open`:
|
319
|
+
|
320
|
+
```term
|
321
|
+
:::>> $ heroku open
|
322
|
+
```
|
323
|
+
|
324
|
+
The browser displays the “Hello World” text. If it doesn't, or there's an error, [review and confirm the welcome page contents](#create-a-welcome-page).
|
325
|
+
|
326
|
+
Heroku provides a [default web URL](app-names-and-subdomains) for every application during development. When the application is ready for production, add a [custom domain](https://devcenter.heroku.com/articles/custom-domains).
|
327
|
+
|
328
|
+
## View Application Logs
|
329
|
+
|
330
|
+
The app logs are a valuable tool if the app is not performing correctly or generating errors.
|
331
|
+
|
332
|
+
View information about a running app using the Heroku CLI [logging command](logging), `heroku logs`. Here's example output:
|
333
|
+
|
334
|
+
```term
|
335
|
+
:::>> $ heroku logs
|
336
|
+
```
|
337
|
+
|
338
|
+
Append `-t`/`--tail` to the command to see a full, live stream of the app’s logs:
|
339
|
+
|
340
|
+
```term
|
341
|
+
$ heroku logs --tail
|
342
|
+
```
|
343
|
+
|
344
|
+
By default, Heroku stores 1500 lines of logs from your application, but the full log stream is available as a service. Several [add-on providers](https://elements.heroku.com/addons/#logging) have logging services that provide things such as log persistence, search, and email and SMS alerts.
|
345
|
+
|
346
|
+
## Optional Steps
|
347
|
+
|
348
|
+
### Use The Rails Console
|
349
|
+
|
350
|
+
Use the Heroku CLI `run` command to trigger [one-off dynos](one-off-dynos) to run scripts and applications only when necessary. Use the command to launch a Rails console process attached to the local terminal for experimenting in the app's environment:
|
351
|
+
|
352
|
+
```term
|
353
|
+
$ heroku run rails console
|
354
|
+
irb(main):001:0> puts 1+1
|
355
|
+
2
|
356
|
+
```
|
357
|
+
|
358
|
+
The `run bash` Heroku CLI command is also helpful for debugging. The command starts a new one-off dyno with an interactive bash session.
|
359
|
+
|
360
|
+
### Run Rake Commands
|
361
|
+
|
362
|
+
Run `rake` commands, such as `db:migrate`, using the `run` command exactly like the Rails console:
|
363
|
+
|
364
|
+
```term
|
365
|
+
$ heroku run rake db:migrate
|
366
|
+
```
|
367
|
+
|
368
|
+
### Use a Procfile locally
|
369
|
+
|
370
|
+
To use the `Procfile` locally, use the `heroku local` CLI command.
|
371
|
+
|
372
|
+
In addition to running commands in the `Procfile`, the `heroku local` command can also manage environment variables locally through a `.env` file. Set `RACK_ENV` to `development` for the local environment and the `PORT` for Puma.
|
373
|
+
|
374
|
+
```term
|
375
|
+
:::>> $ echo "RACK_ENV=development" >>.env
|
376
|
+
:::>> $ echo "PORT=3000" >> .env
|
377
|
+
```
|
378
|
+
|
379
|
+
>note
|
380
|
+
> Another alternative to using environment variables locally with a `.env` file is the [dotenv](https://github.com/bkeepers/dotenv) gem.
|
381
|
+
|
382
|
+
Add `.env` to `.gitignore` as these variables are for local environment setup only.
|
383
|
+
|
384
|
+
```term
|
385
|
+
:::>- $ echo ".env" >> .gitignore
|
386
|
+
:::>- $ git add .gitignore
|
387
|
+
:::>- $ git commit -m "add .env to .gitignore"
|
388
|
+
```
|
389
|
+
|
390
|
+
Test the Procfile locally using [Foreman](heroku-local#run-your-app-locally-using-foreman). Start the web server with `local`:
|
391
|
+
|
392
|
+
```term
|
393
|
+
:::>> background.start("heroku local", name: "local", wait: "Ctrl-C to stop", timeout: 15)
|
394
|
+
:::-- background.stop(name: "local")
|
395
|
+
```
|
396
|
+
|
397
|
+
Press `Ctrl+C` or `Cmd+C` to exit.
|
398
|
+
|
399
|
+
## Troubleshooting
|
400
|
+
|
401
|
+
If an app deployed to Heroku crashes, for example, `heroku ps` shows the state `crashed`, review the app’s logs. The following section covers common causes of app crashes.
|
402
|
+
|
403
|
+
### Runtime Dependencies on Development or Test Gems
|
404
|
+
|
405
|
+
If a gem is missing during deployment, check the Bundler groups. Heroku builds apps without the `development` or `test` groups, and if the app depends on a gem from one of these groups to run, move it out of the group.
|
406
|
+
|
407
|
+
A common example is using the RSpec tasks in the `Rakefile`. The error often looks like this:
|
408
|
+
|
409
|
+
```term
|
410
|
+
$ heroku run rake -T
|
411
|
+
Running `bundle exec rake -T` attached to terminal... up, ps.3
|
412
|
+
rake aborted!
|
413
|
+
no such file to load -- rspec/core/rake_task
|
414
|
+
```
|
415
|
+
First, duplicate the problem locally by running `bundle install` without the development or test gem groups:
|
416
|
+
|
417
|
+
```term
|
418
|
+
$ bundle install --without development:test
|
419
|
+
…
|
420
|
+
$ bundle exec rake -T
|
421
|
+
rake aborted!
|
422
|
+
no such file to load -- rspec/core/rake_task
|
423
|
+
```
|
424
|
+
|
425
|
+
>note
|
426
|
+
>The `--without` option on `bundler` is persistent. To remove this option, run `bundle config --delete without`.
|
427
|
+
|
428
|
+
Fix the error by making these Rake tasks conditional during 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 it to Heroku.
|
445
|
+
|
446
|
+
## Next Steps
|
447
|
+
|
448
|
+
Congratulations on deploying a Rails 8 application! To continue exploring, review the following articles next:
|
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.
|
452
|
+
|
453
|
+
>note
|
454
|
+
>Remember to [delete your example app](https://devcenter.heroku.com/articles/heroku-cli-commands#heroku-apps-destroy), and [database](https://devcenter.heroku.com/articles/heroku-postgresql#removing-the-add-on) as soon as you're done with the tutorial, to control costs.
|
455
|
+
|
456
|
+
## Delete Your App and Add-on
|
457
|
+
|
458
|
+
Remove the app and database from your account. You're only charged for the resources you used.
|
459
|
+
|
460
|
+
>warning
|
461
|
+
>This action removes your add-on and any data saved in the database.
|
462
|
+
|
463
|
+
```term
|
464
|
+
$ heroku addons:destroy heroku-postgresql
|
465
|
+
```
|
466
|
+
|
467
|
+
>warning
|
468
|
+
>This action permanently deletes your application
|
469
|
+
|
470
|
+
```term
|
471
|
+
$ heroku apps:destroy
|
472
|
+
```
|
473
|
+
|
474
|
+
You can confirm that your add-on and app are gone with these commands:
|
475
|
+
|
476
|
+
```term
|
477
|
+
$ heroku addons --all
|
478
|
+
$ heroku apps --all
|
479
|
+
```
|
480
|
+
|
481
|
+
You're now ready to <a href= "https://devcenter.heroku.com/articles/preparing-a-codebase-for-heroku-deployment" target= "_blank">deploy your app</a>.
|
@@ -0,0 +1,13 @@
|
|
1
|
+
```
|
2
|
+
:::-- $ git config --get user.email || git config --global user.email "developer@example.com"
|
3
|
+
:::-- $ git config --get user.name || git config --global user.name "Developer name"
|
4
|
+
```
|
5
|
+
|
6
|
+
```
|
7
|
+
:::>> $ echo "hello world" >> lol.txt
|
8
|
+
:::>> $ git init .
|
9
|
+
:::>> $ git add .
|
10
|
+
:::>> $ git commit -m first
|
11
|
+
:::>> $ echo "so long and thanks for all the fish" >> lol.txt
|
12
|
+
:::>> $ git add . && git commit -m second
|
13
|
+
```
|