name: Publish

# Controls when the action will run.
on:
  workflow_dispatch:
  push:
    # branches: [ main ]
    # only publish on version tags
    tags:
      - v*

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  docs:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: '16.x'
      - uses: actions/cache@v2
        with:
          path: |
            ~/.npm
            ~/.config/yarn/global
          key: ${{ runner.os }}-node-server-api-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-node-server-api-

      - name: Install Yarn
        run: |
          npm install -g yarn
          yarn install

      - name: Build
        run: yarn build

      - name: Build Docs
        run: yarn build-docs

      - name: Publish to npm
        run: |
          npm config set '//registry.npmjs.org/:_authToken' $NPM_TOKEN
          npm publish
        env:
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

      - name: S3 Upload
        run: aws s3 cp docs/ s3://livekit-docs/server-sdk-js --recursive
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.DOCS_DEPLOY_AWS_ACCESS_KEY }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.DOCS_DEPLOY_AWS_API_SECRET }}
          AWS_DEFAULT_REGION: "us-east-1"
