Skip to content

Instantly share code, notes, and snippets.

@ArturT
Last active June 8, 2021 14:50
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ArturT/b3679cfe7c2d3d8625d54fb5a8966874 to your computer and use it in GitHub Desktop.
Save ArturT/b3679cfe7c2d3d8625d54fb5a8966874 to your computer and use it in GitHub Desktop.
GitHub Actions CI config for Ruby on Rails project with MySQL, Redis, Elasticsearch - how to run parallel tests with Knapsack Pro https://knapsackpro.com/?utm_source=github&utm_medium=gist&utm_campaign=rails-mysql-redis-elasticsearch-on-github-actions&utm_content=sign_up See article https://docs.knapsackpro.com/2019/github-actions-ci-config-for-…
# .github/workflows/main.yaml
name: Main
on: [push]
jobs:
vm-job:
name: CI
runs-on: ubuntu-latest
# If you need DB like MySQL then define service below.
# Example for PostgreSQL and Redis can be found here:
# https://github.com/actions/example-services/tree/master/.github/workflows
services:
# How to use MySQL
mysql:
image: mysql:5.7
env:
MYSQL_ROOT_PASSWORD: root
ports:
- 3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
# How to use Redis
redis:
image: redis
ports:
- 6379/tcp
# How to use Elasticsearch
elasticsearch:
image: elasticsearch:6.8.3
ports:
- 9200/tcp
options: -e="discovery.type=single-node" --health-cmd="curl http://localhost:9200/_cluster/health" --health-interval=10s --health-timeout=5s --health-retries=10
# https://help.github.com/en/articles/workflow-syntax-for-github-actions#jobsjob_idstrategymatrix
strategy:
fail-fast: false
matrix:
# Set N number of parallel jobs you want to run tests on.
# Use higher number if you have slow tests to split them on more parallel jobs.
# Remember to update ci_node_index below to 0..N-1
ci_node_total: [5]
# set N-1 indexes for parallel jobs
# When you run 2 parallel jobs then first job will have index 0, the second job will have index 1 etc
ci_node_index: [0, 1, 2, 3, 4]
steps:
- name: Checkout
uses: actions/checkout@v1
with:
fetch-depth: 1
- name: Set up Ruby 2.6
uses: actions/setup-ruby@v1
with:
ruby-version: 2.6.5
- uses: actions/cache@v1
with:
path: vendor/bundle
key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
restore-keys: |
${{ runner.os }}-gems-
- name: Verify MySQL connection from host
run: |
sudo apt-get install -y mysql-client libmysqlclient-dev
mysql --host 127.0.0.1 --port ${{ job.services.mysql.ports[3306] }} -uroot -proot -e "SHOW GRANTS FOR 'root'@'localhost'"
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql --host 127.0.0.1 --port ${{ job.services.mysql.ports[3306] }} -uroot -proot mysql
- name: Verify Elasticsearch connection from host
env:
ELASTIC_SEARCH_URL: http://localhost:${{ job.services.elasticsearch.ports[9200] }}
run: |
echo $ELASTIC_SEARCH_URL
curl -fsSL "$ELASTIC_SEARCH_URL/_cat/health?h=status"
- name: Bundle Install and Create DB
env:
RAILS_ENV: test
DB_PASSWORD: root
# tell Rails to use proper port for MySQL
DB_PORT: ${{ job.services.mysql.ports[3306] }}
run: |
cp .env.sample .env
cp config/database.yml.ci config/database.yml
gem install bundler --version 1.17.3 --no-ri --no-rdoc
bundle config path vendor/bundle
bundle install --jobs 4 --retry 3 --path vendor/bundle
# create DB for Rails 5.x
bin/rails db:setup
# create DB for Rails 6.x
bin/rails db:prepare
- name: Run tests
env:
DB_PASSWORD: root
DB_PORT: ${{ job.services.mysql.ports[3306] }} # get randomly assigned published port
REDIS_PORT: ${{ job.services.redis.ports[6379] }}
ELASTIC_SEARCH_URL: http://localhost:${{ job.services.elasticsearch.ports[9200] }}
RAILS_ENV: test
# You need to set API tokens in GitHub repository Settings -> Secrets
KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC: ${{ secrets.KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC }}
KNAPSACK_PRO_TEST_SUITE_TOKEN_CUCUMBER: ${{ secrets.KNAPSACK_PRO_TEST_SUITE_TOKEN_CUCUMBER }}
KNAPSACK_PRO_TEST_SUITE_TOKEN_MINITEST: ${{ secrets.KNAPSACK_PRO_TEST_SUITE_TOKEN_MINITEST }}
KNAPSACK_PRO_TEST_SUITE_TEST_UNIT: ${{ secrets.KNAPSACK_PRO_TEST_SUITE_TOKEN_TEST_UNIT }}
KNAPSACK_PRO_TEST_SUITE_TOKEN_SPINACH: ${{ secrets.KNAPSACK_PRO_TEST_SUITE_TOKEN_SPINACH }}
KNAPSACK_PRO_CI_NODE_TOTAL: ${{ matrix.ci_node_total }}
KNAPSACK_PRO_CI_NODE_INDEX: ${{ matrix.ci_node_index }}
# if you use Knapsack Pro Queue Mode you must set below env variable
# to be able to retry CI build and run previously recorded tests
KNAPSACK_PRO_FIXED_QUEUE_SPLIT: true
run: |
# run tests in Knapsack Pro Regular Mode
bundle exec rake knapsack_pro:rspec
bundle exec rake knapsack_pro:cucumber
bundle exec rake knapsack_pro:minitest
bundle exec rake knapsack_pro:test_unit
bundle exec rake knapsack_pro:spinach
# you can use Knapsack Pro in Queue Mode once recorded first CI build with Regular Mode
bundle exec rake knapsack_pro:queue:rspec
bundle exec rake knapsack_pro:queue:cucumber
bundle exec rake knapsack_pro:queue:minitest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment