eizengan/reforge

View on GitHub
.github/workflows/ci.yml

Summary

Maintainability
Test Coverage
name: CI

on:
  pull_request:
  push:
    branches:
    - main

env:
  BUNDLE_PATH: vendor/bundle

jobs:
  ci:
    strategy:
      fail-fast: false
      matrix:
        os: ['macos', 'ubuntu', 'windows']
        ruby: ['2.5', '2.6', '2.7', '3.0', '3.1']
    runs-on: ${{ matrix.os }}-latest
    steps:
    - uses: actions/checkout@v2
      with:
        fetch-depth: 2
    - uses: ruby/setup-ruby@v1
      with:
        ruby-version: ${{ matrix.ruby }}
    - name: Cache dependencies
      id: cache-dependencies
      uses: actions/cache@v2
      with:
        path: ${{ env.BUNDLE_PATH }}
        key: ${{ matrix.os }}-${{ matrix.ruby }}-gems-${{ hashFiles('**/Gemfile', '**/*.gemspec') }}
        restore-keys: ${{ matrix.os }}-${{ matrix.ruby }}-gems-
    - name: Download test reporter
      if: ${{ matrix.os == 'ubuntu' && matrix.ruby == '2.7' }}
      run: |
        curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
        chmod +x ./cc-test-reporter
    - name: Install dependencies
      if: steps.cache-dependencies.outputs.cache-hit != 'true'
      run: bundle install --jobs 20 --retry 5
    - name: Lint files
      run: bundle exec rake rubocop
    - name: Notify of pending report
      if: ${{ matrix.os == 'ubuntu' && matrix.ruby == '2.7' }}
      run: ./cc-test-reporter before-build
    - name: Run tests
      run: bundle exec rake rspec
    - name: Publish code coverage
      if: ${{ matrix.os == 'ubuntu' && matrix.ruby == '2.7' }}
      # TRICKY: We need to manually set the env vars required by Code Climate. GIT_BRANCH is simple to determine, but
      # GIT_COMMIT_SHA is context dependent:
      # - When running actions on main when it is pushed we can simply use GITHUB_SHA
      # - When running actions on a pull request actions/checkout@v2 creates a merge commit, so GITHUB_SHA is one
      #   commit ahead. We use the log to determine GIT_COMMIT_SHA by looking at the second parent of the merge commit,
      #   and set actions/checkout@v2's fetch-depth to 2 above to ensure that the commit is available
      # (ref: https://docs.codeclimate.com/docs/github-actions-test-coverage)
      run: |
        export GIT_BRANCH="${GITHUB_HEAD_REF}"
        if [ $GITHUB_EVENT_NAME == "pull_request" ]
        then
          export GIT_COMMIT_SHA="$(git log --pretty=%P -n 1 "${GITHUB_SHA}" | cut -d' ' -f2)"
        else
          export GIT_COMMIT_SHA="${GITHUB_SHA}"
        fi
        ./cc-test-reporter after-build -r ${{secrets.CC_TEST_REPORTER_ID}}