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: v1.0.${{ github.run_number }}
          release_name: Release v1.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@v1
      - name: Set up Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.17

      - name: Test
        run: go test -v ./...

      - name: Build
        env:
          RELEASE: v1.0.${{ github.run_number }} 
          ArmoBEServer: api.armo.cloud
          ArmoERServer: report.armo.cloud
          ArmoWebsite: portal.armo.cloud
          CGO_ENABLED: 0
        run: python3 --version && python3 build.py
      
      - 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


  build-docker:
    name: Build docker container, tag and upload to registry
    needs: build
    if: ${{ github.repository == 'armosec/kubescape' }}
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Set name
        run: echo quay.io/armosec/kubescape:v1.0.${{ github.run_number }} > build_tag.txt

      - name: Build the Docker image
        run: docker build . --file build/Dockerfile --tag $(cat build_tag.txt)
      
      - name: Re-Tag Image to latest
        run: docker tag $(cat build_tag.txt) quay.io/armosec/kubescape:latest

      - name: Login to Quay.io
        env: # Or as an environment variable
          QUAY_PASSWORD: ${{ secrets.QUAYIO_REGISTRY_PASSWORD }}
          QUAY_USERNAME: ${{ secrets.QUAYIO_REGISTRY_USERNAME }}
        run: docker login -u="${QUAY_USERNAME}" -p="${QUAY_PASSWORD}" quay.io
      - name: Push Docker image
        run: |
          docker push $(cat build_tag.txt)
          docker push quay.io/armosec/kubescape:latest
        
