name: BuildContainerImage

on:
  push:
    tags:
      - 'v4*'

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    if: github.repository == 'kubesphere/kubekey'
    env:
      GO111MODULE: on
    steps:
      - name: Prepare
        id: prepare
        run: |
          VERSION=latest

          if [[ $GITHUB_REF == refs/tags/* ]]; then
            VERSION=${GITHUB_REF#refs/tags/}
          fi
          echo "version=${VERSION}" >> "$GITHUB_OUTPUT"

      - name: Checkout code
        uses: actions/checkout@v3

      - name: Login to DOCKER
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_PASSWORD }}

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

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

      - name: Build and push Docker images
        run: |
          tag=${{ steps.prepare.outputs.version }}
          if [[ $tag =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
            REGISTRY=docker.io/kubesphere TAG=$tag make docker-push
          else
            REGISTRY=docker.io/kubespheredev TAG=$tag make docker-push
          fi
