name: Build

on:
  pull_request:
    branches:
      - 'develop'
      - 'main'

concurrency:
  group: mizu-pr-validation-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build-cli:
    name: CLI executable build
    runs-on: ubuntu-latest
    steps:
      - name: Check out code into the Go module directory
        uses: actions/checkout@v2
        with:
          fetch-depth: 2
      
      - name: Check modified files
        id: modified_files
        run: devops/check_modified_files.sh cli/

      - name: Set up Go 1.17
        if: steps.modified_files.outputs.matched == 'true'
        uses: actions/setup-go@v2
        with:
          go-version: '1.17'

      - name: Build CLI
        if: steps.modified_files.outputs.matched == 'true'
        run: make cli

  build-agent:
    name: Agent docker image build
    runs-on: ubuntu-latest
    steps:
      - name: Check out code into the Go module directory
        uses: actions/checkout@v2
        with:
          fetch-depth: 2
      
      - name: Check modified files
        id: modified_files
        run: devops/check_modified_files.sh agent/ shared/ tap/ ui/ Dockerfile

      - name: Set up Docker Buildx
        if: steps.modified_files.outputs.matched == 'true'
        uses: docker/setup-buildx-action@v1

      - name: Build
        uses: docker/build-push-action@v2
        if: steps.modified_files.outputs.matched == 'true'
        with:
          context: .
          push: false
          tags: up9inc/mizu:devlatest
          cache-from: type=gha
          cache-to: type=gha,mode=max
