name: version release

on:
  push:
    tags:
      - "v*"

jobs:
  variables:
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.version.outputs.version }}
    steps:
      - name: compute version
        id: version
        run: echo "version=$(echo ${GITHUB_REF} | cut -d 'v' -f 2)" >> $GITHUB_OUTPUT

  build-alpine:
    runs-on: ubuntu-latest
    needs: [variables]

    concurrency:
      group: ${{ github.ref }}-build-docker-alpine-version
      cancel-in-progress: true

    steps:
      - name: checkout
        uses: actions/checkout@v3

      - name: login to docker hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_HUB_USERNAME }}
          password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

      - name: set up qemu
        uses: docker/setup-qemu-action@v2

      - name: set up docker buildx
        uses: docker/setup-buildx-action@v2
        with:
          platforms: linux/amd64,linux/arm64

      - name: build and push alpine
        uses: docker/build-push-action@v3
        with:
          context: .
          file: ./alpine.Dockerfile
          platforms: linux/amd64,linux/arm64
          cache-from: type=gha
          cache-to: type=gha,mode=max
          push: true
          tags: ${{ secrets.DOCKER_HUB_USERNAME }}/catapulte:latest-alpine,${{ secrets.DOCKER_HUB_USERNAME }}/catapulte:${{ needs.variables.outputs.version }}-alpine

  debian:
    runs-on: ubuntu-latest
    needs: [variables]

    concurrency:
      group: ${{ github.ref }}-build-docker-debian-version
      cancel-in-progress: true

    steps:
      - name: checkout
        uses: actions/checkout@v3

      - name: login to docker hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_HUB_USERNAME }}
          password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

      - name: set up qemu
        uses: docker/setup-qemu-action@v2

      - name: set up docker buildx
        uses: docker/setup-buildx-action@v2
        with:
          platforms: linux/amd64,linux/arm64

      - name: build and push debian
        uses: docker/build-push-action@v3
        with:
          platforms: linux/amd64,linux/arm64
          push: true
          cache-from: type=gha
          cache-to: type=gha,mode=max
          tags: ${{ secrets.DOCKER_HUB_USERNAME }}/catapulte:latest,${{ secrets.DOCKER_HUB_USERNAME }}/catapulte:${{ needs.variables.outputs.version }},${{ secrets.DOCKER_HUB_USERNAME }}/catapulte:latest-debian,${{ secrets.DOCKER_HUB_USERNAME }}/catapulte:${{ needs.variables.outputs.version }}-debian
