name: update

on:
  schedule:
    # Run daily at 04:20 UTC.
    - cron: '20 4 * * *'
  workflow_dispatch:

jobs:
  update:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          ref: ${{ github.head_ref }}
          token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
          # Fetching full history is required for the changelog script.
          fetch-depth: 0

      - name: Set up Node.js
        uses: actions/setup-node@v3
        with:
          node-version-file: '.nvmrc'

      - name: Build
        run: |
          scripts/update-to-latest.sh

      - name: Check out debugger-protocol-viewer
        uses: actions/checkout@v2
        with:
          repository: ChromeDevTools/debugger-protocol-viewer
          path: debugger-protocol-viewer
          token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}

      - name: Build site
        working-directory: debugger-protocol-viewer
        run: |
          npm install
          npm run prep
          npm run build

      - name: Deploy to gh-pages
        uses: JamesIves/github-pages-deploy-action@v4
        with:
          branch: gh-pages
          folder: debugger-protocol-viewer/devtools-protocol
          ssh-key: ${{ secrets.SSH_PRIVATE_KEY }}
          single-commit: true
