# How to setup GitHub Actions CI in a Rails 7 app

# What is GitHub Actions

For the uninitiated, GitHub Actions helps automate certain aspects of a software project's build, test and deployment aspects. If your project is already on Github, it makes sense to utilise what GitHub Actions has to offer.  
The focus of this write-up is the CI aspect of GitHub Actions.

# Why I did this

I set out to familiarise myself with how to use GitHub Actions for CI. I wanted to automate the running of the system tests in [Rumblr](https://github.com/Esseme/rumblr).  
I precisely wanted the CI to run every time I push to a new branch or the main branch and every time a new Pull Request is made.

# How I achieved it

To get the CI to build, new changes needed to be made and new files needed to be added.

A new file: `.env.test` had to be created. Below are the contents of the file;

```ruby
EMAIL_RECIPIENTS=test@test.com
FROM_EMAIL=sample@test.com
HONEYBADGER_API_KEY=honeybadger
```

A new file for the workflow needed to be added.  
Below are the contents of the actual CI file which is located via: `.github/workflows/config.yml`

```yaml
name: CI

 on: [push,pull_request]
 jobs:
   test:
     name: Tests
     runs-on: ubuntu-latest
     services:
       postgres:
         image: postgres:12.7
         env:
           POSTGRES_USER: rumblr
           POSTGRES_DB: rumblr_test
           POSTGRES_PASSWORD: password
         ports: ["5432:5432"]
         options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

     steps:
       - name: Checkout code
         uses: actions/checkout@v2

       - name: Setup Ruby and install gems
         uses: ruby/setup-ruby@v1
         with:
           bundler-cache: true

       - name: Install postgres client
         run: sudo apt-get install libpq-dev

       - name: Setup test database
         env:
           RAILS_ENV: test
           POSTGRES_HOST: localhost
           POSTGRES_USER: rumblr
           POSTGRES_PASSWORD: password
         run: |
           bin/rails db:setup

       - name: Run tests
         env:
           RAILS_ENV: test
           POSTGRES_HOST: localhost
           POSTGRES_USER: rumblr
           POSTGRES_PASSWORD: password
         run: |
           bundle exec rake assets:precompile
           bundle exec rspec
```

Some changes needed to be made to the `database.yml` file. The changes are below;

```yaml
test:
   <<: *default
   database: rumblr_test
   host: <%= ENV.fetch('POSTGRES_HOST',  'localhost') %>
   username: <%= ENV.fetch('POSTGRES_USER', nil) %>
   password: <%= ENV.fetch('POSTGRES_PASSWORD', nil) %>
   port: <%= ENV.fetch('POSTGRES_PORT', 5432) %>
```

These changes were done to the `test` portion of the database configuration.

# Challenges faced

Some challenges (as expected) were faced while attempting this task.  
Consider this [git change log](https://github.com/Esseme/rumblr/commit/150a355bff8c9e28809055594489683e0f983ec3) for the challenges that were faced.

### The `.env.test` file

This file had to be included and the suggestion came from a [colleague](https://twitter.com/purinkle).  
The official Github guides were only partly helpful to assist in setting this up.  
Otherwise, the build kept on failing and the error didn't make a lot of sense.

### The changes to the `test` section of the DB config

Consider the `config/database.yml` file in the [change log](https://github.com/Esseme/rumblr/commit/150a355bff8c9e28809055594489683e0f983ec3).  
All that had to be added as well for the build to happen successfully.

# References

The links below were helpful in achieving this task;

1. [Boring Rails Article](https://boringrails.com/articles/building-a-rails-ci-pipeline-with-github-actions/)
    
2. [Andy Croll Article](https://andycroll.com/ruby/github-actions-ci-for-rails-with-postgresql/)
    
3. [This Stack Overflow answer](https://stackoverflow.com/a/68258327/2257815)
    
4. [Medium Article](https://medium.com/@OwenTran/github-workflow-for-rails-ci-34209a53d19e)
    
5. [Github Tailwind CSS Issue](https://github.com/rails/tailwindcss-rails/issues/153#issuecomment-1191022279)
    

# Conclusion

This is included in the Rumblr project which can be found [here](https://github.com/Esseme/rumblr).
