name: kata deploy build

on: 
  pull_request:
    types:
      - opened
      - edited
      - reopened
      - synchronize
    paths:
      - tools/**
      - versions.yaml

jobs:
  build-asset:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        asset:
          - kernel
          - kernel-dragonball-experimental
          - shim-v2
          - qemu
          - cloud-hypervisor
          - firecracker
          - rootfs-image
          - rootfs-initrd
          - virtiofsd
          - nydus
    steps:
      - uses: actions/checkout@v2
      - name: Build ${{ matrix.asset }}
        if: ${{ !contains(github.event.pull_request.labels.*.name, 'force-skip-ci') }}
        run: |
          make "${KATA_ASSET}-tarball"
          build_dir=$(readlink -f build)
          # store-artifact does not work with symlink
          sudo cp -r --preserve=all "${build_dir}" "kata-build"
        env:
          KATA_ASSET: ${{ matrix.asset }}

      - name: store-artifact ${{ matrix.asset }}
        if: ${{ !contains(github.event.pull_request.labels.*.name, 'force-skip-ci') }}
        uses: actions/upload-artifact@v2
        with:
          name: kata-artifacts
          path: kata-build/kata-static-${{ matrix.asset }}.tar.xz
          if-no-files-found: error

  create-kata-tarball:
    runs-on: ubuntu-latest
    needs: build-asset
    steps:
      - uses: actions/checkout@v2
      - name: get-artifacts
        if: ${{ !contains(github.event.pull_request.labels.*.name, 'force-skip-ci') }}
        uses: actions/download-artifact@v2
        with:
          name: kata-artifacts
          path: build
      - name: merge-artifacts
        if: ${{ !contains(github.event.pull_request.labels.*.name, 'force-skip-ci') }}
        run: |
          make merge-builds
      - name: store-artifacts
        if: ${{ !contains(github.event.pull_request.labels.*.name, 'force-skip-ci') }}
        uses: actions/upload-artifact@v2
        with:
          name: kata-static-tarball
          path: kata-static.tar.xz

  make-kata-tarball:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: make kata-tarball
        if: ${{ !contains(github.event.pull_request.labels.*.name, 'force-skip-ci') }}
        run: |
          make kata-tarball
          sudo make install-tarball
