Skip to content

Commit

Permalink
Switch to using GitHub actions for CI (#351)
Browse files Browse the repository at this point in the history
https://blog.travis-ci.com/2020-11-02-travis-ci-new-billing
https://www.jeffgeerling.com/blog/2020/travis-cis-new-pricing-plan-threw-wrench-my-open-source-works

On Nov 2 Travis-CI introduced a new pricing model for their free tier offering. Those on this tier would now be limited to 1000 build minutes a month. This would start to be rolled out across accounts imminently. Defra's seems to have been updated November 9 because by the end of the day all builds stopped. When you went to Travis-CI a message was shown stating

> Builds have been temporarily disabled for private and public repositories due to a negative credit balance. Please go to the Plan page to replenish your credit balance.

It's going to take some time to decide and agree whether we will move to a paid-for option, or choose to start rolling our own. We don't like the idea of having no automated CI during this unknown period. A number of people caught up in this change have suggested [GitHub Actions](https://docs.github.com/en/free-pro-team@latest/actions) as an alternative.

So this change moves the project from Travis to GitHub to handle our CI

Because of the dependence on selenium based tests we also had to make a number of other changes

** Update chrome headless capybara driver code

We spent ages trying to get the selenium based tests to run in GitHub Actions. All our initial efforts were focused on thinking there was something we needed to do in the environment. Start xvfb, set special chromedriver settings, grab the latest chromedriver etc. This was even though lots of posts noted that this was no longer needed as GitHub's `ubuntu-latest` build comes with tools like these pre-installed.

We then started looking at how the driver was being registered. It turns out that a recent update to the Capybara gem required us to update how we were registering the driver. Switching to the pre-registered `:selenium_chrome_headless` got us from no passing selenium tests to all but 4.

So we then reviewed the [source code](https://github.com/teamcapybara/capybara/blob/master/lib/capybara/registrations/drivers.rb#L27) for what [team capybara](https://github.com/teamcapybara) were doing. By combining that with existing args we were using that again lots of posts advised, we finally got it working!

** Remove mini-test reporters

Whilst the progress bar format is nice, its unnecessary. Plus it does not work well with the GitHub action.

** Silence puma for Capybara tests

Whenever we run `bundle exec rails test` at some point we see this in the test output

```
Capybara starting Puma...
* Version 5.0.4 , codename: Spoony Bard
* Min threads: 0, max threads: 4
/home/vagrant/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/puma-5.0.4/lib/puma/server.rb:94: warning: deprecated Object#=~ is called on Proc; it always returns nil
* Listening on http://127.0.0.1:37836
```

It seems to be triggered by hitting a test that requires javascript. That and the solution I actually found on an [RSpec issue](rspec/rspec-rails#1897).

We still get the following error appearing

```
/home/vagrant/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/puma-5.0.4/lib/puma/server.rb:94: warning: deprecated Object#=~ is called on Proc; it always returns nil
```

But we are hoping this will be fixed in the next version of Puma.
  • Loading branch information
Cruikshanks authored Nov 12, 2020
1 parent b8310ef commit 44055c4
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 99 deletions.
74 changes: 74 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: CI

on: push

jobs:
build:
# You must use a Linux environment when using service containers or container jobs
runs-on: ubuntu-latest
env:
DEVISE_MAILER_SENDER: [email protected]
POSTGRES_HOST: localhost
POSTGRES_PASSWORD: pinafore
POSTGRES_PORT: 5432
POSTGRES_USERNAME: postgres
# Though we don't have redis running, we keep the env var so the app does not complain about a missing env var
REDIS_URL: "redis://localhost:6379"

# Service containers to run with `runner-job`
services:
# Label used to access the service container
postgres:
# Docker Hub image
image: postgres:9.5-alpine
# Provide the password for postgres
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: pinafore
POSTGRES_DB: sroc-tcm-admin_test
# Maps tcp port 5432 on service container to the host
ports:
- 5432:5432
# Set health checks to wait until postgres has started. You must have this so the runner knows to wait till
# postgres is up and running before proceeding
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

steps:
# Downloads a copy of the code in your repository before running CI tests
- name: Checkout repository
uses: actions/checkout@v2
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of sonarcloud analysis

# We don't have to specify the ruby version, or grab it from .ruby-verion. This action supports reading the
# version from .ruby-verion itself
- name: Install Ruby
uses: ruby/setup-ruby@v1
with:
bundler-cache: true # runs 'bundle install' and caches installed gems automatically

- name: Database migrations
run: |
RAILS_ENV=test bundle exec rake db:migrate
# Run linting first. No point running the tests if there is a linting issue
- name: Run lint check
run: |
bundle exec rubocop --format progress --format json --out rubocop-result.json
# This includes an extra run step. The sonarcloud analysis will be run in a docker container with the current
# folder mounted as `/github/workspace`. The problem is when the .resultset.json file is generated it will
# reference the code in the current folder. So to enable sonarcloud to matchup code coverage with the files we use
# sed to update the references in .resultset.json
# https://community.sonarsource.com/t/code-coverage-doesnt-work-with-github-action/16747/6
- name: Run unit tests
run: |
bundle exec rails test
bundle exec rspec
sed -i 's/\/home\/runner\/work\/sroc-tcm-admin\/sroc-tcm-admin\//\/github\/workspace\//g' coverage/.resultset.json
- name: Analyze with SonarCloud
uses: sonarsource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This is provided automatically by GitHub
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} # This needs to be set in your repo; settings -> secrets
70 changes: 0 additions & 70 deletions .travis.yml

This file was deleted.

1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ group :test do
gem "capybara"
gem "capybara-selenium"
gem "factory_bot_rails", "~> 4.0"
gem "minitest-reporters"
gem "mocha"
gem "rails-controller-testing"
gem "selenium-webdriver"
Expand Down
7 changes: 0 additions & 7 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ GEM
airbrake (5.8.1)
airbrake-ruby (~> 1.8)
airbrake-ruby (1.8.0)
ansi (1.5.0)
ast (2.4.1)
autoprefixer-rails (10.0.1.2)
execjs
Expand Down Expand Up @@ -175,11 +174,6 @@ GEM
mini_mime (1.0.2)
mini_portile2 (2.4.0)
minitest (5.14.2)
minitest-reporters (1.4.2)
ansi
builder
minitest (>= 5.0)
ruby-progressbar
mocha (1.11.2)
mono_logger (1.1.0)
multi_json (1.15.0)
Expand Down Expand Up @@ -401,7 +395,6 @@ DEPENDENCIES
kaminari
listen (>= 3.0.5, < 3.2)
memory_profiler
minitest-reporters
mocha
oauth2
passenger (~> 5.1)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Strategic Review of Charges - Tactical Charging Module

[![Build Status](https://travis-ci.com/DEFRA/sroc-tcm-admin.svg?branch=master)](https://travis-ci.com/DEFRA/sroc-tcm-admin)
![Build Status](https://github.com/DEFRA/sroc-tcm-admin/workflows/CI/badge.svg?branch=main)
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=DEFRA_sroc-tcm-admin&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=DEFRA_sroc-tcm-admin)
[![Technical Debt](https://sonarcloud.io/api/project_badges/measure?project=DEFRA_sroc-tcm-admin&metric=sqale_index)](https://sonarcloud.io/dashboard?id=DEFRA_sroc-tcm-admin)
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=DEFRA_sroc-tcm-admin&metric=coverage)](https://sonarcloud.io/dashboard?id=DEFRA_sroc-tcm-admin)
Expand Down
4 changes: 0 additions & 4 deletions config/database.travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ sonar.projectName=sroc-tcm-admin

# This will add the same links in the SonarCloud UI
sonar.links.homepage=https://github.com/DEFRA/sroc-tcm-admin
sonar.links.ci=https://travis-ci.com/DEFRA/sroc-tcm-admin
sonar.links.ci=https://github.com/DEFRA/sroc-tcm-admin/actions
sonar.links.scm=https://github.com/DEFRA/sroc-tcm-admin
sonar.links.issue=https://github.com/DEFRA/sroc-service-team

Expand Down
28 changes: 13 additions & 15 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,35 @@
require "capybara/minitest"
require "capybara/minitest/spec"

require "minitest/reporters"
Minitest::Reporters.use!

require "selenium/webdriver"

require "mocha/minitest"

Dir[Rails.root.join("test/support/**/*.rb")].sort.each { |f| require f }

# remove http auth which is only for heroku deployment
ENV["HEROKU"] = nil

Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome)
end

Capybara.register_driver :headless_chrome do |app|
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
chromeOptions: { args: %w[headless disable-gpu no-sandbox disable-dev-shm-usage window-size=1600,1000] }
)
service = Selenium::WebDriver::Service.chrome(args: { verbose: true, log_path: "tmp/chromedriver.log" })
Capybara::Selenium::Driver.new(app,
browser: :chrome,
desired_capabilities: capabilities,
service: service)
Capybara::Selenium::Driver.load_selenium
browser_options = ::Selenium::WebDriver::Chrome::Options.new.tap do |opts|
opts.args << "--headless"
opts.args << "--disable-gpu" if Gem.win_platform?
opts.args << "--no-sandbox"
opts.args << "--disable-dev-shm-usage"
opts.args << "--window-size=1600,1000"
# Workaround https://bugs.chromium.org/p/chromedriver/issues/detail?id=2650&q=load&sort=-id&colspec=ID%20Status%20Pri%20Owner%20Summary
opts.args << "--disable-site-isolation-trials"
end
Capybara::Selenium::Driver.new(app, browser: :chrome, options: browser_options)
end

# Capybara.javascript_driver = :chrome
driver = ENV.fetch("TEST_DRIVER", :headless_chrome)
Capybara.javascript_driver = driver.to_sym

Capybara.server = :puma, { Silent: true }

class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
Expand Down

0 comments on commit 44055c4

Please sign in to comment.