name: Build and Push Images

on:
  push:
    branches: [ "main" ]
    tags:
      - v[0-9]+.[0-9]+.[0-9]+.[0-9]+
      - v[0-9]+.[0-9]+.[0-9]+
      - v[0-9]+.[0-9]+
  pull_request:
    branches: [ "main" ]

env:
  GO_VERSION: "1.22.5"

jobs:
  build-and-push-frontend:
    name: Build and Push Frontend Image
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Get branch names.
        id: branch-names
        uses: tj-actions/branch-names@v8

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

      - name: Log in to DockerHub
        if: ${{ github.event_name != 'pull_request' }}
        uses: docker/login-action@v3.1.0
        with:
          username: ${{ secrets.DOCKERHUB_TOKEN }}
          password: ${{ secrets.DOCKERHUB_PASSWD }}

      - name: Build and push frontend image
        uses: docker/build-push-action@v6
        with:
          platforms: linux/amd64,linux/arm64
          push: ${{ github.event_name != 'pull_request' }}
          tags: projecthami/hami-webui-fe-oss:${{ steps.branch-names.outputs.tag || steps.branch-names.outputs.current_branch == 'main' && 'main' || format('pr-{0}', github.run_number) }}

  build-and-push-backend:
    name: Build and Push Backend Image
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2


      - name: Get branch names.
        id: branch-names
        uses: tj-actions/branch-names@v8

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

      - name: Log in to DockerHub
        if: ${{ github.event_name != 'pull_request' }}
        uses: docker/login-action@v3.1.0
        with:
          username: ${{ secrets.DOCKERHUB_TOKEN }}
          password: ${{ secrets.DOCKERHUB_PASSWD }}

      - name: Build and push backend image
        uses: docker/build-push-action@v6
        with:
          context: ./server
          platforms: linux/amd64,linux/arm64
          push: ${{ github.event_name != 'pull_request' }}
          tags: youys/hami-webui-be-oss:${{ steps.branch-names.outputs.tag || steps.branch-names.outputs.current_branch == 'main' && 'main' || format('pr-{0}', github.run_number) }}
