#
# Copyright 2021 The Dapr Authors
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#     http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

name: dapr-base-containers

on:
  # Manual trigger
  workflow_dispatch:
  # Dispatch on external events
  repository_dispatch:
    types: [windows-base]
jobs:
  build:
    strategy:
      matrix:
        include:
          - os: "windows-2019"
            windows-version: "1809"
          - os: "windows-2022"
            windows-version: "ltsc2022"
    name: Build base Windows images
    runs-on: ${{ matrix.os }}
    env:
      WINDOWS_VERSION: ${{ matrix.windows-version }}
      TARGET_OS: windows
    steps:
      - name: Set up for manual runs
        if: github.event_name != 'repository_dispatch'
        run: |
          echo "CHECKOUT_REPO=${{ github.repository }}" >> $GITHUB_ENV
          echo "CHECKOUT_REF=refs/heads/master" >> $GITHUB_ENV
        shell: bash
      - name: Parse event payload
        if: github.event_name == 'repository_dispatch'
        uses: actions/github-script@0.3.0
        with:
          github-token: ${{secrets.DAPR_BOT_TOKEN}}
          script: |
            const testPayload = context.payload.client_payload;
            if (testPayload && testPayload.command == "windows-base") {
              var fs = require('fs');
              // Set environment variables
              fs.appendFileSync(process.env.GITHUB_ENV,
                `CHECKOUT_REPO=${testPayload.pull_head_repo}\n`+
                `CHECKOUT_REF=${testPayload.pull_head_ref}`);
            }
      - name: Check out dapr
        if: env.CHECKOUT_REPO != ''
        uses: actions/checkout@v4
        with:
          repository: ${{ env.CHECKOUT_REPO }}
          ref: ${{ env.CHECKOUT_REF }}
      - name: Set REPO_OWNER
        shell: bash
        run: |
          REPO_OWNER=${{ github.repository_owner }}
          # Lowercase the value
          echo "REPO_OWNER=${REPO_OWNER,,}" >>${GITHUB_ENV}
      - name: Docker Hub login
        if: env.CHECKOUT_REPO != '' && env.DAPR_REGISTRY != ''
        env:
          DAPR_REGISTRY: ${{ secrets.DOCKER_REGISTRY }}
        run: |
          docker login -u ${{ secrets.DOCKER_REGISTRY_ID }} -p ${{ secrets.DOCKER_REGISTRY_PASS }}
      - name: GitHub container registry login
        if: env.CHECKOUT_REPO != ''
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Build Windows base images for Docker Hub
        if: env.CHECKOUT_REPO != '' && env.DAPR_REGISTRY != ''
        env:
          DAPR_REGISTRY: ${{ secrets.DOCKER_REGISTRY }}
        run: |
          echo "Build docker image..."
          make docker-windows-base-build
      - name: Push Windows base images to Docker Hub
        if: env.CHECKOUT_REPO != '' && env.DAPR_REGISTRY != ''
        env:
          DAPR_REGISTRY: ${{ secrets.DOCKER_REGISTRY }}
        run: |
          echo "Push docker image..."
          make docker-windows-base-push
      # If the images were built for Docker Hub, this step is cached
      - name: Build Windows base images for GHCR
        if: env.CHECKOUT_REPO != ''
        env:
          DAPR_REGISTRY: ghcr.io/${{ env.REPO_OWNER }}
        run: |
          echo "Build docker image..."
          make docker-windows-base-build
      - name: Push Windows base images to GHCR
        if: env.CHECKOUT_REPO != ''
        env:
          DAPR_REGISTRY: ghcr.io/${{ env.REPO_OWNER }}
        run: |
          echo "Push docker image..."
          make docker-windows-base-push
