GitHub Actions: CI / CD Tools - Applying for Ruby / Rspec Project
Github released the beta version of "actions" last year for creating a CI/CD pipeline for the projects. This feature enables us to create a pipeline easily with the web interface of the Github. I want to run the tests for one of my public project in the Github before merging branches to the master. For testsozluk aka "test dictionary", which contains terms frequently used in the software industry with the meaning and usage of them in Turkish. For the project, tests are critical because JSON files in the Github project are the data source of the application so if any corruption in the data may break the android and the ios application. The tests in the project are written in Ruby by using Rspec to assert them so I used the Github actions to verify before merging code. In this post, I want to explain how this can be done.
Github's actions are very easy to apply. You can add it just go to the Actions tab in your project and then select one of the predefined options on the list. For Ruby projects, there is one for Ruby project and one for Ruby Gems project, or you can skip these predefined workflows and create your own workflow with create yourself option. Ruby Gems project is also including deployment part so you can easily push your gem to RubyGems with the credentials easily no need to push it in your local repository. Let's look at the Ruby projects, see the image below:
When you get the predefined Ruby workflow, you can add some tasks related to your pipeline and customize it. Basically, the workflow that I want to apply is to create a virtual Linux machine and install Ruby with the version of 2.6 and install requirements with the bundle. This workflow will run on push, but you can also set it on pull_request, or on schedule by defining a schedule with Cron syntax. It also has the webhook to trigger the workflow. I selected ubuntu-latest as a virtual machine but there are also other options as following:
For the project, tests are present in the `src/test` folder since I created the tests with RSpec it has its own structure, so I need to run tests in the test folder. Therefore running tests should have two steps; first I need to go to the test folder and then run the RSpec specs/test_spec.rb command to run them. The run command in the workflow supports multiple commands with adding pipe |. Let's look at the workflow as below:
Basically, I have created three tests to check first if the file is well-formatted JSON, second every term has term and meaning fields and third there is duplicated term. If one of the tests fails, it doesn't allow us to merge it. See the tests:
To able to stop merging to master when the tests are failing, you should add a rule in the Setting tab of the project and select the Branch option in the right list and add a rule that you should enable "Require status checks to pass before merging" and check the test which is a given name in your workflow yaml file. With this setup, merging action is now dependent on the result of the tests.