env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}

name: Build and publish windmill for RHEL8
on: workflow_dispatch

permissions: write-all

jobs:
  build_ee:
    runs-on: ubicloud
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Read EE repo commit hash
        run: |
          echo "ee_repo_ref=$(cat ./backend/ee-repo-ref.txt)" >> "$GITHUB_ENV"

      - uses: actions/checkout@v4
        with:
          repository: windmill-labs/windmill-ee-private
          path: ./windmill-ee-private
          ref: ${{ env.ee_repo_ref }}
          token: ${{ secrets.WINDMILL_EE_PRIVATE_ACCESS }}
          fetch-depth: 0

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

      - name: Docker meta
        id: meta-ee-public
        uses: docker/metadata-action@v5
        with:
          images: |
            ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-ee-rhel8
          flavor: |
            latest=false
          tags: |
            type=sha

      - name: Login to registry
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Substitute EE code
        run: |
          ./backend/substitute_ee_code.sh --copy --dir ./windmill-ee-private

      - name: Copy RHEL8 Dockerfile
        run: |
          cp ./docker/RHEL8/Dockerfile ./Dockerfile

      - name: Build and push publicly ee amd64
        uses: depot/build-push-action@v1
        with:
          context: .
          platforms: linux/amd64
          push: true
          build-args: |
            features=enterprise,enterprise_saml,stripe,embedding,parquet,prometheus,openidconnect,cloud,jemalloc,license,otel,http_trigger,zip,oauth2,kafka,sqs_trigger,nats,postgres_trigger,gcp_trigger,mqtt_trigger,websocket,smtp,native_trigger,static_frontend,all_languages,deno_core,mcp,private
          secrets: |
            rh_username=${{ secrets.RH_USERNAME }}
            rh_password=${{ secrets.RH_PASSWORD }}
          tags: |
            ${{ steps.meta-ee-public.outputs.tags }}-amd64
          labels: |
            ${{ steps.meta-ee-public.outputs.labels }}-amd64
            org.opencontainers.image.licenses=Windmill-Enterprise-License

      - name: Build and push publicly ee arm64
        uses: depot/build-push-action@v1
        with:
          context: .
          platforms: linux/arm64
          push: true
          build-args: |
            features=enterprise,enterprise_saml,stripe,embedding,parquet,prometheus,openidconnect,cloud,jemalloc,license,otel,http_trigger,zip,oauth2,kafka,sqs_trigger,nats,postgres_trigger,gcp_trigger,mqtt_trigger,websocket,smtp,native_trigger,static_frontend,all_languages,deno_core,mcp,private
          secrets: |
            rh_username=${{ secrets.RH_USERNAME }}
            rh_password=${{ secrets.RH_PASSWORD }}
          tags: |
            ${{ steps.meta-ee-public.outputs.tags }}-arm64
          labels: |
            ${{ steps.meta-ee-public.outputs.labels }}-arm64
            org.opencontainers.image.licenses=Windmill-Enterprise-License

      - uses: shrink/actions-docker-extract@v3
        id: extract-ee-amd64
        with:
          image: ${{ steps.meta-ee-public.outputs.tags}}-amd64
          path: "/windmill/target/release/windmill"

      - uses: shrink/actions-docker-extract@v3
        id: extract-duckdb-ffi-internal
        with:
          image: ${{ steps.meta-ee-public.outputs.tags}}-amd64
          path: "/usr/src/app/libwindmill_duckdb_ffi_internal.so"

      # - uses: shrink/actions-docker-extract@v3
      #   id: extract-ee-arm64
      #   with:
      #     image: ${{ steps.meta-ee-public.outputs.tags}}-arm64
      #     path: "/windmill/target/release/windmill"

      - name: Rename binary with corresponding architecture
        run: |
          mv "${{ steps.extract-ee-amd64.outputs.destination }}/windmill" "${{ steps.extract-ee-amd64.outputs.destination }}/windmill-ee-amd64-rhel8"
          # mv "${{ steps.extract-ee-arm64.outputs.destination }}/windmill" "${{ steps.extract-ee-arm64.outputs.destination }}/windmill-ee-arm64-rhel8"

      - uses: actions/upload-artifact@v4
        with:
          name: RHEL8-amd64 build
          path: ${{ steps.extract-ee-amd64.outputs.destination }}/windmill-ee-amd64-rhel8

      - uses: actions/upload-artifact@v4
        with:
          name: RHEL8-amd64 dynamic libraries build
          path: ${{ steps.extract-duckdb-ffi-internal.outputs.destination }}/libwindmill_duckdb_ffi_internal.so

      # - uses: actions/upload-artifact@v4
      #   with:
      #     name: RHEL8-arm64 build
      #     path:
      #       ${{ steps.extract-ee-arm64.outputs.destination
      #       }}/windmill-ee-arm64-rhel8

      # - name: Attach binary to release
      #   uses: softprops/action-gh-release@v2
      #   if: startsWith(github.ref, 'refs/tags/')
      #   with:
      #     files: |
      #       ${{ steps.extract-ee-arm64.outputs.destination }}/windmill-ee-arm64-rhel8
      #       ${{ steps.extract-ee-amd64.outputs.destination }}/windmill-ee-amd64-rhel8
