DEV Community

Cover image for Run Rubocop with GitHub Actions
Andrew Mason
Andrew Mason

Posted on • Updated on • Originally published at andrewm.codes

Run Rubocop with GitHub Actions

GitHub Actions are a great new tool that you have at your disposal if you are using GitHub. There is a lot you can do with them, but in this article we will focus on how to use actions to run Rubocop, a Ruby static code analyzer and code formatter, against a vanilla Rails app, with the help of the Rubocop Linter Action.

The action uses GitHub's Checks API to display the results of the action in the UI, to help you better visualize your failing lints.

What We Will Build

To demonstrate how to use the Rubocop Linter Action, we will scaffold a small Rails app and add the action. After we get the action working, we will add some configuration options to showcase some of the action's abilities.

The Code

For this demo, I am using Ruby 2.6.5 and Rails 6.0.1.

First, let's create a new Rails app:

rails new devto-rubocop-linter-action-demo
cd devto-rubocop-linter-action-demo/
Enter fullscreen mode Exit fullscreen mode

To run the action out of the box, all we need to do is add a workflow file:

mkdir -p .github/workflows
touch .github/workflows/rubocop.yml
Enter fullscreen mode Exit fullscreen mode

You should now have a file named rubocop.yml inside .github/workflows.

Now, lets configure our workflow to run the Rubocop Linter Action:

# .github/workflows/rubocop.yml

name: Rubocop

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Rubocop Linter Action
      uses: andrewmcodes/rubocop-linter-action@v3.0.0.rc2
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Enter fullscreen mode Exit fullscreen mode

This tells GitHub that our action name is "Rubocop", and we are going to run it on the latest ubuntu version available whenever there is a push to the repository.

For our steps, we will first check-out the repository using actions/checkout@v2, which is an action that checks-out your repository under $GITHUB_WORKSPACE, so your workflow can access it.

We will then run a step with the name of Rubocop Linter Action that uses the action with the same name. For this tutorial, we will be using version 3.0.0.rc2 of the action.

The last step is to pass in a generated GitHub token to the action under the GITHUB_TOKEN environment variable, which will be accessible inside of the action. You can read more about that here.

Now I am going to create a repository called devto-rubocop-linter-action-demo on GitHub. You can find it here. After the repository is created, we need to run a few commands to push our action to our master branch.

git add .
git commit -m "first commit"
git remote add origin https://github.com/andrewmcodes/devto-rubocop-linter-action-demo.git
git push -u origin master
Enter fullscreen mode Exit fullscreen mode

Now if we open our repository on GitHub, under the "Actions" tab, we should see this:

GitHub Action Tab

If we click on this action, and wait a few seconds, it should succeed. On the left side of the screen you should see our action name, "Rubocop", and two items underneath it, "Build" and "Rubocop Action".

If you click on "Build", it will show you the build logs for the action.

If you click on "Rubopcop Action", we should see our Rubocop results.

Rubocop Action Results

Pretty cool! We can now see all of the failures for the action on our fresh Rails app, using just the Rubocop Linter Action and Rubocop defaults. Notice that we didn't even need to add Rubocop to our Gemfile!

Advanced Configuration

The real power of the action is that you can configure it to work how you want. It also works even better with pull requests, which is what I will show you now.

First, let's create a new branch:

git checkout -b advanced-rubocop-action-config
Enter fullscreen mode Exit fullscreen mode

Next, create a Rubocop config file:

touch .rubocop.yml
Enter fullscreen mode Exit fullscreen mode

Inside of that file, let's add:

# .rubocop.yml

require:
  - rubocop-performance
  - rubocop-rails
Enter fullscreen mode Exit fullscreen mode

What this does is tell Rubocop that we want to use the Rubocop Performance and Rubocop Rails extensions. Notice that we have not added these gems to our Gemfile.

Now, lets create a config file for our action:

mkdir -p .github/config
touch .github/config/rubocop_linter_action.yml
Enter fullscreen mode Exit fullscreen mode

This will be the file that we use to configure the action. Let's add some config options to it:

# .github/config/rubocop_linter_action.yml

rubocop_extensions:
  - 'rubocop-rails'
  - 'rubocop-performance': '1.5.1'
Enter fullscreen mode Exit fullscreen mode

What this does is tell the action we want to use the Rubocop Rails and Rubocop Performance extensions. Specifically, we want to use the latest version of rubocop-rails, and version 1.5.1 of rubocop-performance.

Let's commit these and see what happens:

git add .
git commit -m "add rubocop and rubocop-linter-action config files"
git push --set-upstream origin advanced-rubocop-action-config
Enter fullscreen mode Exit fullscreen mode

If we go to our repo on GitHub, we should see a banner with our branch name and a button that says "Compare & pull request". If we click that, we should be taken to a pull request edit page. Click "Create Pull Request" button to open our new pull request.

Now that our pull request is open, click the "Checks" tab, and we should see that we have a few more offenses now that we have added our plugins:

Updated Config Rubocop Action Results

If we click on one of the checks, you should be taken to the location in the file where lint was recorded:

Inline Rubocop Action Results

In Summary

There are lots more changes we can add to finely tune Rubocop Linter Action to run the way we want. A few of my favorites are the ability to only run Rubocop against the diff and bundling our Gemfile instead of installing extensions from RubyGems.org directly.

If this has interested you, I would suggest checking out the action documentation and the GitHub repo.

Version 3.0.0.rc2 is in a pre-release phase, but I would love if you give it a try and let me know what issues you come across!

Happy Coding! 😃

Top comments (1)

Collapse
 
lperry65 profile image
Lee Perry

andrewmcodes/rubocop-linter-action is no longer supported and wont work with ruby 3.1