name: Multi Arch QEMU CI

on:
  workflow_dispatch:  
  push:
    branches:
      - 'releases/v*'
      - 'develop'
    tags:
      - 'v*.*.*'

concurrency:
  group: ${{ github.ref }}
  cancel-in-progress: true

jobs:
  docker:
    runs-on: [self-hosted, qemu]
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

      - name: Set the environment variable
        run: echo REPOSITORY_NAME=${GITHUB_REPOSITORY#*\/} >> $GITHUB_ENV

      - name: Docker meta
        id: meta
        uses: docker/metadata-action@v4     
        with:
          images: |
            "${{ secrets.DOCKERHUB_USERNAME }}/${{ env.REPOSITORY_NAME }}"
          tags: |
            type=ref,event=branch
            type=ref,event=pr
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=semver,pattern={{major}}
          labels: |
            org.opencontainers.image.description=A test for Docker CI with Github Actions.            

      - name: Login to DockerHub
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Login to Private registry
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v2
        with:
          registry: ${{ secrets.PRIVATEHUB_HOSTNAME }}
          username: ${{ secrets.PRIVATEHUB_USERNAME }}
          password: ${{ secrets.PRIVATEHUB_TOKEN }}          

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
        with:       
          config-inline: |
            [registry."docker.io"]
              mirrors = ["${{ secrets.DOCKERHUB_PROXY_HOSTNAME }}"]
            [registry."${{ secrets.DOCKERHUB_PROXY_HOSTNAME }}"]
              http = true              
              insecure = true
            [registry."${{ secrets.PRIVATEHUB_HOSTNAME }}"]
              http = true
              insecure = true
          driver-opts: |
            network=host
          buildkitd-flags: |
            --debug 
            --allow-insecure-entitlement network.host
            --allow-insecure-entitlement security.insecure  

      - name: Build and push
        uses: docker/build-push-action@v4
        with:
          context: .
          platforms: linux/amd64,linux/arm64/v8
#          platforms: linux/amd64
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          cache-from: type=registry,ref=${{ secrets.PRIVATEHUB_HOSTNAME }}/${{ env.REPOSITORY_NAME }}:buildcache
          cache-to: type=registry,ref=${{ secrets.PRIVATEHUB_HOSTNAME }}/${{ env.REPOSITORY_NAME }}:buildcache,mode=min,compression=zstd

      - name: Update repo description
        uses: peter-evans/dockerhub-description@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
          repository: "${{ secrets.DOCKERHUB_USERNAME }}/${{ env.REPOSITORY_NAME }}"          
