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.
For this demo, I am using Ruby
First, let’s create a new Rails app:
rails new devto-rubocop-linter-action-demo cd devto-rubocop-linter-action-demo/
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
You should now have a file named
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: email@example.com env: GITHUB_TOKEN: $
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
, which is an action that checks-out your repository under
, 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
of the action.
The last step is to pass in a generated GitHub token to the action under the
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
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
Now if we open our repository on GitHub, under the “Actions” tab, we should see this:
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.
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!
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
Next, create a Rubocop config file:
Inside of that file, let’s add:
# .rubocop.yml require: - rubocop-performance - rubocop-rails
Now, lets create a config file for our action:
mkdir -p .github/config touch .github/config/rubocop_linter_action.yml
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'
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
, and version 1.5.1 of
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
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:
If we click on one of the checks, you should be taken to the location in the file where lint was recorded:
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.
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! 😃