name: EstablishDockerImage

on:
  push:
    branches:
      - main
      - dev
      - 'feat/**'
    tags:
      - '*'

env:
  IMAGE_FQDN: registry.cn-hangzhou.aliyuncs.com/playedu/api
  IMAGE_TAG: ''

jobs:
  build-and-push:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
      - name: Login to GHCR
        uses: docker/login-action@v2
        with:
          registry: registry.cn-hangzhou.aliyuncs.com
          username: ${{ secrets.ALI_REGISTRY_EMAIL }}
          password: ${{ secrets.ALI_REGISTRY_PASS }}
      - name: Build
        uses: docker/build-push-action@v3
        with:
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          tags: |
            ${{ env.IMAGE_FQDN }}:${{ env.IMAGE_TAG }}
        env:
          IMAGE_TAG: ${{ startsWith(github.ref, 'refs/heads/main') && 'latest' || startsWith(github.ref, 'refs/heads/dev') && 'dev' || github.ref_name }}
        if: startsWith(github.ref, 'refs/heads/')
      - name: Build with Tag
        uses: docker/build-push-action@v3
        with:
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          tags: |
            ${{ env.IMAGE_FQDN }}:${{ github.ref_name }}
        if: startsWith(github.ref, 'refs/tags/')
