name: Test, Build & Deploy

on:
  push:
    branches: [ 'master' ]
  pull_request:
    branches: [ 'master' ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

jobs:
  clojure:
    name: Test & Build Clojure
    env:
      DOCKER: stable
    runs-on: ubuntu-latest
    steps:
      - name: checkout
        uses: actions/checkout@v3
      - name: install java
        uses: actions/setup-java@v3
        with:
          distribution: 'zulu'
          java-version: '11'
      - name: install clojure tools
        uses: DeLaGuardo/setup-clojure@9.5
        with:
          lein: latest
      - run: ./init-db.sh
        name: start DB
        working-directory: dev/script
      - run: ./init-influx.sh
        name: start Influx
        working-directory: dev/script
      - run: ./init-influx.sh
        name: start Agent
        working-directory: dev/script
      - run: ./start-dind.sh
        name: start Docker in Docker
        working-directory: dev/script
      - run: lein deps
      - run: lein test :all
      - run: lein uberjar
      - name: upload release jar
        uses: actions/upload-artifact@v4
        with:
          name: swarmpit.jar
          path: target/swarmpit.jar
  docker:
    name: Build and push docker
    runs-on: ubuntu-latest
    needs: clojure
    env:
      SECRET: ${{ secrets.PKG_USER }}
      PR: ${{ github.event.pull_request.number }}
    steps:
      - name: checkout
        uses: actions/checkout@v3
      - uses: actions/download-artifact@v4
        name: download release jar
        with:
          name: swarmpit.jar
          path: target
      - name: set up QEMU
        uses: docker/setup-qemu-action@v2
      - name: set up Docker Buildx
        uses: docker/setup-buildx-action@v2
      - name: login to DockerHub
        if: env.SECRET != 0
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USER }}
          password: ${{ secrets.DOCKERHUB_PASS }}
      - name: build and push
        uses: docker/build-push-action@v4
        with:
          context: .
          push: ${{ env.SECRET != 0 }}
          platforms: linux/amd64,linux/arm64/v8,linux/arm/v7,linux/arm/v5
          tags: swarmpit/swarmpit:latest
