name: build-dev

on:
  push:
    branches: [ dev ]
jobs:
  build:
    name: Create cross-platform dev build
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: recursive
      
      - name: Cache Go modules (Linux)
        if: matrix.os == 'ubuntu-latest' 
        uses: actions/cache@v3
        with:
          path: |
            ~/.cache/go-build
            ~/go/pkg/mod
          key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-

      - name: Cache Go modules (macOS)
        if: matrix.os == 'macos-latest' 
        uses: actions/cache@v3
        with:
          path: |
            ~/Library/Caches/go-build
            ~/go/pkg/mod
          key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-

      - name: Cache Go modules (Windows)
        if: matrix.os == 'windows-latest'
        uses: actions/cache@v3
        with:
          path: |
            ~\AppData\Local\go-build
            ~\go\pkg\mod
          key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
          restore-keys: |
            ${{ runner.os }}-go-

      - name: Set up Go
        uses: actions/setup-go@v3
        with:
          go-version: 1.18
          
      # - name: Test cmd pkg
      #   run: cd cmd && go test -v ./...
      
      # - name: Test core pkg
      #   env:
      #     GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      #   run: cd core && go test -v ./...

      # - name: Test cmd pkg
      #   run: cd cmd && go test -v ./...

      - name: Install MSYS2 & libgit2 (Windows)
        shell: cmd
        run: .\build.bat all
        if: matrix.os == 'windows-latest'

      - name: Install libgit2 (Linux/macOS)
        run: make libgit2
        if: matrix.os != 'windows-latest'

      - name: Test core pkg
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: go test -tags=static -v ./...

      - name: Test httphandler pkg
        run: cd httphandler && go test -tags=static -v ./...

      - name: Build
        env:
          RELEASE: v2.0.${{ github.run_number }} 
          CLIENT: release-dev
          ArmoBEServer: api.armo.cloud
          ArmoAuthServer: auth.armo.cloud
          ArmoERServer: report.armo.cloud
          ArmoWebsite: portal.armo.cloud
          CGO_ENABLED: 1
        run: python3 --version && python3 build.py
      
      - name: Smoke Testing
        env:
          RELEASE: v2.0.${{ github.run_number }}
          KUBESCAPE_SKIP_UPDATE_CHECK: "true"
        run: python3 smoke_testing/init.py ${PWD}/build/${{ matrix.os }}/kubescape

      - name: Upload build artifacts 
        uses: actions/upload-artifact@v2
        with:
          name: kubescape-${{ matrix.os }}
          path: build/${{ matrix.os }}/kubescape 

  build-docker:
    name: Build docker container, tag and upload to registry
    needs: build
    if: ${{ github.repository == 'armosec/kubescape' }} # TODO
    runs-on: ubuntu-latest
    permissions:
      id-token: write
      packages: write
      contents: read

    steps:
      - uses: actions/checkout@v2
        with:
          submodules: recursive

      - name: Set image version
        id: image-version
        run: echo '::set-output name=IMAGE_VERSION::dev-v2.0.${{ github.run_number }}'

      - name: Set image name
        id: image-name
        run: echo '::set-output name=IMAGE_NAME::quay.io/${{ github.repository_owner }}/kubescape'

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

      - name: Login to Quay.io
        env:
          QUAY_PASSWORD: ${{ secrets.QUAYIO_REGISTRY_PASSWORD }}
          QUAY_USERNAME: ${{ secrets.QUAYIO_REGISTRY_USERNAME }}
        run: docker login -u="${QUAY_USERNAME}" -p="${QUAY_PASSWORD}" quay.io

      - name: Build the Docker image
        run: docker buildx build . --file build/Dockerfile --tag ${{ steps.image-name.outputs.IMAGE_NAME }}:${{ steps.image-version.outputs.IMAGE_VERSION }} --build-arg image_version=${{ steps.image-version.outputs.IMAGE_VERSION }} --build-arg client=image-dev --push --platform linux/amd64,linux/arm64
