Skip to content

Commit 1a43593

Browse files
Merge pull request circleci#1754 from circleci/update-rails
Catch up language guide for Rails app following demo changes
2 parents dfb7c39 + 9236812 commit 1a43593

File tree

1 file changed

+101
-30
lines changed

1 file changed

+101
-30
lines changed

jekyll/_cci2/language-ruby.md

Lines changed: 101 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -42,38 +42,69 @@ jobs:
4242
parallelism: 3
4343
working_directory: ~/circleci-demo-ruby-rails
4444
docker:
45-
- image: circleci/ruby:2.4.1-node
45+
- image: circleci/ruby:2.4-node
4646
environment:
47+
PGHOST: 127.0.0.1
48+
PGUSER: circleci-demo-ruby
4749
RAILS_ENV: test
48-
- image: circleci/postgres:9.4.12-alpine
50+
- image: circleci/postgres:9.5-alpine
51+
environment:
52+
POSTGRES_USER: circleci-demo-ruby
53+
POSTGRES_DB: rails_blog
54+
POSTGRES_PASSWORD: ""
4955
steps:
5056
- checkout
5157

5258
# Restore bundle cache
53-
- restore_cache:
54-
key: rails-demo-{{ checksum "Gemfile.lock" }}
59+
- type: cache-restore
60+
name: Restore bundle cache
61+
key: rails-demo-bundle-{{ checksum "Gemfile.lock" }}
5562

56-
# Bundle install dependencies
57-
- run: bundle install --path vendor/bundle
63+
- run:
64+
name: Bundle Install
65+
command: bundle install --path vendor/bundle
5866

5967
# Store bundle cache
60-
- save_cache:
61-
key: rails-demo-{{ checksum "Gemfile.lock" }}
62-
paths:
63-
- vendor/bundle
64-
65-
# Database setup
66-
- run: bundle exec rake db:create
67-
- run: bundle exec rake db:schema:load
68+
- type: cache-save
69+
name: Store bundle cache
70+
key: rails-demo-bundle-{{ checksum "Gemfile.lock" }}
71+
paths:
72+
- vendor/bundle
73+
74+
# Only necessary if app uses webpacker or yarn in some other way
75+
- type: cache-restore
76+
name: Restore yarn cache
77+
key: rails-demo-yarn-{{ checksum "yarn.lock" }}
78+
79+
- run:
80+
name: Yarn Install
81+
command: yarn install
82+
83+
# Store yarn / webpacker cache
84+
- type: cache-save
85+
name: Store yarn cache
86+
key: rails-demo-yarn-{{ checksum "yarn.lock" }}
87+
paths:
88+
- ~/.yarn-cache
89+
90+
- run:
91+
name: Wait for DB
92+
command: dockerize -wait tcp://localhost:5432 -timeout 1m
93+
94+
- run:
95+
name: Database setup
96+
command: bin/rails db:schema:load --trace
6897

6998
# Run rspec in parallel
70-
- run: |
99+
- type: shell
100+
command: |
71101
bundle exec rspec --profile 10 \
72102
--format RspecJunitFormatter \
73103
--out test_results/rspec.xml \
74104
--format progress \
75105
$(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)
76106
107+
77108
# Save test results for timing analysis
78109
- store_test_results:
79110
path: test_results
@@ -122,17 +153,25 @@ Directly beneath `working_directory`, we can specify container images under a `d
122153
version: 2
123154
# ...
124155
docker:
125-
- image: circleci/ruby:2.4.1-node
156+
- image: circleci/ruby:2.4-node
126157
environment:
158+
PGHOST: 127.0.0.1
159+
PGUSER: circleci-demo-ruby
127160
RAILS_ENV: test
128-
- image: circleci/postgres:9.4.12-alpine
161+
- image: circleci/postgres:9.5-alpine
162+
environment:
163+
POSTGRES_USER: circleci-demo-ruby
164+
POSTGRES_DB: rails_blog
165+
POSTGRES_PASSWORD: ""
129166
```
130167

131-
We use the [official Ruby images](https://hub.docker.com/_/ruby/) tagged to version `2.4.1` and with additional packages installed for NodeJS.
168+
We use the [official Ruby images](https://hub.docker.com/_/ruby/) tagged to use the latest patch-level version of `2.4` and with additional packages installed for NodeJS.
132169

133170
We've also specified the [official Postgres image](https://hub.docker.com/_/postgres/) for use as our database container.
134171

135-
Now we’ll add several `steps` within the `build` job.
172+
As well, we've added several environment variables for connecting our application container with the database for our testing purposes.
173+
174+
Now let's add several `steps` within the `build` job.
136175

137176
We start with `checkout` so we can operate on the codebase.
138177

@@ -151,29 +190,61 @@ steps:
151190
# ...
152191
153192
# Restore bundle cache
154-
- restore_cache:
155-
key: rails-demo-{{ checksum "Gemfile.lock" }}
193+
- type: cache-restore
194+
name: Restore bundle cache
195+
key: rails-demo-bundle-{{ checksum "Gemfile.lock" }}
156196
157-
# Bundle install dependencies
158-
- run: bundle install --path vendor/bundle
197+
- run:
198+
name: Bundle Install
199+
command: bundle install --path vendor/bundle
159200
160201
# Store bundle cache
161-
- save_cache:
162-
key: rails-demo-{{ checksum "Gemfile.lock" }}
163-
paths:
164-
- vendor/bundle
202+
- type: cache-save
203+
name: Store bundle cache
204+
key: rails-demo-bundle-{{ checksum "Gemfile.lock" }}
205+
paths:
206+
- vendor/bundle
165207
```
166208
{% endraw %}
167209

210+
If you're application is using Webpack or Yarn for JavaScript dependencies, you should also add this to your config.
211+
212+
{% raw %}
213+
```YAML
214+
steps:
215+
# ...
216+
217+
# Only necessary if app uses webpacker or yarn in some other way
218+
- type: cache-restore
219+
name: Restore yarn cache
220+
key: rails-demo-yarn-{{ checksum "yarn.lock" }}
221+
222+
- run:
223+
name: Yarn Install
224+
command: yarn install
225+
226+
# Store yarn / webpacker cache
227+
- type: cache-save
228+
name: Store yarn cache
229+
key: rails-demo-yarn-{{ checksum "yarn.lock" }}
230+
paths:
231+
- ~/.yarn-cache
232+
```
233+
168234
Now we can setup our test database we'll use during the build.
169235

170236
```YAML
171237
steps:
172238
# ...
173239
174240
# Database setup
175-
- run: bundle exec rake db:create
176-
- run: bundle exec rake db:schema:load
241+
- run:
242+
name: Wait for DB
243+
command: dockerize -wait tcp://localhost:5432 -timeout 1m
244+
245+
- run:
246+
name: Database setup
247+
command: bin/rails db:schema:load --trace
177248
```
178249

179250
Then `bundle exec rspec` which runs the actual tests in parallel.
@@ -194,7 +265,7 @@ steps:
194265
--out test_results/rspec.xml \
195266
--format progress \
196267
$(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)
197-
268+
198269
# Save test results for timing analysis
199270
- store_test_results:
200271
path: test_results

0 commit comments

Comments
 (0)