#
# 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: Build Quickstarts

on:
  push:
    branches:
      - master
      - feature/new_quickstarts
      - release-*
    tags:
      - v*
    paths-ignore: 
      - '**.md'
  workflow_dispatch:
jobs:
  build:
    name: Build ${{ matrix.target_os }}_${{ matrix.target_arch }} binaries
    runs-on: ${{ matrix.os }}
    env:
      TARGET_OS: ${{ matrix.target_os }}
      TARGET_ARCH: ${{ matrix.target_arch }}
      DOCKER_CLI_EXPERIMENTAL: enabled
      SAMPLE_REGISTRY: ${{ vars.SAMPLE_REGISTRY }}
    strategy:
      matrix:
        os: [ubuntu-latest]
        target_arch: [arm, arm64, amd64]
        include:
          - os: ubuntu-latest
            target_os: linux
    steps:
      - name: Check Docker version
        run: docker --version
      - name: Set REPO_OWNER
        run: |
          REPO_OWNER=${{ github.repository_owner }}
          # Lowercase the value
          echo "REPO_OWNER=${REPO_OWNER,,}" >>${GITHUB_ENV}
        shell: bash
      - name: Check out code
        uses: actions/checkout@v2
      - name: Parse release version and set REL_VERSION
        run: python ./.github/scripts/get_release_version.py
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v1
        if: ${{ matrix.target_arch == 'arm' || matrix.target_arch == 'arm64' }}
        with:
          image: tonistiigi/binfmt:latest
          platforms: arm,arm64
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
        with:
          version: v0.9.1 # Don't use latest since it broke our workflow once
      - name: Build docker image
        run: | 
          SAMPLE_LIST=(tutorials/hello-kubernetes tutorials/distributed-calculator tutorials/pub-sub tutorials/bindings tutorials/observability tutorials/secretstore)
          for sample in "${SAMPLE_LIST[@]}"; do
            echo "Building image for ${sample}..."
            pushd ${sample}
            make build
            popd
          done
      - name: docker login
        run: |
          docker login -u ${{ secrets.DOCKER_REGISTRY_ID }} -p ${{ secrets.DOCKER_REGISTRY_PASS }}
      - name: publish image to dockerhub
        run: |
          SAMPLE_LIST=(tutorials/hello-kubernetes tutorials/distributed-calculator tutorials/pub-sub tutorials/bindings tutorials/observability tutorials/secretstore)
          for sample in "${SAMPLE_LIST[@]}"; do
            echo "Push image for ${sample}..."
            pushd ${sample}
            make push
            popd
          done
      - name: GitHub container registry login
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v1
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Build docker image
        env:
          SAMPLE_REGISTRY: ghcr.io/${{ env.REPO_OWNER }}/samples
        run: | 
          SAMPLE_LIST=(tutorials/hello-kubernetes tutorials/distributed-calculator tutorials/pub-sub tutorials/bindings tutorials/observability tutorials/secretstore)
          for sample in "${SAMPLE_LIST[@]}"; do
            echo "Building image for ${sample}..."
            pushd ${sample}
            make build
            popd
          done
      - name: publish image to ghcr
        if: github.event_name != 'pull_request'
        env:
          SAMPLE_REGISTRY: ghcr.io/${{ env.REPO_OWNER }}/samples
        run: |
          SAMPLE_LIST=(tutorials/hello-kubernetes tutorials/distributed-calculator tutorials/pub-sub tutorials/bindings tutorials/observability tutorials/secretstore)
          for sample in "${SAMPLE_LIST[@]}"; do
            echo "Push image for ${sample}..."
            pushd ${sample}
            make push
            popd
          done
  publish:
    name: Publish docker manifest
    needs: build
    runs-on: ubuntu-latest
    env:
      DOCKER_CLI_EXPERIMENTAL: enabled
      SAMPLE_REGISTRY: ${{ vars.SAMPLE_REGISTRY }}
    steps:
      - name: Set REPO_OWNER
        run: |
          REPO_OWNER=${{ github.repository_owner }}
          # Lowercase the value
          echo "REPO_OWNER=${REPO_OWNER,,}" >>${GITHUB_ENV}
      - name: Check out code
        uses: actions/checkout@v2
      - name: Parse release version and set REL_VERSION
        run: python ./.github/scripts/get_release_version.py
      - name: docker login
        run: |
          docker login -u ${{ secrets.DOCKER_REGISTRY_ID }} -p ${{ secrets.DOCKER_REGISTRY_PASS }}
      - name: create docker manifest
        run: |
          SAMPLE_LIST=(tutorials/hello-kubernetes tutorials/distributed-calculator tutorials/pub-sub tutorials/bindings tutorials/observability tutorials/secretstore)
          for sample in "${SAMPLE_LIST[@]}"; do
            echo "Building image for ${sample}..."
            pushd ${sample}
            make manifest-create
            popd
          done
      - name: publish manifest to dockerhub
        run: |
          SAMPLE_LIST=(tutorials/hello-kubernetes tutorials/distributed-calculator tutorials/pub-sub tutorials/bindings tutorials/observability tutorials/secretstore)
          for sample in "${SAMPLE_LIST[@]}"; do
            echo "Push image for ${sample}..."
            pushd ${sample}
            make manifest-push
            popd
          done
      - name: GitHub container registry login
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v1
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: create ghcr manifest
        if: github.event_name != 'pull_request'
        env: 
          SAMPLE_REGISTRY: ghcr.io/${{ env.REPO_OWNER }}/samples
        run: |
          SAMPLE_LIST=(tutorials/hello-kubernetes tutorials/distributed-calculator tutorials/pub-sub tutorials/bindings tutorials/observability tutorials/secretstore)
          for sample in "${SAMPLE_LIST[@]}"; do
            echo "Building image for ${sample}..."
            pushd ${sample}
            make manifest-create
            popd
          done
      - name: publish manifest to ghcr
        if: github.event_name != 'pull_request'
        env: 
          SAMPLE_REGISTRY: ghcr.io/${{ env.REPO_OWNER }}/samples
        run: |
          SAMPLE_LIST=(tutorials/hello-kubernetes tutorials/distributed-calculator tutorials/pub-sub tutorials/bindings tutorials/observability tutorials/secretstore)
          for sample in "${SAMPLE_LIST[@]}"; do
            echo "Push image for ${sample}..."
            pushd ${sample}
            make manifest-push
            popd
          done
