A tool to check your Ruby code for code smells in CI/CD pipelines using reek.
This tool has been written and tested using GitHub Actions but it should work out of the box with a lot of other CI/CD tools.
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
- name: Set up Ruby 3.0
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.0
- name: Run Reek
run: bash <(curl -s https://raw.githubusercontent.com/CICDToolbox/reek/master/pipeline.sh)
The following environment variables can be set in order to customise the script.
Name | Purpose | Default Value |
---|---|---|
EXCLUDE_FILES | A comma separated list of files to exclude from being scanned. You can also use regex to do pattern matching. |
Unset |
REPORT_ONLY | Generate the report but do not fail the build even if an error occurred. | False |
SHOW_ERRORS | Show the actual errors instead of just which files had errors. | True |
SHOW_SKIPPED | Show which files are being skipped. | False |
You can use any combination of the above settings.
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
- name: Set up Ruby 3.0
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.0
- name: Run Reek
env:
REPORT_ONLY: true
SHOW_ERRORS: true
run: bash <(curl -s https://raw.githubusercontent.com/CICDToolbox/reek/master/pipeline.sh)
This is an example of the output report generated by this tool, this is the actual output from the tool running against itself.
-------------------------------------------------------------------------- Stage 1 - Parameters --
No parameters given
--------------------------------------------------------------- Stage 2 - Install Prerequisites --
[ OK ] gem install --quiet reek
------------------------------------------------------------------- Stage 3 - Run reek (v6.0.6) --
[ OK ] tests/test-no-extension
[ OK ] tests/test.rb
------------------------------------------------------------------------------ Stage 4 - Report --
Total: 2, OK: 2, Failed: 0, Skipped: 0
---------------------------------------------------------------------------- Stage 5 - Complete --
Target files are identified using the following code:
file -b "${filename}" | grep -qE '(Ruby|ruby) script'
AND
[[ ${filename} =~ \.rb$ ]]
We have had to implement one work around due to how reek identifies ruby files. In short by default it will not scan any file that doesn't have a .rb suffix and it scans all the files it can find. We wanted this scanner to work on a file by file basis, so we have had to create a simple workaround.
reek --stdin-filename "${filename}" < "${filename}"
This allows us to pass in any file we identify and convince reek to honour the filename while accepting the file contents via STDIN.
If your ruby code fails to compile then reek will error but set the exit value to 0 causing the scanning to assume success. We have add an additional pre-test of ruby -c
which tries to compile the ruby code first before moving onto the linting phase to cope with this.