Rails guide – Adding CI – Part 5

rails-guide-–-adding-ci-–-part-5

Hey guys! How are you?

This article is a part of a series that will teach how to create a production ready rails application. Today we’re gonna talk about CI.

Table of content

  • What is CI
  • Tooling
  • Action creation

What is CI

CI, ou integração contínua, é uma metodologia que visa automatizar o processo de execução de testes e linting para cada alteração de código feita, garantindo assim mais qualidade e segurança no processo

CI, or continuous integration, is a methodology that automates linting and tests execution after each pull request ensuring quality and security in the process.

Tooling

The tool we’ll use to solve this problem is [Github actions[(https://github.com/features/actions). This tool allow us to automate the tests and linting execution after any commit on a pull request.

Action creation

To create the github action we gonna work on .github/workflows path, all files related to a action goes here.

First thing to do is create a file called linter.yml with the following content:

name: Linter

on: push

jobs:
  linting:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - uses: actions/setup-ruby@v1
      - name: Install dependencies
        run: bundle install
      - name: Run linter
        run: rubocop

This file will create a action called Linter that have on job called linting, this job will be executed in a ubuntu doker image that will perform the following steps:

  1. Pull the most recent code
  2. Install ruby on the image
  3. Install system dependencies
  4. Install gems
  5. Execute linter

Now that linter is configured we gonna create a file called tests.yml to configure tests action, here the file content:

name: Tests

on: push

jobs:
  specs:
    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres
        env:
          POSTGRES_USER: user
          POSTGRES_PASSWORD: 123456
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 5432:5432

    steps:
      - uses: actions/checkout@v1

      - uses: actions/setup-ruby@v1
        with:
          bundler-cache: true

      - name: Install dependencies
        run: sudo apt-get install libpq-dev

      - name: Install dependencies
        run: |
          sudo apt-get -yqq install libpq-dev
          gem install bundler
          bundle install --jobs 4 --retry 3

      - name: Database setup
        run: |
          bundle exec rake db:create
          bundle exec rake db:migrate
        env:
          RAILS_ENV: test
          POSTGRES_USER: user
          POSTGRES_PASSWORD: 123456

      - name: Run specs
        run: bundle exec rspec

This file will create a job called Tests that will create a job called specs, this job will be executed using a ubuntu docker image as base and perform the following steps:

  1. Setup postgres database
  2. Pull the most recent code
  3. Setup ruby
  4. Install dependencies
  5. Create database
  6. Run specs

If everything is ok you should look something like this whey you open a pull request on github:

Image description

You can see all code changes here

Total
13
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
introduction-to-ai-&-ml

Introduction to AI & ML

Next Post
managing-file-system-permissions

Managing File System Permissions

Related Posts