pupilfirst/pupilfirst

View on GitHub
.github/workflows/ci.yml

Summary

Maintainability
Test Coverage
name: Continuous Integration

on:
  push:
    branches: [master]
    paths-ignore:
      - "docs/**"
  pull_request:
    branches: [master]
    paths-ignore:
      - "docs/**"
  workflow_dispatch:

env:
  PF_IMAGE_NAME: pupilfirst
  PF_VERSION: "2024.1"
  YARN_CHECKSUM_BEHAVIOR: ignore
jobs:
  tests:
    runs-on: ubuntu-latest
    if: "!contains(github.event.head_commit.message, 'skip ci')"

    services:
      postgres:
        image: postgres:11.7
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: postgres
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 5432:5432

    env:
      RAILS_ENV: test
      RUBYOPT: "-W:no-deprecated -W:no-experimental"
      JAVASCRIPT_DRIVER: headless_chrome
      DB_HOST: localhost
      DB_NAME_TEST: postgres
      DB_USERNAME: postgres
      DB_PASSWORD: postgres
      CAPYBARA_MAX_WAIT_TIME: 5
      CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
      SPEC_USER_TIME_ZONE: Etc/UTC
      SPEC_RETRY_COUNT: 2
      RECAPTCHA_V3_SITE_KEY: ignored
      RECAPTCHA_V3_SECRET_KEY: ignored
      RECAPTCHA_V2_SITE_KEY: ignored
      RECAPTCHA_V2_SECRET_KEY: ignored

    steps:
      - name: Install libvips
        run: |
          sudo apt-get update
          sudo apt-get install -y libvips
      - uses: actions/checkout@v2
      - name: Use Ruby 3.2
        uses: ruby/setup-ruby@v1
      - name: Set branch name & commit SHA in env
        uses: actions/github-script@v3
        with:
          script: |
            console.log(`Event: ${context.eventName}`);
            let branchName = "";

            if (context.eventName === "push") {
              // Pushes have a '/ref/heads/' prefixed to the branch name.
              branchName = process.env.GITHUB_REF.replace("refs/heads/", "");
            } else {
              branchName = process.env.GITHUB_HEAD_REF;
            }

            console.log(`Setting GIT_BRANCH=${branchName}`);
            core.exportVariable("GIT_BRANCH", branchName);
            console.log(`Setting GIT_COMMIT_SHA=${context.sha}`);
            core.exportVariable("GIT_COMMIT_SHA", context.sha);
      - name: Restore Bundle
        uses: actions/cache@master
        with:
          path: vendor/bundle
          key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
          restore-keys: |
            ${{ runner.os }}-gems-
      - name: Bundle
        run: |
          bundle config path vendor/bundle
          bundle install --jobs 4 --retry 3
      - name: Rubocop
        run: bundle exec rubocop
      - name: Setup Database
        run: |
          bundle exec rake db:schema:load
      - name: Restore Lerna
        uses: actions/cache@master
        with:
          path: |
            node_modules
            app/frontend/packages/*/node_modules
          key: ${{ runner.os }}-lerna-${{ hashFiles('**/yarn.lock') }}
      - name: Yarn Install
        run: yarn install
      - name: Generate locales.json
        run: bundle exec i18n export
      - name: Compile ReScript files
        run: yarn run re:build
      - name: Specs (On master branch)
        if: env.GIT_BRANCH == 'master'
        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
          COVERAGE=true bundle exec rspec -fd
          ./cc-test-reporter after-build -t simplecov --exit-code $?
      - name: Specs (On non-master branches)
        if: env.GIT_BRANCH != 'master'
        run: bundle exec rspec -fd
      - uses: actions/upload-artifact@v3
        if: failure()
        with:
          name: Logs
          path: |
            tmp/capybara
            log/test.log
  dockerhub-master:
    needs: tests
    environment: Production
    name: Build & Push to Dockerhub
    if: github.ref == 'refs/heads/master'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Github Short SHA
        run: echo "SHORT_SHA=`echo ${GITHUB_SHA} | cut -c1-8`" >> $GITHUB_ENV

      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_HUB_USERNAME }}
          password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
      - name: Build image
        run: docker build . --file Dockerfile --tag $PF_IMAGE_NAME

      - name: Push image
        run: |
          PF_IMAGE_ID=${{ secrets.DOCKER_HUB_USERNAME }}/$PF_IMAGE_NAME
          PF_IMAGE_ID=$(echo $PF_IMAGE_ID | tr '[A-Z]' '[a-z]')
          echo PF_IMAGE_ID=$PF_IMAGE_ID
          echo PF_VERSION=$PF_VERSION
          docker tag $PF_IMAGE_NAME $PF_IMAGE_ID:latest
          docker tag $PF_IMAGE_NAME $PF_IMAGE_ID:$PF_VERSION
          docker tag $PF_IMAGE_NAME $PF_IMAGE_ID:$PF_VERSION.$SHORT_SHA
          docker push $PF_IMAGE_ID --all-tags
  deploy_to_digital_ocean:
    needs: dockerhub-master
    name: DigitalOcean App Platform deployment
    if: github.ref == 'refs/heads/master'
    runs-on: ubuntu-latest
    env:
      DIGITALOCEAN_ACCESS_TOKEN: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
    steps:
      - name: Deploy to DigitalOcean
        uses: digitalocean/app_action@main
        if: "${{ env.DIGITALOCEAN_ACCESS_TOKEN != '' && vars.ENABLE_AUTO_DEPLOY == 'true' }}"
        with:
          app_name: pupilfirst
          token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}