Use CircleCI version 2.1 at the top of your .circleci/config.yml file.
1
version: 2.1Add the orbs stanza below your version, invoking the orb:
1
2
orbs:
ruby: circleci/ruby@2.7.1Use ruby elements in your existing workflows and jobs.
For environments where Ruby is not pre-installed. (Recommended: It is faster and more deterministic to use a Docker image with Ruby pre-installed. Installing Ruby at run-time is not advised unless required.)
1
2
3
4
5
6
7
8
9
10
11
12
13
version: '2.1'
orbs:
ruby: circleci/ruby@x.y
jobs:
build:
docker:
- image: cimg/base:stable
steps:
- checkout
- ruby/install:
version: '2.7'
- run: echo "Ruby 2.7 has been installed"
workflows: null
Build and test a full Ruby Rails application with a Postgres database using RSpec. View the full sample application source: https://github.com/CircleCI-Public/circleci-demo-ruby-rails/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
version: '2.1'
orbs:
node: circleci/node@x.y
ruby: circleci/ruby@x.y
jobs:
build:
docker:
- image: cimg/ruby:2.7-node
steps:
- checkout
- ruby/install-deps
- node/install-packages:
cache-key: yarn.lock
pkg-manager: yarn
checking:
docker:
- image: cimg/ruby:2.7-node
steps:
- checkout
- ruby/install-deps
- ruby/rubocop-check:
format: progress
label: Inspecting with Rubocop
test:
docker:
- image: cimg/ruby:2.7-node
- environment:
POSTGRES_DB: rails_blog_test
POSTGRES_PASSWORD: ''
POSTGRES_USER: circleci-demo-ruby
image: circleci/postgres:9.5-alpine
environment:
BUNDLE_JOBS: '3'
BUNDLE_RETRY: '3'
PGHOST: 127.0.0.1
PGPASSWORD: ''
PGUSER: circleci-demo-ruby
RAILS_ENV: test
parallelism: 3
steps:
- checkout
- ruby/install-deps
- node/install-packages:
cache-key: yarn.lock
pkg-manager: yarn
- run:
command: dockerize -wait tcp://localhost:5432 -timeout 1m
name: Wait for DB
- run:
command: bundle exec rails db:schema:load --trace
name: Database setup
- ruby/rspec-test:
include: spec/**/*_spec.rb
workflows:
build_and_test:
jobs:
- build
- checking
- test:
requires:
- build
Install Ruby within a build. To be used in a Linux distro with Apt available.
| PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
|---|---|---|---|---|
openssl-path Directory where OpenSSL is intalled. This will overwrite the value of --with-openssl-dir in the rvm install commmand. Set this if you need to use an OpenSSL version different to 1.0.1 (rvm default). The version must be already installed.
| Directory where OpenSSL is intalled. This will overwrite the value of --with-openssl-dir in the rvm install commmand. Set this if you need to use an OpenSSL version different to 1.0.1 (rvm default). The version must be already installed.
| No | '' type: string | string |
version Ruby version. This can be a literal value (e.g, `2.7.5`). You can also pass in a string to be evaluated. For example, `${MY_RUBY_VERSION}` or `$(cat foo/bar/.ruby-version)`.
Required | Ruby version. This can be a literal value (e.g, `2.7.5`). You can also pass in a string to be evaluated. For example, `${MY_RUBY_VERSION}` or `$(cat foo/bar/.ruby-version)`.
| Yes | - type: string | string |
Install gems with Bundler.
| PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
|---|---|---|---|---|
app-dir Path to the directory containing your Gemfile file. Not needed if Gemfile lives in the root.
| Path to the directory containing your Gemfile file. Not needed if Gemfile lives in the root.
| No | . type: string | string |
bundler-version Configure which version of bundler to install and utilize. By default, it gets the bundler version from Gemfile.lock, but if it is not working use this to override.
| Configure which version of bundler to install and utilize. By default, it gets the bundler version from Gemfile.lock, but if it is not working use this to override.
| No | '' type: string | string |
clean-bundle Run `bundle clean --force` after `bundle install` to clean Bundler before saving dependencies to cache. By default, it is set to false.
| Run `bundle clean --force` after `bundle install` to clean Bundler before saving dependencies to cache. By default, it is set to false.
| No | false type: boolean | boolean |
gemfile Name of your Gemfile file. | Name of your Gemfile file. | No | Gemfile type: string | string |
include-arch-in-cache-key If true, this cache bucket will only apply to jobs running on the same architecture.
| If true, this cache bucket will only apply to jobs running on the same architecture.
| No | true type: boolean | boolean |
include-branch-in-cache-key If true, this cache bucket will only apply to jobs within the same branch.
| If true, this cache bucket will only apply to jobs within the same branch.
| No | true type: boolean | boolean |
key The cache key to use. The key is immutable. | The cache key to use. The key is immutable. | No | gems-v1 type: string | string |
no_output_timeout Specify a timeout for the bundle install command. By default, it is set to 10 minutes.
| Specify a timeout for the bundle install command. By default, it is set to 10 minutes.
| No | 10m type: string | string |
only-use-gemfile-checksum-cache-key When enabled only uses the cache key that contains the Gemile.lock checksum for restoring cache. | When enabled only uses the cache key that contains the Gemile.lock checksum for restoring cache. | No | false type: boolean | boolean |
override-cache-file Specify an alternative file to use in the cache key
| Specify an alternative file to use in the cache key
| No | '' type: string | string |
path Installation path. By default, it will run bundle with `--deployment` flag and installs gems to the vendor/bundle directory.
| Installation path. By default, it will run bundle with `--deployment` flag and installs gems to the vendor/bundle directory.
| No | ./vendor/bundle type: string | string |
pre-install-steps Steps that will be executed between installing bundler, and running bundle install
| Steps that will be executed between installing bundler, and running bundle install
| No | [] type: steps | steps |
with-cache Enable automatic caching of your gemfile dependencies for increased speed. | Enable automatic caching of your gemfile dependencies for increased speed. | No | true type: boolean | boolean |
Test with RSpec. You have to add `gem 'rspec_junit_formatter'` to your Gemfile. Enable parallelism on CircleCI for faster testing.
| PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
|---|---|---|---|---|
app-dir Path to the directory containing your Gemfile file. Not needed if Gemfile lives in the root.
| Path to the directory containing your Gemfile file. Not needed if Gemfile lives in the root.
| No | . type: string | string |
include Glob to define where your test files are kept within your repository. Should multiple globs be required, they must be passed in a comma separated string (e.g.: "{spec/**/*_spec.rb,spec2/**/*_spec.rb}").
| Glob to define where your test files are kept within your repository. Should multiple globs be required, they must be passed in a comma separated string (e.g.: "{spec/**/*_spec.rb,spec2/**/*_spec.rb}").
| No | spec/**/*_spec.rb type: string | string |
label Task label | Task label | No | RSpec Tests type: string | string |
no_output_timeout Allows you to specify the no_output_timeout for the rspec test. Defaults to 10m.
| Allows you to specify the no_output_timeout for the rspec test. Defaults to 10m.
| No | 10m type: string | string |
order Use the order parameter to tell RSpec how to order the files, groups, and examples. Available options can be found at: https://rspec.info/features/3-12/rspec-core/command-line/order
| Use the order parameter to tell RSpec how to order the files, groups, and examples. Available options can be found at: https://rspec.info/features/3-12/rspec-core/command-line/order
| No | '' type: string | string |
out-path Where to save the rspec.xml file. Will automatically be saved to test_results and artifacts on CircleCI. | Where to save the rspec.xml file. Will automatically be saved to test_results and artifacts on CircleCI. | No | /tmp/test-results/rspec type: string | string |
rerun-fail Enabling the option uses circleci tests run command and allows the "Rerun failed tests only" feature. This feature helps optimize test execution by re-running only the failed tests from previous test run data. More information can be found at: https://circleci.com/docs/rerun-failed-tests-only
| Enabling the option uses circleci tests run command and allows the "Rerun failed tests only" feature. This feature helps optimize test execution by re-running only the failed tests from previous test run data. More information can be found at: https://circleci.com/docs/rerun-failed-tests-only
| No | true type: boolean | boolean |
tag Use the tag parameter to tell RSpec to run only examples with (or without) a specified tag(s). Multiple tags should be separated by a space. Available options can be found at: https://rspec.info/features/3-12/rspec-core/command-line/tag
| Use the tag parameter to tell RSpec to run only examples with (or without) a specified tag(s). Multiple tags should be separated by a space. Available options can be found at: https://rspec.info/features/3-12/rspec-core/command-line/tag
| No | '' type: string | string |
Check the code by Rubocop. You have to add `gem 'rubocop'` to your Gemfile. Enable parallelism on CircleCI for faster checking.
| PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
|---|---|---|---|---|
app-dir Path to the directory containing your Gemfile file. Not needed if Gemfile lives in the root.
| Path to the directory containing your Gemfile file. Not needed if Gemfile lives in the root.
| No | . type: string | string |
check-path - | - | No | . type: string | string |
format Customize the formatter for rubocop https://docs.rubocop.org/rubocop/0.88/formatters.html | Customize the formatter for rubocop https://docs.rubocop.org/rubocop/0.88/formatters.html | No | progress type: string | string |
label Task label | Task label | No | Rubocop Checks type: string | string |
out-file-name Customize the name of the output file | Customize the name of the output file | No | check-results.xml type: string | string |
out-path Customize the directory of output file | Customize the directory of output file | No | /tmp/rubocop-results type: string | string |
parallel Use available CPUs to execute inspection in parallel.
| Use available CPUs to execute inspection in parallel.
| No | true type: boolean | boolean |
Select the version of Ruby to use. Uses CircleCI's highly cached convenience images built for CI. Any available tag from this list can be used: https://hub.docker.com/r/cimg/ruby/tags
| PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
|---|---|---|---|---|
resource_class The resources_class of the instance to run on. Defaults to Medium. | The resources_class of the instance to run on. Defaults to Medium. | No | medium type: string | string |
tag The `cimg/ruby` Docker image version tag. | The `cimg/ruby` Docker image version tag. | No | '2.7' type: string | string |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# This code is licensed from CircleCI to the user under the MIT license.
# See here for details: https://circleci.com/developer/orbs/licensing
version: 2.1
description: |
Easily cache and install your Ruby Gems automatically, run parallel Rspec tests, or just install Ruby.
display:
home_url: https://www.ruby-lang.org/
source_url: https://github.com/CircleCI-Public/ruby-orb
commands:
install:
description: Install Ruby within a build. To be used in a Linux distro with Apt available.
parameters:
openssl-path:
default: ""
description: |
Directory where OpenSSL is intalled. This will overwrite the value of --with-openssl-dir in the rvm install commmand. Set this if you need to use an OpenSSL version different to 1.0.1 (rvm default). The version must be already installed.
type: string
version:
description: |
Ruby version. This can be a literal value (e.g, `2.7.5`). You can also pass in a string to be evaluated. For example, `${MY_RUBY_VERSION}` or `$(cat foo/bar/.ruby-version)`.
type: string
steps:
- run:
command: "#!/usr/bin/env bash\n\ndetected_platform=\"$(uname -s | tr '[:upper:]' '[:lower:]')\"\nif [ \"$detected_platform\" = \"darwin\" ]; then\n brew install gpg2\nfi\n\n\n# Disable IPv6\nmkdir -p ~/.gnupg/\nfind ~/.gnupg -type d -exec chmod 700 {} \\;\necho \"disable-ipv6\" >> ~/.gnupg/dirmngr.conf\n\n# get keys to validate install https://rvm.io/rvm/security\n# https://stackoverflow.com/questions/69344989/gpg-no-keyserver-available\ndeclare -a keyservers=(\n \"keys.openpgp.org\"\n \"hkp://keyserver.ubuntu.com:80\"\n \"keyserver.ubuntu.com\"\n \"ha.pool.sks-keyservers.net\"\n \"hkp://ha.pool.sks-keyservers.net:80\"\n \"p80.pool.sks-keyservers.net\"\n \"hkp://p80.pool.sks-keyservers.net:80\"\n \"pgp.mit.edu\"\n \"hkp://pgp.mit.edu:80\"\n)\n\ngpg_key_downloaded=\"false\"\nfor server in \"${keyservers[@]}\"; do\n echo \"Fetching GPG keys from ${server}:\"\n \n if gpg --keyserver $server --keyserver-options timeout=10 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB \n then\n echo \"- GPG keys successfully added from server '${server}'\"\n gpg_key_downloaded=\"true\"\n break\n else\n echo \"- Network error: Unable to receive GPG keys from server '${server}'.\"\n fi\ndone\nif [ \"$gpg_key_downloaded\" = \"false\" ]; then\n echo \"Unable to receive GPG keys from any of the known GPG keyservers. Trying to import them from rvm.io.\"\n # https://rvm.io/rvm/security#alternatives\n \n if curl -sSL https://rvm.io/mpapis.asc | gpg --import - && curl -sSL https://rvm.io/pkuczynski.asc | gpg --import - \n then\n echo \"- GPG keys successfully imported directly from rvm.io server\"\n else\n echo \"- Could not get keys from rvm.io server either, FAILING\"\n exit 1\n fi\nfi\n\n# https://rvm.io/rvm/security#trust-our-keys\necho 409B6B1796C275462A1703113804BB82D39DC0E3:6: | gpg --import-ownertrust\necho 7D2BAF1CF37B13E2069D6956105BD0E739499BDB:6: | gpg --import-ownertrust\n\n## Update if RVM is installed and exit\nif [ -x \"$(command -v rvm -v)\" ]; then\n rvm get head\n exit 0\nfi\n\ncurl -sSL \"https://get.rvm.io\" | bash -s stable\n\n# check for machine image specific path\nif [ -d /opt/circleci/.rvm ]; then\n echo \"Setting PATH up for system install\"\n # this should be what needs to be added to that $BASH_ENV since this is what's in bash_profile - i dont know when $HOME is set\n echo 'export PATH=$PATH:/opt/circleci/.rvm/bin' >> $BASH_ENV\n echo \"source /opt/circleci/.rvm/scripts/rvm\" >> $BASH_ENV\n # this will source if anyone logs in noninteractively, nvm setup only adds nvm to the path, to get the rubygems later you need to source this again\n echo \"source /opt/circleci/.rvm/scripts/rvm\" >> ~/.bashrc\n echo \"export RVM_HOME=/opt/circleci/.rvm\" >> $BASH_ENV\nelse\n # Most circle builds run as a root user, in which case rvm gets installed in /usr/local/rvm instead of $HOME/.rvm\n RVM_HOME=$HOME/.rvm\n if [ -f \"$RVM_HOME/scripts/rvm\" ]; then\n echo \"Using $RVM_HOME\"\n else\n RVM_HOME=/usr/local/rvm\n echo \"Using $RVM_HOME\"\n fi\n echo \"export RVM_HOME=$RVM_HOME\" >> $BASH_ENV\n\n echo \"Setting PATH up for local install\"\n # this should be what needs to be added to that $BASH_ENV since this is what's in bash_profile - i dont know when $HOME is set\n echo 'export PATH=$PATH:$RVM_HOME/bin' >> $BASH_ENV\n echo \"source $RVM_HOME/scripts/rvm\" >> $BASH_ENV\n # this will source if anyone logs in noninteractively, nvm setup only adds nvm to the path, to get the rubygems later you need to source this again\n echo \"source $RVM_HOME/scripts/rvm\" >> ~/.bashrc\nfi\n\n# check if it seems like they're using rbenv already\nif command -v rbenv &> /dev/null && [ -f \".ruby-version\" ]\nthen\n echo -e \"\\e[91m\"\n cat \\<<'SUGGESTION'\n\n#######################################################################\n# WARNING\n#######################################################################\n\nWe've detected that you're running on a system that has the rbenv ruby\nversion manager already installed, and you have a .ruby-version file in\nthe current working directory.\n\nThe circleci/ruby orb (that's currently executing) uses RVM to install\nruby. Using more than one ruby version manager at once can, depending\non the configuration of your system, cause issues.\n\nTo install ruby with rbenv without using the circleci/ruby's \"install\"\ncommand, you can simply run a step that executes:\n\n rbenv install\n\nWhich will install the version of ruby that is specified in the\n.ruby-version file.\n\n#######################################################################\n\nSUGGESTION\n echo -e \"\\e[0m\"\nfi\n"
name: Install/Verify Ruby Version Manager
- run:
command: "#!/usr/bin/env bash\nPARAM_RUBY_VERSION=$(eval echo \"${PARAM_VERSION}\")\nRUBY_VERSION_MAJOR=$(echo \"$PARAM_RUBY_VERSION\" | cut -d. -f1)\nRUBY_VERSION_MINOR=$(echo \"$PARAM_RUBY_VERSION\" | cut -d. -f2)\ndetected_platform=\"$(uname -s | tr '[:upper:]' '[:lower:]')\"\n\nif command -v ruby >/dev/null 2>&1; then\n if ruby --version | grep -q \"$PARAM_RUBY_VERSION\"; then\n echo \"Ruby $PARAM_RUBY_VERSION is already installed, exiting.\"\n exit 0\n fi\nfi\n\n# When on MacOS, and versions minor or equal to 3.0.x. These are the versions depending on OpenSSL 1.1\nif [[ \"$RUBY_VERSION_MAJOR\" -le 2 || ( \"$RUBY_VERSION_MAJOR\" -eq 3 && \"$RUBY_VERSION_MINOR\" -eq 0 ) ]]; then\n if [[ \"$detected_platform\" = \"darwin\" ]]; then\n rbenv install $PARAM_RUBY_VERSION\n rbenv global $PARAM_RUBY_VERSION\n exit 0\n else\n if [ -n \"$PARAM_OPENSSL_PATH\" ]; then\n echo \"Using path $PARAM_OPENSSL_PATH for OpenSSL\"\n WITH_OPENSSL=\"--with-openssl-dir=$PARAM_OPENSSL_PATH\"\n elif ! openssl version | grep -q -E '1\\.[0-9]+\\.[0-9]+'; then \n echo \"Did not find supported OpenSSL version. Installing OpenSSL rvm package.\"\n rvm pkg install openssl\n # location of RVM is expected to be available at RVM_HOME env var\n WITH_OPENSSL=\"--with-openssl-dir=$RVM_HOME/usr\"\n fi\n fi\nelse\n rvm autolibs enable\nfi\n\nrvm get master\nrvm install \"$PARAM_RUBY_VERSION\" \"$WITH_OPENSSL\"\nrvm use \"$PARAM_RUBY_VERSION\"\n\nRUBY_PATH=\"$(rvm $PARAM_RUBY_VERSION 1> /dev/null 2> /dev/null && rvm env --path)\"\nprintf '%s\\n' \"source $RUBY_PATH\" >> \"$BASH_ENV\"\n"
environment:
PARAM_OPENSSL_PATH: << parameters.openssl-path >>
PARAM_VERSION: << parameters.version >>
name: Install Ruby v<< parameters.version >> via RVM
install-deps:
description: Install gems with Bundler.
parameters:
app-dir:
default: .
description: |
Path to the directory containing your Gemfile file. Not needed if Gemfile lives in the root.
type: string
bundler-version:
default: ""
description: |
Configure which version of bundler to install and utilize. By default, it gets the bundler version from Gemfile.lock, but if it is not working use this to override.
type: string
clean-bundle:
default: false
description: |
Run `bundle clean --force` after `bundle install` to clean Bundler before saving dependencies to cache. By default, it is set to false.
type: boolean
gemfile:
default: Gemfile
description: Name of your Gemfile file.
type: string
include-arch-in-cache-key:
default: true
description: |
If true, this cache bucket will only apply to jobs running on the same architecture.
type: boolean
include-branch-in-cache-key:
default: true
description: |
If true, this cache bucket will only apply to jobs within the same branch.
type: boolean
key:
default: gems-v1
description: The cache key to use. The key is immutable.
type: string
no_output_timeout:
default: 10m
description: |
Specify a timeout for the bundle install command. By default, it is set to 10 minutes.
type: string
only-use-gemfile-checksum-cache-key:
default: false
description: When enabled only uses the cache key that contains the Gemile.lock checksum for restoring cache.
type: boolean
override-cache-file:
default: ""
description: |
Specify an alternative file to use in the cache key
type: string
path:
default: ./vendor/bundle
description: |
Installation path. By default, it will run bundle with `--deployment` flag and installs gems to the vendor/bundle directory.
type: string
pre-install-steps:
default: []
description: |
Steps that will be executed between installing bundler, and running bundle install
type: steps
with-cache:
default: true
description: Enable automatic caching of your gemfile dependencies for increased speed.
type: boolean
steps:
- run:
command: |
#!/usr/bin/env bash
TARGET_DIR="/tmp"
if [ -n "$HOMEDRIVE" ]; then
TARGET_DIR="$HOMEDRIVE\\tmp"
fi
# Link corresponding lock file to a temporary file used by cache commands
if [ -n "$PARAM_OVERRIDE_LOCKFILE" ] && [ -f "$PARAM_OVERRIDE_LOCKFILE" ]; then
echo "Using $PARAM_OVERRIDE_LOCKFILE as lock file"
cp "$PARAM_OVERRIDE_LOCKFILE" $TARGET_DIR/ruby-project-lockfile
elif [ -n "$DEPENDENCIES_NEXT" ] && [ -f "Gemfile_next.lock" ]; then
echo "Using Gemfile_next.lock as lock file"
cp Gemfile_next.lock $TARGET_DIR/ruby-project-lockfile
elif [[ "$PARAM_GEMFILE" == *.rb ]]; then
GEMS_LOCKED="${PARAM_GEMFILE%.rb}.locked"
if [ -f "$GEMS_LOCKED" ]; then
echo "Using $GEMS_LOCKED as lock file"
cp "$GEMS_LOCKED" $TARGET_DIR/ruby-project-lockfile
else
echo "Could not find $GEMS_LOCKED file"
fi
elif [ -f "$PARAM_GEMFILE.lock" ]; then
echo "Using $PARAM_GEMFILE.lock as lock file"
cp "$PARAM_GEMFILE.lock" $TARGET_DIR/ruby-project-lockfile
else
echo "Unable to determine lock file for $PARAM_GEMFILE."
fi
environment:
PARAM_GEMFILE: << parameters.gemfile >>
PARAM_OVERRIDE_LOCKFILE: << parameters.override-cache-file >>
name: Determine lock file
working_directory: <<parameters.app-dir>>
- when:
condition: <<parameters.with-cache>>
steps:
- when:
condition: << parameters.only-use-gemfile-checksum-cache-key >>
steps:
- restore_cache:
keys:
- << parameters.key >>-<<#parameters.include-arch-in-cache-key>>{{ arch }}-<</parameters.include-arch-in-cache-key>><<#parameters.include-branch-in-cache-key>>{{ .Branch }}-<</parameters.include-branch-in-cache-key>>{{ checksum "/tmp/ruby-project-lockfile" }}
- unless:
condition: << parameters.only-use-gemfile-checksum-cache-key >>
steps:
- restore_cache:
keys:
- << parameters.key >>-<<#parameters.include-arch-in-cache-key>>{{ arch }}-<</parameters.include-arch-in-cache-key>><<#parameters.include-branch-in-cache-key>>{{ .Branch }}-<</parameters.include-branch-in-cache-key>>{{ checksum "/tmp/ruby-project-lockfile" }}
- << parameters.key >>-<<#parameters.include-arch-in-cache-key>>{{ arch }}-<</parameters.include-arch-in-cache-key>><<#parameters.include-branch-in-cache-key>>{{ .Branch }}-<</parameters.include-branch-in-cache-key>>
- run:
command: |
#!/usr/bin/env bash
TARGET_DIR="/tmp"
if [ -n "$HOMEDRIVE" ]; then
TARGET_DIR="$HOMEDRIVE\\tmp"
fi
if test -f "$TARGET_DIR/ruby-project-lockfile"; then
APP_BUNDLER_VERSION=$(cat "$TARGET_DIR/ruby-project-lockfile" | tail -1 | tr -d " ")
if [ -z "$APP_BUNDLER_VERSION" ]; then
echo "Could not find bundler version from lockfile. Please use bundler-version parameter"
else
echo "Lock file detected bundler version $APP_BUNDLER_VERSION"
fi
fi
if [ -n "$PARAM_BUNDLER_VERSION" ]; then
echo "Found bundler-version parameter to override"
APP_BUNDLER_VERSION="$PARAM_BUNDLER_VERSION"
fi
if ! bundle version | grep -q $APP_BUNDLER_VERSION; then
echo "Installing bundler $APP_BUNDLER_VERSION"
gem install bundler:$APP_BUNDLER_VERSION
else
echo "bundler $APP_BUNDLER_VERSION is already installed."
fi
environment:
PARAM_BUNDLER_VERSION: << parameters.bundler-version >>
PARAM_GEMFILE: << parameters.gemfile >>
name: Install Bundler
working_directory: <<parameters.app-dir>>
- steps: << parameters.pre-install-steps >>
- run:
command: |
#!/usr/bin/env bash
GEMFILE_ABS_PATH="$(cd "$(dirname "$PARAM_GEMFILE")" && pwd -P)/$(basename "$PARAM_GEMFILE")"
echo "$(cd "$(dirname LICENSE)" && pwd -P)/$(basename LICENSE)"
if bundle config set > /dev/null 2>&1; then
if [ "$PARAM_PATH" == "./vendor/bundle" ]; then
bundle config deployment 'true'
fi
bundle config gemfile "$GEMFILE_ABS_PATH"
bundle config path "$PARAM_PATH"
if [ -d /opt/circleci/.rvm ]; then
RVM_HOME=/opt/circleci/.rvm
else
# Most circle builds run as a root user, in which case rvm gets installed in /usr/local/rvm instead of $HOME/.rvm
RVM_HOME=$HOME/.rvm
if [ ! -f "$RVM_HOME/scripts/rvm" ]; then
RVM_HOME=/usr/local/rvm
fi
fi
if [ -d "$RVM_HOME/usr/ssl" ]; then
echo "Detected rvm ssl version. Configuring bundle package with openssl dir $RVM_HOME/usr."
bundle config build.openssl --with-openssl-dir="$RVM_HOME/usr"
fi
else
if [ "$PARAM_PATH" == "./vendor/bundle" ]; then
bundle config set deployment 'true'
fi
bundle config set gemfile "$GEMFILE_ABS_PATH"
bundle config set path "$PARAM_PATH"
if [ -d "$RVM_HOME/usr/ssl" ]; then
echo "Detected rvm ssl version. Configuring bundle package with openssl dir $RVM_HOME/usr."
bundle config set build.openssl --with-openssl-dir="$RVM_HOME/usr"
fi
fi
if [ "$PARAM_CLEAN_BUNDLE" = true ]; then
bundle check || (bundle install && bundle clean --force)
else
bundle check || bundle install
fi
environment:
PARAM_CLEAN_BUNDLE: << parameters.clean-bundle >>
PARAM_GEMFILE: << parameters.gemfile >>
PARAM_PATH: << parameters.path >>
name: Bundle Install <<^parameters.with-cache>>(No Cache)<</parameters.with-cache>>
no_output_timeout: << parameters.no_output_timeout >>
working_directory: <<parameters.app-dir>>
- when:
condition: << parameters.with-cache >>
steps:
- save_cache:
key: << parameters.key >>-<<#parameters.include-arch-in-cache-key>>{{ arch }}-<</parameters.include-arch-in-cache-key>><<#parameters.include-branch-in-cache-key>>{{ .Branch }}-<</parameters.include-branch-in-cache-key>>{{ checksum "/tmp/ruby-project-lockfile" }}
paths:
- <<parameters.app-dir>>/<< parameters.path >>
rspec-test:
description: Test with RSpec. You have to add `gem 'rspec_junit_formatter'` to your Gemfile. Enable parallelism on CircleCI for faster testing.
parameters:
app-dir:
default: .
description: |
Path to the directory containing your Gemfile file. Not needed if Gemfile lives in the root.
type: string
include:
default: spec/**/*_spec.rb
description: |
Glob to define where your test files are kept within your repository. Should multiple globs be required, they must be passed in a comma separated string (e.g.: "{spec/**/*_spec.rb,spec2/**/*_spec.rb}").
type: string
label:
default: RSpec Tests
description: Task label
type: string
no_output_timeout:
default: 10m
description: |
Allows you to specify the no_output_timeout for the rspec test. Defaults to 10m.
type: string
order:
default: ""
description: |
Use the order parameter to tell RSpec how to order the files, groups, and examples. Available options can be found at: https://rspec.info/features/3-12/rspec-core/command-line/order
type: string
out-path:
default: /tmp/test-results/rspec
description: Where to save the rspec.xml file. Will automatically be saved to test_results and artifacts on CircleCI.
type: string
rerun-fail:
default: true
description: |
Enabling the option uses circleci tests run command and allows the "Rerun failed tests only" feature. This feature helps optimize test execution by re-running only the failed tests from previous test run data. More information can be found at: https://circleci.com/docs/rerun-failed-tests-only
type: boolean
tag:
default: ""
description: |
Use the tag parameter to tell RSpec to run only examples with (or without) a specified tag(s). Multiple tags should be separated by a space. Available options can be found at: https://rspec.info/features/3-12/rspec-core/command-line/tag
type: string
steps:
- run:
command: |
#!/usr/bin/env bash
if [ "$CIRCLE_NODE_TOTAL" -eq 1 ]; then
printf '%s\n' "Your job parallelism is set to 1."
printf '%s\n' "The split test by timings requires at least 2 nodes to generate historical timing data."
printf '%s\n' "Consider increasing your job parallelism to 2 or more."
printf '%s\n' "See https://circleci.com/docs/2.0/parallelism-faster-jobs/#using-the-circleci-cli-to-split-tests for more information."
fi
# Disable bash glob expansion
# Without this, the glob parameter will be expanded before the split command is run
set -o noglob
if ! mkdir -p "$PARAM_OUT_PATH"; then
printf '%s\n' "Failed to create output directory: \"$PARAM_OUT_PATH\""
exit 1
fi
# store it as an array in the globs variable
read -ra globs \<<< "$PARAM_INCLUDE"
prepare_split_files() {
# Backup IFS
readonly old_ifs="$IFS"
# Split globs per comma and run the CLI split command
IFS=","
split_files=$(circleci tests glob "${globs[@]}" | circleci tests split --split-by=timings)
# Convert list of test files to array
# This is necessary because the split command returns a list of files separated by newline
while IFS= read -r line; do test_files+=("$line"); done \<<< "$split_files"
# Rollback IFS
IFS="$old_ifs"
}
args=()
if [ -n "$PARAM_ORDER" ]; then
args+=(--order "$PARAM_ORDER")
fi
if [ -n "$PARAM_TAG" ]; then
IFS=' ' read -ra tags \<<< "$PARAM_TAG"
for tag in "${tags[@]}"; do
args+=(--tag "$tag")
done
fi
# Parse array of test files to string separated by single space and run tests
# Leaving set -x here because it's useful for debugging what files are being tested
set -x
if [ "$PARAM_RERUN_FAIL" = 1 ]; then
circleci tests glob "${globs[@]}" | circleci tests run --command "xargs -t bundle exec rspec --seed \"$RANDOM\" --profile 10 --format RspecJunitFormatter --out \"$PARAM_OUT_PATH\"/results.xml --format progress ${args[*]}" --verbose --split-by=timings
else
prepare_split_files
bundle exec rspec --seed "$RANDOM" "${test_files[@]}" --profile 10 --format RspecJunitFormatter --out "$PARAM_OUT_PATH"/results.xml --format progress "${args[@]}"
fi
set +x
environment:
PARAM_INCLUDE: <<parameters.include>>
PARAM_ORDER: <<parameters.order>>
PARAM_OUT_PATH: <<parameters.out-path>>
PARAM_RERUN_FAIL: <<parameters.rerun-fail>>
PARAM_TAG: <<parameters.tag>>
name: <<parameters.label>>
no_output_timeout: <<parameters.no_output_timeout>>
working_directory: <<parameters.app-dir>>
- store_test_results:
path: <<parameters.out-path>>
- store_artifacts:
destination: test-results
path: <<parameters.out-path>>
rubocop-check:
description: Check the code by Rubocop. You have to add `gem 'rubocop'` to your Gemfile. Enable parallelism on CircleCI for faster checking.
parameters:
app-dir:
default: .
description: |
Path to the directory containing your Gemfile file. Not needed if Gemfile lives in the root.
type: string
check-path:
default: .
type: string
format:
default: progress
description: Customize the formatter for rubocop https://docs.rubocop.org/rubocop/0.88/formatters.html
type: string
label:
default: Rubocop Checks
description: Task label
type: string
out-file-name:
default: check-results.xml
description: Customize the name of the output file
type: string
out-path:
default: /tmp/rubocop-results
description: Customize the directory of output file
type: string
parallel:
default: true
description: |
Use available CPUs to execute inspection in parallel.
type: boolean
steps:
- run:
command: |
#!/usr/bin/env bash
mkdir -p "$PARAM_OUT_PATH"
RUBOCOP_VERSION=$(bundle exec rubocop -v)
RUBOCOP_VERSION_MAJOR=$(echo "$RUBOCOP_VERSION" | cut -d. -f1)
RUBOCOP_VERSION_MINOR=$(echo "$RUBOCOP_VERSION" | cut -d. -f2)
if [ "$PARAM_PARALLEL" -eq 1 ]; then
bundle exec rubocop "$PARAM_CHECK_PATH" \
--format "$PARAM_FORMAT" \
--parallel \
--out $"$PARAM_OUT_PATH"/"$PARAM_FILE_OUT"
else
if [ "$RUBOCOP_VERSION_MAJOR" -gt 1 ] || { [ "$RUBOCOP_VERSION_MAJOR" -eq 1 ] && [ "$RUBOCOP_VERSION_MINOR" -ge 13 ]; }; then
bundle exec rubocop "$PARAM_CHECK_PATH" \
--format "$PARAM_FORMAT" \
--no-parallel \
--out $"$PARAM_OUT_PATH"/"$PARAM_FILE_OUT"
else
bundle exec rubocop "$PARAM_CHECK_PATH" \
--format "$PARAM_FORMAT" \
--out $"$PARAM_OUT_PATH"/"$PARAM_FILE_OUT"
fi
fi
environment:
PARAM_CHECK_PATH: <<parameters.check-path>>
PARAM_FILE_OUT: <<parameters.out-file-name>>
PARAM_FORMAT: <<parameters.format>>
PARAM_OUT_PATH: <<parameters.out-path>>
PARAM_PARALLEL: <<parameters.parallel>>
name: <<parameters.label>>
working_directory: <<parameters.app-dir>>
executors:
default:
description: |
Select the version of Ruby to use. Uses CircleCI's highly cached convenience images built for CI.
Any available tag from this list can be used: https://hub.docker.com/r/cimg/ruby/tags
docker:
- image: cimg/ruby:<< parameters.tag >>
parameters:
resource_class:
default: medium
description: The resources_class of the instance to run on. Defaults to Medium.
type: string
tag:
default: "2.7"
description: The `cimg/ruby` Docker image version tag.
type: string
resource_class: << parameters.resource_class >>
examples:
install_ruby:
description: |
For environments where Ruby is not pre-installed. (Recommended: It is faster and more deterministic to use a Docker image with Ruby pre-installed. Installing Ruby at run-time is not advised unless required.)
usage:
version: "2.1"
orbs:
ruby: circleci/ruby@x.y
jobs:
build:
docker:
- image: cimg/base:stable
steps:
- checkout
- ruby/install:
version: "2.7"
- run: echo "Ruby 2.7 has been installed"
workflows: null
ruby_rails_sample_app:
description: |
Build and test a full Ruby Rails application with a Postgres database using RSpec.
View the full sample application source: https://github.com/CircleCI-Public/circleci-demo-ruby-rails/
usage:
version: "2.1"
orbs:
node: circleci/node@x.y
ruby: circleci/ruby@x.y
jobs:
build:
docker:
- image: cimg/ruby:2.7-node
steps:
- checkout
- ruby/install-deps
- node/install-packages:
cache-key: yarn.lock
pkg-manager: yarn
checking:
docker:
- image: cimg/ruby:2.7-node
steps:
- checkout
- ruby/install-deps
- ruby/rubocop-check:
format: progress
label: Inspecting with Rubocop
test:
docker:
- image: cimg/ruby:2.7-node
- environment:
POSTGRES_DB: rails_blog_test
POSTGRES_PASSWORD: ""
POSTGRES_USER: circleci-demo-ruby
image: circleci/postgres:9.5-alpine
environment:
BUNDLE_JOBS: "3"
BUNDLE_RETRY: "3"
PGHOST: 127.0.0.1
PGPASSWORD: ""
PGUSER: circleci-demo-ruby
RAILS_ENV: test
parallelism: 3
steps:
- checkout
- ruby/install-deps
- node/install-packages:
cache-key: yarn.lock
pkg-manager: yarn
- run:
command: dockerize -wait tcp://localhost:5432 -timeout 1m
name: Wait for DB
- run:
command: bundle exec rails db:schema:load --trace
name: Database setup
- ruby/rspec-test:
include: spec/**/*_spec.rb
workflows:
build_and_test:
jobs:
- build
- checking
- test:
requires:
- build