#
# 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-dev-container

on:
  workflow_dispatch:
  schedule:
    # Run weekly on Tuesdays
    - cron: "22 03 * * 2"

jobs:
  build:
    name: build dev container
    runs-on: ubuntu-22.04
    env:
      DOCKER_REGISTRY: ${{ secrets.DOCKER_REGISTRY }}
    steps:
      - name: Check out code into the Go module directory
        uses: actions/checkout@v4
      - name: Set REPO_OWNER
        shell: bash
        run: |
          REPO_OWNER=${{ github.repository_owner }}
          # Lowercase the value
          echo "REPO_OWNER=${REPO_OWNER,,}" >>${GITHUB_ENV}
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3
        with:
          image: tonistiigi/binfmt:latest
          platforms: arm64
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      - name: Docker Hub login
        if: github.event_name != 'pull_request'
        run: |
          docker login -u ${{ secrets.DOCKER_REGISTRY_ID }} -p ${{ secrets.DOCKER_REGISTRY_PASS }}
      - name: GitHub Container Registry login
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Build multi-arch dev container
        run: make build-dev-container-all-arch
      - name: Push multi-arch dev container to Docker Hub
        if: github.event_name != 'pull_request'
        run: make push-dev-container-all-arch DAPR_REGISTRY=${{ env.DOCKER_REGISTRY }}
      # If the images were built for Docker Hub, this step is cached
      - name: Push multi-arch dev container to GitHub Container Registry
        if: github.event_name != 'pull_request'
        run: make push-dev-container-all-arch DAPR_REGISTRY="ghcr.io/${{ env.REPO_OWNER }}"
