name: Publish docs via GitHub Pages
on:
  push:
    branches:
      - master
  release:
    types:
      - published
jobs:
  verify-docs:
    name: Verify docs
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [12.x]
    steps:
      - name: Set up Python 3.7
        uses: actions/setup-python@v2
        with:
          python-version: '3.x'
          
      - name: Checkout Release from lens
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Using Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}

      - name: Generate Extensions API Reference using typedocs
        run: |
           yarn install
           yarn typedocs-extensions-api

      - name: Verify that the markdown is valid
        run: |
          yarn run verify-docs

  build:
    name: Deploy docs
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [12.x]
    needs: verify-docs
    steps:
      - name: Set up Python 3.7
        uses: actions/setup-python@v2
        with:
          python-version: '3.x'

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install git+https://${{ secrets.GH_TOKEN }}@github.com/lensapp/mkdocs-material-insiders.git
          pip install mike

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

      - name: git config
        run: |
           git config --local user.email "action@github.com"
           git config --local user.name "GitHub Action"

      - name: Using Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}

      - name: Generate Extensions API Reference using typedocs
        run: |
           yarn install
           yarn typedocs-extensions-api

      - name: mkdocs deploy master
        if: contains(github.ref, 'refs/heads/master')
        run: |
           mike deploy --push master

      - name: Get the release version
        if: contains(github.ref, 'refs/tags/v') # && !github.event.release.prerelease (generate pre-release docs until Lens 4.0.0 is GA, see #1408)
        id: get_version
        run: echo ::set-output name=VERSION::${GITHUB_REF/refs\/tags\//}

      - name: mkdocs deploy new release
        if: contains(github.ref, 'refs/tags/v') # && !github.event.release.prerelease (generate pre-release docs until Lens 4.0.0 is GA, see #1408)
        run: |
           mike deploy --push --update-aliases ${{ steps.get_version.outputs.VERSION }} latest
           mike set-default --push ${{ steps.get_version.outputs.VERSION }}
