name: Deploy lib
on:
  push:
    tags:
      - '*'
jobs:
  send-tg-msg:
    name: Send TG message
    runs-on: ubuntu-latest
    steps:
      - name: Checkout source code
        uses: actions/checkout@v2

      - name: Send Telegram message
        uses: proDreams/actions-telegram-notifier@main
        with:
          token: ${{ secrets.TELEGRAM_TOKEN }}
          chat_id: ${{ secrets.TELEGRAM_CHAT_ID }}
          thread_id: ${{ secrets.TELEGRAM_TOPIC_ID }}
          status: pending
          notify_fields: 'repo_with_tag,commit,workflow'
          title: 'Building npm package.'

  build:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      id-token: write

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: '22.x'
          registry-url: 'https://registry.npmjs.org'

      - name: Update npm
        run: npm install -g npm@latest

      - name: Install dependencies and build
        working-directory: './'
        run: npm i && npm run prepublish

      - name: Publish package on NPM
        working-directory: './'
        run: npm publish

  send-finish-tg-msg:
    name: Send TG message
    needs: [build]
    runs-on: ubuntu-latest
    steps:
      - name: Checkout source code
        uses: actions/checkout@v2

      - name: Send Telegram message
        uses: proDreams/actions-telegram-notifier@main
        with:
          token: ${{ secrets.TELEGRAM_TOKEN }}
          chat_id: ${{ secrets.TELEGRAM_CHAT_ID }}
          thread_id: ${{ secrets.TELEGRAM_TOPIC_ID }}
          status: ${{ job.status }}
          notify_fields: 'repo_with_tag,commit,workflow'
          title: 'Build Dev finished.'

  notify-on-error:
    runs-on: ubuntu-latest
    needs: [build]
    if: failure()
    steps:
      - name: Checkout source code
        uses: actions/checkout@v2

      - name: Send error notification
        uses: proDreams/actions-telegram-notifier@main
        with:
          token: ${{ secrets.TELEGRAM_TOKEN }}
          chat_id: ${{ secrets.TELEGRAM_CHAT_ID }}
          thread_id: ${{ secrets.TELEGRAM_TOPIC_ID }}
          status: failure
          notify_fields: 'repo_with_tag,commit,workflow'
          title: 'Build Dev failed.'
