tipresias/augury

View on GitHub
.github/workflows/build.yml

Summary

Maintainability
Test Coverage
name: build

on: push

jobs:
  build:
    runs-on: ubuntu-18.04
    env:
      CC_TEST_REPORTER_ID: bc52d889596333356007dd578d7a33f4a69a2c134db9afa2b3d1700ae5455fa9
      AFL_DATA_SERVICE: ${{ secrets.AFL_DATA_SERVICE }}
      PROJECT_ID: ${{ secrets.PROJECT_ID }}
      # Need to use an alternative env var to GOOGLE_APPLICATION_CREDENTIALS,
      # because setup-gcloud overwrites it before exporting the credentials,
      # resulting in the default randomised filepath for the keyfile
      GCP_CREDENTIALS_FILE: .gcloud/keyfile.json
    steps:
      - name: Install dependencies
        run: |
          sudo apt-get update && sudo apt-get -y install haveged && sudo systemctl start haveged
          docker-compose
          docker-compose version
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Set up code coverage monitoring
        run: |
          curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-0.6.3-linux-amd64 > ./cc-test-reporter
          chmod +x ./cc-test-reporter
          ./cc-test-reporter before-build
      - name: Set up Docker Buildx
        id: buildx
        uses: docker/setup-buildx-action@v1
      # - name: Cache Docker layers
      #   uses: actions/cache@v2
      #   with:
      #     path: /tmp/.buildx-cache
      #     key: ${{ runner.os }}-buildx-${{ hashFiles('Dockerfile*') }}-${{ secrets.CACHE_VERSION }}
      #     restore-keys: |
      #       ${{ runner.os }}-buildx-
      - name: Build Docker image
        uses: docker/build-push-action@v2
        with:
          context: ./
          file: ./Dockerfile.local
          builder: ${{ steps.buildx.outputs.name }}
          tags: cfranklin11/tipresias_data_science:latest
          load: true
          cache-from: type=local,src=/tmp/.buildx-cache
          cache-to: type=local,dest=/tmp/.buildx-cache
      - name: Pull afl_data image
        run: docker pull cfranklin11/tipresias_afl_data:latest
      - run: mkdir .gcloud
      - name: Set up Google Cloud SDK
        uses: google-github-actions/setup-gcloud@v0.2.1
        with:
          project_id: ${{ env.PROJECT_ID }}
          service_account_key: ${{ secrets.GC_SA_KEY }}
          export_default_credentials: true
          credentials_file_path: ${{ env.GCP_CREDENTIALS_FILE }}
      # setup-gcloud fails silently, so we have to exit manually
      - name: Check Google Cloud credentials were exported
        id: gcloud_setup_check
        run: |
          if [[ ! -f ${{ env.GCP_CREDENTIALS_FILE }} ]]
          then
              echo 'File "${{ env.GCP_CREDENTIALS_FILE }}" was not found. GOOGLE_APPLICATION_CREDENTIALS=${{ env.GOOGLE_APPLICATION_CREDENTIALS }}.'
              exit 1
          fi
      - name: Lint
        if: ${{ always() }}
        # Temporarily disabling dependency version check, because sub-sub-dependencies
        # are causing issues (i.e. 'cookiecutter' uses old versions of 'MarkupSafe' and 'Jinja2'),
        # and it's not fixable at the moment
        run: |
          # docker run cfranklin11/tipresias_data_science:latest pip3 check
          docker run cfranklin11/tipresias_data_science:latest pylint --disable=R src app.py scripts/save_default_models.py
      - name: Check types
        if: ${{ always() }}
        run: |
          docker run cfranklin11/tipresias_data_science:latest mypy src app.py scripts/save_default_models.py
      - name: Check documentation
        if: ${{ always() }}
        run: |
          docker run cfranklin11/tipresias_data_science:latest pydocstyle src app.py
      - name: Run unit tests
        if: ${{ always() && steps.gcloud_setup_check.conclusion == 'success' }}
        # Using pytest, because coverage.py doesn't play nice with the kedro CLI
        run: |
          docker run \
            -v "${PWD}:/app" \
            -e PYTHON_ENV=ci \
            -e GOOGLE_APPLICATION_CREDENTIALS="${GOOGLE_APPLICATION_CREDENTIALS}" \
            cfranklin11/tipresias_data_science:latest \
            coverage run -m pytest --no-cov src/tests/unit/
      - name: Run integration/system tests
        if: ${{ always() && steps.gcloud_setup_check.conclusion == 'success' }}
        # Using pytest, because coverage.py doesn't play nice with the kedro CLI
        run: |
          docker-compose -f docker-compose.ci.yml run data_science \
            coverage run --append -m pytest src/tests/integration
      - name: Upload test coverage report
        if: ${{ github.ref == 'refs/heads/main' }}
        run: |
          docker-compose -f docker-compose.ci.yml run data_science coverage xml -i
          ./cc-test-reporter format-coverage -t coverage.py
      - name: Login to Docker Hub
        if: ${{ github.ref == 'refs/heads/main' }}
        uses: docker/login-action@v1
        with:
          username: cfranklin11
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: Push updated images
        if: ${{ github.ref == 'refs/heads/main' }}
        run: |
          docker push cfranklin11/tipresias_data_science:latest
      - name: Deploy
        if: ${{ github.ref == 'refs/heads/main' }}
        env:
          AFL_DATA_SERVICE_TOKEN: ${{ secrets.AFL_DATA_SERVICE_TOKEN }}
          DATA_SCIENCE_SERVICE_TOKEN: ${{ secrets.DATA_SCIENCE_SERVICE_TOKEN }}
          ROLLBAR_TOKEN: ${{ secrets.ROLLBAR_TOKEN }}
          TIPRESIAS_APP_TOKEN: ${{ secrets.TIPRESIAS_APP_TOKEN }}
        run: ./scripts/deploy.sh