name: Publish Docker image
on:
  push:
    tags:
     - 'v*.*.*'
  workflow_dispatch:
jobs:
  push_to_registry:
    name: Push Docker image to Docker Hub
    runs-on: ubuntu-latest
    steps:
      - name: Prepare
        id: prep
        run: |
          DOCKER_IMAGE=flobz/psa_car_controller
          VERSION=edge
          if [[ $GITHUB_REF == refs/tags/* ]]; then
            VERSION=${GITHUB_REF#refs/tags/}
            MINOR_VERSION=${VERSION%.*}
            MAJOR=${MINOR_VERSION%.*}
            TAGS="${DOCKER_IMAGE}:${VERSION},${DOCKER_IMAGE}:${MINOR_VERSION},${DOCKER_IMAGE}:${MAJOR},${DOCKER_IMAGE}:latest"
          else
            TAGS="${DOCKER_IMAGE}:${VERSION}"
          fi
          echo ::set-output name=tags::${TAGS}
      - name: Set up QEMU
        uses: docker/setup-qemu-action@master
        with:
          platforms: all
      - name: Set up Docker Buildx
        id: buildx
        uses: docker/setup-buildx-action@master
      - name: Check out the repo
        uses: actions/checkout@v2
      - name: Login to DockerHub
        uses: docker/login-action@v1 
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
      -
        name: Build and push
        id: docker_build
        uses: docker/build-push-action@v2
        with:
          context: .
          push: true
          tags: ${{ steps.prep.outputs.tags }}
          platforms: linux/amd64,linux/arm64,linux/arm/v7
      -
        name: Image digest
        run: echo ${{ steps.docker_build.outputs.digest }}
