name: Publish documentation on GitHub Pages

on: workflow_dispatch

jobs:
  authorize:
    name: Authorize
    runs-on: ubuntu-latest
    steps:
      - name: ${{ github.actor }} permission check to do a release
        uses: "lannonbr/repo-permission-check-action@2.0.2"
        with:
          permission: "write"
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  gh-pages:
    name: Publish to GitHub Pages
    runs-on: ubuntu-latest
    needs: [authorize]

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

      - name: node_modules cache
        uses: actions/cache@v4
        with:
          path: '**/node_modules'
          key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}

      - name: Setup Node.js
        uses: actions/setup-node@v3
        with:
          node-version: 16.x

      - name: Install dependencies
        run: yarn install --frozen-lockfile && yarn docs:install --frozen-lockfile

      - name: Generate website assets
        run: yarn docs:generate-jsdoc

      - name: Add key to allow access to repository
        env:
          SSH_AUTH_SOCK: /tmp/ssh_agent.sock
        run: |
          mkdir -p ~/.ssh
          ssh-keyscan github.com >> ~/.ssh/known_hosts
          echo "${{ secrets.GH_PAGES_DEPLOY }}" > ~/.ssh/id_rsa
          chmod 600 ~/.ssh/id_rsa
          cat <<EOT >> ~/.ssh/config
          Host github.com
          HostName github.com
          IdentityFile ~/.ssh/id_rsa
          EOT

      - name: Release to GitHub Pages
        env:
          USE_SSH: true
          GIT_USER: amplitude-sdk-bot
        run: |
          git config --global user.email "amplitude-sdk-bot@users.noreply.github.com"
          git config --global user.name "amplitude-sdk-bot"
          yarn docs:deploy
