name: CI on:

push:
  branches: [ main ]
pull_request:
  branches: [ main ]

jobs:

test:
  name: Tests
  runs-on: ubuntu-latest
  services:
    postgres:
      image: postgres:11
      env:
        POSTGRES_USER: <%= @repository_name %>
        POSTGRES_DB: <%= "#{@repository_name}_test" %>
        POSTGRES_PASSWORD: postgres
      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: <%= "Set up Ruby #{@ruby_version}" %>
      uses: ruby/setup-ruby@v1
      with:
        ruby-version: <%= @ruby_version %>
        bundler-cache: true
    - name: Setup Node
      uses: actions/setup-node@v1
      with:
        node-version: <%= @node_version %>
    - name: Install library for postgres
      run: sudo apt-get install libpq-dev
    - name: Find yarn cache location
      id: yarn-cache
      run: echo "::set-output name=dir::$(yarn cache dir)"
    - name: JS package cache
      uses: actions/cache@v1
      with:
        path: ${{ steps.yarn-cache.outputs.dir }}
        key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
        restore-keys: |
          ${{ runner.os }}-yarn-
    - name: Install packages
      run: |
        yarn install --pure-lockfile
    - name: Setup test database
      env:
        RAILS_ENV: test
        PGHOST: localhost
        PGUSER: <%= @repository_name %>
        PGPASSWORD: postgres
      run: |
        bin/rails db:setup
    - name: Run tests
      env:
        RAILS_ENV: test
        PGHOST: localhost
        PGUSER: <%= @repository_name %>
        PGPASSWORD: postgres
      run: bundle exec rails test # TODO: Update the test runner command as per your requirement.