name: 'Build, Test, Release'

on:
  workflow_dispatch:
    inputs:
      version:
        description: Choose a release type
        required: true
        type: choice
        options:
          - prerelease
          - prepatch
          - preminor
          - premajor
          - patch
          - minor
          - major

jobs:
  release:
    name: 'Build, Test, Publish'
    runs-on: ubuntu-20.04

    steps:
      - name: "Check out repository ✨"
        uses: "actions/checkout@v3.1.0"
        with:
          ref: ${{ github.ref }}
          token: ${{ secrets.TSPAT }}

      - name: Setup node env 📦
        uses: actions/setup-node@v3
        with:
          node-version-file: 'package.json'
          check-latest: true
          cache: 'npm'

      - name: npm version 
        run: |
          echo "NPM_VERSION=$(npm version ${{ github.event.inputs.version }} --preid pre --no-git-tag-version)" >> $GITHUB_ENV

      - name: Push version to git
        run: |
          git config user.name github-actions
          git config user.email github-actions@github.com
          git commit -a -m "Update TileServer version to ${{ env.NPM_VERSION }} (${{ github.event.inputs.version }})"
          git push

      - name: Update apt-get 🚀
        run: sudo apt update -qq

      - name: Install dependencies (Ubuntu) 🚀
        run: >-
          sudo apt-get install -qq libcairo2-dev libjpeg8-dev libpango1.0-dev
          libgif-dev build-essential g++ xvfb libgles2-mesa-dev libgbm-dev
          libxxf86vm-dev

      - name: Install node dependencies
        run: npm install

      - name: Pull test data 📦
        run: wget -O test_data.zip https://github.com/acalcutt/tileserver-gl/releases/download/test_data/test_data.zip

      - name: Prepare test data 📦
        run: unzip -q test_data.zip -d test_data

      - name: Run tests 🧪
        run: xvfb-run --server-args="-screen 0 1024x768x24" npm test

      - name: Remove Test Data
        run: rm -R test_data*

      - name: Publish to Full Version NPM
        run: |
          npm config set //registry.npmjs.org/:_authToken ${NPM_TOKEN}
          npm publish --access public
        env:
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2
        with:
          platforms: 'arm64'

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

      - name: Login to DockerHub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Build and publish Full Version to Docker Hub
        uses: docker/build-push-action@v4
        with:
          context: .
          push: true
          tags: wifidb/tileserver-gl:latest, wifidb/tileserver-gl:${{env.NPM_VERSION}}
          platforms: linux/arm64,linux/amd64
          # experimental: https://github.com/docker/build-push-action/blob/master/docs/advanced/cache.md#cache-backend-api
          cache-from: type=gha
          cache-to: type=gha,mode=max

      - name: Create Tileserver Light Directory
        run: node publish.js --no-publish

      - name: Install node dependencies
        run: npm install
        working-directory: ./light

      - name: Publish to Light Version NPM
        working-directory: ./light
        run: |
          npm config set //registry.npmjs.org/:_authToken ${NPM_TOKEN}
          npm publish --access public
        env:
          NPM_TOKEN: ${{ secrets.NPM_TOKEN  }}

      - name: Build and publish Light Version to Docker Hub
        uses: docker/build-push-action@v4
        with:
          context: ./light
          file: ./light/Dockerfile
          push: true
          tags: wifidb/tileserver-gl-light:latest, wifidb/tileserver-gl-light:${{env.NPM_VERSION}}
          platforms: linux/arm64,linux/amd64
          # experimental: https://github.com/docker/build-push-action/blob/master/docs/advanced/cache.md#cache-backend-api
          cache-from: type=gha
          cache-to: type=gha,mode=max
