name: validate-examples

on:
  push:
    branches:
      - main
    tags:
      - v*
  pull_request:
    branches:
      - main
jobs:
  validate:
    runs-on: ubuntu-latest
    env:
      PYTHON_VER: 3.7
      GOVER: 1.18
      GOOS: linux
      GOARCH: amd64
      GOPROXY: https://proxy.golang.org
      DAPR_INSTALL_URL: https://raw.githubusercontent.com/dapr/cli/master/install/install.sh
      DAPR_CLI_REF: ""
      DAPR_REF: ""
    steps:
      - uses: actions/checkout@v3
      - name: Determine latest Dapr Runtime version
        run: |
          RUNTIME_VERSION=$(curl -s "https://api.github.com/repos/dapr/dapr/releases/latest" | grep '"tag_name"' | cut -d ':' -f2 | tr -d '",v')
          echo "DAPR_RUNTIME_VER=$RUNTIME_VERSION" >> $GITHUB_ENV
          echo "Found $RUNTIME_VERSION"
      - name: Determine latest Dapr Cli version
        run: |
          CLI_VERSION=$(curl -s "https://api.github.com/repos/dapr/cli/releases/latest" | grep '"tag_name"' | cut -d ':' -f2 | tr -d '",v')
          echo "DAPR_CLI_VER=$CLI_VERSION" >> $GITHUB_ENV
          echo "Found $CLI_VERSION"
      - name: Set up Python ${{ env.PYTHON_VER }}
        uses: actions/setup-python@v4
        with:
          python-version: ${{ env.PYTHON_VER }}
      - name: Install Mechanical Markdown
        run: |
          python -m pip install --upgrade pip
          pip install mechanical-markdown
      - name: Set up Dapr CLI
        run: wget -q ${{ env.DAPR_INSTALL_URL }} -O - | /bin/bash -s ${{ env.DAPR_CLI_VER }}
      - name: Set up Go ${{ env.GOVER }}
        if: env.DAPR_REF != '' || env.DAPR_CLI_REF != ''
        uses: actions/setup-go@v3
        with:
          go-version: ${{ env.GOVER }}
      - name: Checkout Dapr CLI repo to override dapr command.
        uses: actions/checkout@v3
        if: env.DAPR_CLI_REF != ''
        with:
          repository: dapr/cli
          ref: ${{ env.DAPR_CLI_REF }}
          path: cli
      - name: Checkout Dapr repo to override daprd.
        uses: actions/checkout@v3
        if: env.DAPR_REF != ''
        with:
          repository: dapr/dapr
          ref: ${{ env.DAPR_REF }}
          path: dapr_runtime
      - name: Build and override dapr cli with referenced commit.
        if: env.DAPR_CLI_REF != ''
        run: |
          cd cli
          make
          sudo cp dist/linux_amd64/release/dapr /usr/local/bin/dapr
          cd ..
      - name: Initialize Dapr runtime ${{ env.DAPR_RUNTIME_VER }}
        run: |
          dapr uninstall --all
          dapr init --runtime-version ${{ env.DAPR_RUNTIME_VER }}
      - name: Build and override daprd with referenced commit.
        if: env.DAPR_REF != ''
        run: |
          cd dapr_runtime
          make
          mkdir -p $HOME/.dapr/bin/
          cp dist/linux_amd64/release/daprd $HOME/.dapr/bin/daprd
          cd ..
      - name: Override placement service.
        if: env.DAPR_REF != ''
        run: |
          docker stop dapr_placement
          cd dapr
          ./dist/linux_amd64/release/placement --healthz-port 9091 &
      - name: Check Examples
        run: |
          cd examples
          ./validate.sh hello-world
          ./validate.sh pubsub
          ./validate.sh service
          ./validate.sh actor
