name: Upload the latest release

on:
  push:
    tags: [ "v*" ]

jobs:
  create:
    name: "Creates the newest release by version"
    runs-on: "ubuntu-latest"

    steps:
      - name: Checkout project
        uses: actions/checkout@v2.3.4

      - name: Setup node
        uses: actions/setup-node@v3
        with:
          node-version: '16'

      - name: Set up QEMU
        uses: docker/setup-qemu-action@master
        with:
          platforms: all

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

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

      - run: cd client && npm install --force
      - run: npm run build && mv client/build .

      - name: Get version
        id: get_version
        run: echo "::set-output name=version::$(jq .version package.json --raw-output)"

      - name: Install zip
        run: sudo apt-get install zip

      - name: Zip all files
        run: zip -r MySpeed-${{ steps.get_version.outputs.version }}.zip build server package.json package-lock.json

      - uses: "marvinpinto/action-automatic-releases@latest"
        with:
          repo_token: "${{ secrets.GITHUB_TOKEN }}"
          prerelease: false
          title: Release ${{ steps.get_version.outputs.version }}
          files: |
            ./MySpeed-*.zip

      - name: Build and push
        uses: docker/build-push-action@v3
        with:
          push: true
          platforms: linux/amd64,linux/arm64,linux/arm/v7
          tags: |
            germannewsmaker/myspeed:latest
            germannewsmaker/myspeed:${{ steps.get_version.outputs.version }}