name: Continuous Deployment

on: workflow_dispatch

jobs:
  build-publish:
    name: Build and Publish to NPM
    runs-on: ubuntu-latest

    steps:
      - name: Setup ShabadOS Bot
        uses: shabados/actions/setup-git-identity@release/next
        with:
          user: Shabad OS Bot
          email: team@shabados.com

      - name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Use Node.js 14.x
        uses: actions/setup-node@v1
        with:
          node-version: 14.x
          registry-url: "https://registry.npmjs.org"

      - name: Cache Node.js modules
        uses: actions/cache@v1
        with:
          path: ~/.npm
          key: ${{ runner.OS }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.OS }}-node-
            ${{ runner.OS }}-

      - name: Install npm dependencies
        run: npm ci

      - name: Build module
        run: npm run build

      - name: Release module on NPM
        run: |
          # Configure git
          git config --global push.default current
          git config --global user.email "team@shabados.com"
          git config --global user.name "ShabadOS Bot"

          # Run Release script
          chmod +x release.sh
          echo "Releasing on Github"

          npm run release
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_BOT_TOKEN }}
          GITHUB_TOKEN: ${{ secrets.GH_BOT_TOKEN }}
