name: Publish

on:
  push:
    branches: [ master ]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: 10.x
      - run: yarn install
      - run: yarn test:unit
      - run: yarn test:integration

  publish-npm:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: 10.x
          registry-url: https://registry.npmjs.org/
          scope: '@pusher'
      - run: yarn install
      - run: yarn publish --verbose
        env:
          NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}

  create-release:
    needs: publish-npm
    name: Create Release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Setup git
        run: |
          git config user.email "pusher-ci@pusher.com"
          git config user.name "Pusher CI"
      - name: Prepare description
        run: |
          csplit -s CHANGELOG.md "/##/" {1}
          cat xx01 > CHANGELOG.tmp
      - name: Prepare tag
        run: |
          export TAG=$(head -1 CHANGELOG.tmp | cut -d' ' -f2)
          echo "TAG=$TAG" >> $GITHUB_ENV
      - name: Create Release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ env.TAG }}
          release_name: ${{ env.TAG }}
          body_path: CHANGELOG.tmp
          draft: false
          prerelease: false
