# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: ci

on: [push, pull_request]

jobs:
  build:
    runs-on: ubuntu-latest
    container: docker:latest
    if: "!contains(github.event.head_commit.message, '[skip ci]')"

    strategy:
      matrix:
        node: [14]

    steps:
      - name: Prepare
        run: |
          echo "@edge https://uk.alpinelinux.org/alpine/edge/main" >> /etc/apk/repositories
          apk upgrade --no-cache > /dev/null
          apk add apache2-utils bash bind-tools curl git jq net-tools nodejs npm openssh openssl postgresql-client tar tzdata zstd > /dev/null
          sed -i "s#:/bin/ash#:/bin/bash#g" /etc/passwd
          echo "alias sh='/bin/bash ';" >> /root/.bashrc
          docker -v
          bash --version
          npm -v && node -v
          ssh -V
          tar --version
          git --version
          uname -a
      - uses: actions/checkout@v2
      - name: Use Node.js ${{ matrix.node }}
        uses: actions/setup-node@v2
        with:
          node-version: ${{ matrix.node }}
      - name: Cache
        uses: actions/cache@v2
        id: cache-lerna
        env:
          cache-name: cache-lerna
        with:
          path: |
            node_modules
            */*/node_modules
          key: ${{ runner.OS }}-build-${{ env.cache-name }}-${{ github.ref }}-${{ hashFiles('**/lockfiles') }}

      - name: Verify
        run: |
          psql -V
          dig postgres
          echo "\l" | psql -h $POSTGRES_HOST -p $POSTGRES_PORT -U$POSTGRES_USER -d postgres
          #psql -h $POSTGRES_HOST -p $POSTGRES_PORT -U$POSTGRES_USER -d $POSTGRES_DB -1 -bq -f ./packages/demo-service/src/database/init.sql
          psql -h $POSTGRES_HOST -p $POSTGRES_PORT -U$POSTGRES_USER -d $POSTGRES_DB -c "\d+"
          #psql -h $POSTGRES_HOST -p $POSTGRES_PORT -U$POSTGRES_USER -d $POSTGRES_DB -c "SELECT * FROM tb_user"
        env:
          CI: true
          POSTGRES_HOST: postgres
          POSTGRES_PORT: ${{ job.services.postgres.ports[5432] }}
          POSTGRES_DB: db_ci_test
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          PGPASSWORD: postgres

      - name: npm build
        run: |
          openssl version
          node -v
          npm i -g lerna nyc rollup
          npm run bootstrap
          npm run build --if-present
        env:
          CI: true
      - name: npm ci
        run: |
          # lint must after build
          npm run lint:s
          npm run ci
        env:
          CI: true
          # use `localhost` if running the job on the VM
          # use `postgres` if running the job on a container
          POSTGRES_HOST: postgres
          POSTGRES_PORT: ${{ job.services.postgres.ports[5432] }}
          POSTGRES_DB: db_ci_test
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          PGPASSWORD: postgres
          # redis
          REDIS_HOST: redis
          REDIS_PORT: 6379
      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v1


    services:
      postgres:
        image: postgres:alpine
        ports:
          - 5432:5432
        env:
          POSTGRES_DB: db_ci_test
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
        # needed because the postgres container does not provide a healthcheck
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

