otvorenesudy/otvorenesudy-api

View on GitHub
.github/workflows/main.yml

Summary

Maintainability
Test Coverage
name: CI & CD

on: [push]

env:
  RAILS_ENV: test
  CI: true

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  test:
    name: Run RSpec

    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres:16
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: opencourts_api_test
          RAILS_ENV: test
        ports:
          - 5432:5432
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4
        with:
          submodules: 'true'

      - name: Setup ENV File
        run: mv .env.ci .env.test

      - name: Install Required Packages
        run: |
          sudo apt-get update
          sudo apt-get install -y --no-install-recommends libpq-dev libssl-dev libcurl4 libcurl4-openssl-dev

      - name: 'Read Ruby Version'
        id: read-ruby-version
        run: echo "RUBY_VERSION=$(cat .ruby-version)" >> $GITHUB_OUTPUT

      - name: Setup Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: '${{ steps.read-ruby-version.outputs.RUBY_VERSION }}'
          bundler-cache: true

      - name: Install Bundle
        run: |
          gem install bundler
          bundle install --jobs 4 --retry 3

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version-file: 'package.json'
          cache: 'yarn'
          registry-url: 'https://registry.npmjs.org'

      - name: Install Yarn
        run: npm install -g yarn

      - name: Install Yarn
        run: yarn install

      - name: Setup Database
        run: |
          bin/rails db:create db:schema:load
          bin/rails db:test:load_schema

      - name: Prepare Code Climate
        env:
          CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
        run: |
          curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
          chmod +x ./cc-test-reporter
          ./cc-test-reporter before-build

      - name: Run RSpec
        run: bin/rspec

      - name: Notify Code Climate
        env:
          CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
        run: ./cc-test-reporter after-build --coverage-input-type simplecov --exit-code $?

  deploy:
    name: Deploy to Production
    needs: [test]
    if: github.ref == 'refs/heads/main'

    runs-on: ubuntu-latest

    steps:
      - name: Install SSH key
        uses: shimataro/ssh-key-action@v2
        with:
          key: ${{ secrets.SSH_KEY }}
          name: github-actions
          known_hosts: '...'
          config: |
            host otvorenesudy.sk
              IdentityFile ~/.ssh/github-actions
              IdentitiesOnly yes
              ForwardAgent yes

      - name: Adding Known Hosts
        run: ssh-keyscan -H ${{ secrets.SSH_HOST }}

      - name: Checkout Repository
        uses: actions/checkout@v4
        with:
          submodules: 'true'

      - name: Setup Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: 3.3.0
          bundler-cache: true

      - name: Deploy
        run: |
          eval "$(ssh-agent -s)"
          ssh-add ~/.ssh/github-actions
          bundle exec cap production deploy deploy:restart sidekiq:restart