name: build

on:
  push:
    branches: [ master ]
jobs:
  once:
    name: Create release
    runs-on: ubuntu-latest
    outputs:
      upload_url: ${{ steps.create_release.outputs.upload_url }}
    steps:
      - name: Create a release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: v2.0.${{ github.run_number }}
          release_name: Release v2.0.${{ github.run_number }}
          draft: false
          prerelease: false
  build:
    name: Create cross-platform release build, tag and upload binaries
    needs: once
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
    steps:
      - uses: actions/checkout@v3
      - 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: go test -v ./...

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

      - name: Build
        env:
          RELEASE: v2.0.${{ github.run_number }} 
          CLIENT: release
          ArmoBEServer: api.armo.cloud
          ArmoAuthServer: auth.armo.cloud
          ArmoERServer: report.armo.cloud
          ArmoWebsite: portal.armo.cloud
          CGO_ENABLED: 0
        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 release binaries
        id: upload-release-asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.once.outputs.upload_url }}
          asset_path: build/${{ matrix.os }}/kubescape
          asset_name: kubescape-${{ matrix.os }}
          asset_content_type: application/octet-stream

      - name: Upload release hash
        id: upload-release-hash
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.once.outputs.upload_url }}
          asset_path: build/${{ matrix.os }}/kubescape.sha256
          asset_name: kubescape-${{ matrix.os }}-sha256
          asset_content_type: application/octet-stream
  build-docker:
    name: Build docker container, tag and upload to registry
    needs: build
    runs-on: ubuntu-latest
    if: ${{ github.repository == 'armosec/kubescape' }} # TODO
    permissions:
      id-token: write
      packages: write
      contents: read

    steps:
      - uses: actions/checkout@v2

      - name: Set image version
        id: image-version
        run: echo '::set-output name=IMAGE_VERSION::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 }} --tag ${{ steps.image-name.outputs.IMAGE_NAME }}:latest --build-arg image_version=${{ steps.image-version.outputs.IMAGE_VERSION }} --build-arg client=image-release --push --platform linux/amd64,linux/arm64

      # - name: Login to GitHub Container Registry
      #   uses: docker/login-action@v1
      #   with:
      #     registry: ghcr.io
      #     username: ${{ github.actor }}
      #     password: ${{ secrets.GITHUB_TOKEN }}
    
      # TODO - Wait for casign to support fixed tags -> https://github.com/sigstore/cosign/issues/1424
      # - name: Install cosign
      #   uses: sigstore/cosign-installer@main
      #   with:
      #     cosign-release: 'v1.5.1' # optional
      # - name: sign kubescape container image
      #   env:
      #     COSIGN_EXPERIMENTAL: "true"
      #   run: |
      #       cosign sign --force ${{ steps.image-name.outputs.IMAGE_NAME }}:latest
      #       cosign sign --force ${{ steps.image-name.outputs.IMAGE_NAME }}:${{ steps.image-version.outputs.IMAGE_VERSION }}

