name: Fleetbase Docker Images

on:
    push:
        tags:
            - 'v*'
    workflow_dispatch:
        inputs:
            branch:
                description: 'Branch to build from'
                required: false
                default: 'main'
            version:
                description: 'Image version tag (e.g., v0.7.1-beta)'
                required: false

jobs:
    docker-release:
        name: Build and Push Docker Images
        runs-on: ubuntu-latest

        env:
            REGISTRY: fleetbase
            VERSION: ${{ github.event.inputs.version || (github.ref_type == 'tag' && startsWith(github.ref_name, 'v') && github.ref_name) || 'manual' }}

        steps:
            - name: Checkout Repo
              uses: actions/checkout@v3
              with:
                  ref: ${{ github.event.inputs.branch || github.ref_name }}
                  submodules: recursive

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

            - name: Log in to Docker Hub
              uses: docker/login-action@v3
              with:
                  username: ${{ secrets.DOCKER_HUB_USERNAME }}
                  password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

            - name: Build and Push Console & API Images
              uses: docker/bake-action@v2
              with:
                  push: true
                  targets: |
                      fleetbase-console
                      fleetbase-api
                  files: |
                      ./docker-bake.hcl
