name: Code Examples

on:
  workflow_dispatch:
    inputs:
      hotVersion:
        description: 'Handsontable version to update all code examples (eg.: 8.0.0)'

jobs:
  Examples:
    name: Version and Deploy
    runs-on: ubuntu-18.04
    steps:
      - uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f # https://github.com/actions/checkout/releases/tag/v2.3.4
      - name: Use Node 15
        uses: actions/setup-node@c46424eee26de4078d34105d3de3cc4992202b1e # https://github.com/actions/setup-node/releases/tag/v2.1.4
        with:
          node-version: '15'
      - name: Checkout gh-pages branch to "./examples/tmp" folder
        uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f # https://github.com/actions/checkout/releases/tag/v2.3.4
        with:
          ref: gh-pages
          path: './examples/tmp'
      - name: Install global dependencies
        run: npm ci
      - name: Create new examples for version
        run: npm run examples:version ${{ github.event.inputs.hotVersion }}
        # this action can throw an error, when the examples for specific version exist already. If they exist, move to the next action
        continue-on-error: true
      - name: Install dependencies of code examples
        run: npm run examples:install ${{ github.event.inputs.hotVersion }}
      - name: Build the code examples
        run: npm run examples:build ${{ github.event.inputs.hotVersion }}
      - name: Test each code example
        run: npm run examples:test ${{ github.event.inputs.hotVersion }}
      - name: Setup git config
        run: |
          git config user.name "GitHub Actions Bot"
          git config user.email "<>"
      - name: Commit
        # make commit only when there is something to commit, so when new examples have been made
        run: |
          git pull --ff
          if [ "$(git status --porcelain)" ]; then
            git add examples/${{ github.event.inputs.hotVersion }}
            git commit -m "Update code examples with version: ${{ github.event.inputs.hotVersion }}"
            git push origin develop
          fi
      - name: Deploy
        uses: peaceiris/actions-gh-pages@bbdfb200618d235585ad98e965f4aafc39b4c501 # https://github.com/peaceiris/actions-gh-pages/releases/tag/v3.7.3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./examples/tmp
