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

name: manual-publish

on:
  workflow_dispatch:
    inputs:
      semver:
        description: 'Leave empty or input major|minor|patch'
        required: false
        default: ''

jobs:
  build:
    runs-on: ubuntu-latest
    container: docker:latest

    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 }}
          registry-url: 'https://registry.npmjs.org'
      - name: Verify
        run: |
          echo $GITHUB_REF
          # github project/settings/Secrets/New repository secret: NPM_TOKEN
          npm whoami
          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
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
          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
      - name: npm ci
        run: |
          # lint must after build
          npm run lint:nofix
          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: npm publish
        run: |
          git config user.name $GITHUB_ACTOR
          git config user.email gh-actions-${GITHUB_ACTOR}@github.com
          # npm run pub major
          bash .scripts/publish.sh --yes --conventional-commits --create-release github ${{ github.event.inputs.semver }}
          git push -v origin --follow-tags
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

      - 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

