name: build-and-push

run-name: 构建镜像并推送仓库 ${{ github.event.inputs.dockerImageTag }} (${{ github.event.inputs.registry }})

on:
  workflow_dispatch:
    inputs:
      dockerImageTag:
        description: 'Image Tag'
        default: 'v1.10.7-dev'
        required: true
      dockerImageTagWithLatest:
        description: '是否发布latest tag（正式发版时选择，测试版本切勿选择）'
        default: false
        required: true
        type: boolean
      architecture:
        description: 'Architecture'
        required: true
        default: 'linux/amd64'
        type: choice
        options:
          - linux/amd64
          - linux/arm64
          - linux/amd64,linux/arm64
      registry:
        description: 'Push To Registry'
        required: true
        default: 'fit2cloud-registry'
        type: choice
        options:
          - fit2cloud-registry
          - dockerhub
          - dockerhub, fit2cloud-registry

jobs:
  build-and-push-to-fit2cloud-registry:
    if: ${{ contains(github.event.inputs.registry, 'fit2cloud') }}
    runs-on: ubuntu-latest
    steps:
      - name: Check Disk Space
        run: df -h
      - name: Free Disk Space (Ubuntu)
        uses: jlumbroso/free-disk-space@main
        with:
          tool-cache: true
          android: true
          dotnet: true
          haskell: true
          large-packages: true
          docker-images: true
          swap-storage: true
      - name: Check Disk Space
        run: df -h
      - name: Checkout
        uses: actions/checkout@v4
        with:
          ref: ${{ github.ref_name }}
      - name: Prepare
        id: prepare
        run: |
          DOCKER_IMAGE=${{ secrets.FIT2CLOUD_REGISTRY_HOST }}/maxkb/maxkb
          DOCKER_PLATFORMS=${{ github.event.inputs.architecture }}
          TAG_NAME=${{ github.event.inputs.dockerImageTag }}
          TAG_NAME_WITH_LATEST=${{ github.event.inputs.dockerImageTagWithLatest }}
          if [[ ${TAG_NAME_WITH_LATEST} == 'true' ]]; then            
            DOCKER_IMAGE_TAGS="--tag ${DOCKER_IMAGE}:${TAG_NAME} --tag ${DOCKER_IMAGE}:${TAG_NAME%%.*}"
          else
            DOCKER_IMAGE_TAGS="--tag ${DOCKER_IMAGE}:${TAG_NAME}"
          fi
          echo ::set-output name=buildx_args::--platform ${DOCKER_PLATFORMS} --memory-swap -1 \
            --build-arg DOCKER_IMAGE_TAG=${{ github.event.inputs.dockerImageTag }} --build-arg BUILD_AT=$(TZ=Asia/Shanghai date +'%Y-%m-%dT%H:%M') --build-arg GITHUB_COMMIT=`git rev-parse --short HEAD` --no-cache \
            ${DOCKER_IMAGE_TAGS} .
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      - name: Login to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GH_TOKEN }}
      - name: Login to FIT2CLOUD Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ secrets.FIT2CLOUD_REGISTRY_HOST }}
          username: ${{ secrets.FIT2CLOUD_REGISTRY_USERNAME }}
          password: ${{ secrets.FIT2CLOUD_REGISTRY_PASSWORD }}
      - name: Docker Buildx (build-and-push)
        run: |
          sudo sync && echo 3 | sudo tee /proc/sys/vm/drop_caches && free -m
          docker buildx build --output "type=image,push=true" ${{ steps.prepare.outputs.buildx_args }} -f installer/Dockerfile

  build-and-push-to-dockerhub:
    if: ${{ contains(github.event.inputs.registry, 'dockerhub') }}
    runs-on: ubuntu-latest
    steps:
      - name: Check Disk Space
        run: df -h
      - name: Free Disk Space (Ubuntu)
        uses: jlumbroso/free-disk-space@main
        with:
          tool-cache: true
          android: true
          dotnet: true
          haskell: true
          large-packages: true
          docker-images: true
          swap-storage: true
      - name: Check Disk Space
        run: df -h
      - name: Checkout
        uses: actions/checkout@v4
        with:
          ref: ${{ github.ref_name }}
      - name: Prepare
        id: prepare
        run: |
          DOCKER_IMAGE=1panel/maxkb
          DOCKER_PLATFORMS=${{ github.event.inputs.architecture }}
          TAG_NAME=${{ github.event.inputs.dockerImageTag }}          
          TAG_NAME_WITH_LATEST=${{ github.event.inputs.dockerImageTagWithLatest }}
          if [[ ${TAG_NAME_WITH_LATEST} == 'true' ]]; then            
            DOCKER_IMAGE_TAGS="--tag ${DOCKER_IMAGE}:${TAG_NAME} --tag ${DOCKER_IMAGE}:${TAG_NAME%%.*}"
          else
            DOCKER_IMAGE_TAGS="--tag ${DOCKER_IMAGE}:${TAG_NAME}"
          fi
          echo ::set-output name=buildx_args::--platform ${DOCKER_PLATFORMS} --memory-swap -1 \
            --build-arg DOCKER_IMAGE_TAG=${{ github.event.inputs.dockerImageTag }} --build-arg BUILD_AT=$(TZ=Asia/Shanghai date +'%Y-%m-%dT%H:%M') --build-arg GITHUB_COMMIT=`git rev-parse --short HEAD` --no-cache \
            ${DOCKER_IMAGE_TAGS} .
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      - name: Login to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GH_TOKEN }}
      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: Docker Buildx (build-and-push)
        run: |
          sudo sync && echo 3 | sudo tee /proc/sys/vm/drop_caches && free -m
          docker buildx build --output "type=image,push=true" ${{ steps.prepare.outputs.buildx_args }} -f installer/Dockerfile
