name: Kubernetes

on:
  workflow_dispatch: {}
#  push: { branches: [main] }
#  pull_request:
#    paths-ignore:
#      - '.github/**'

env:
  GKE_ZONE: 'us-central1-c'
  GKE_REGION: 'us-central1'
  GKE_PROJECT: 'unitykubernetesbuilder'
  GKE_CLUSTER: 'unity-builder-cluster'
  UNITY_LICENSE: "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root>\n    <License id=\"Terms\">\n        <MachineBindings>\n            <Binding Key=\"1\" Value=\"576562626572264761624c65526f7578\"/>\n            <Binding Key=\"2\" Value=\"576562626572264761624c65526f7578\"/>\n        </MachineBindings>\n        <MachineID Value=\"D7nTUnjNAmtsUMcnoyrqkgIbYdM=\"/>\n        <SerialHash Value=\"2033b8ac3e6faa3742ca9f0bfae44d18f2a96b80\"/>\n        <Features>\n            <Feature Value=\"33\"/>\n            <Feature Value=\"1\"/>\n            <Feature Value=\"12\"/>\n            <Feature Value=\"2\"/>\n            <Feature Value=\"24\"/>\n            <Feature Value=\"3\"/>\n            <Feature Value=\"36\"/>\n            <Feature Value=\"17\"/>\n            <Feature Value=\"19\"/>\n            <Feature Value=\"62\"/>\n        </Features>\n        <DeveloperData Value=\"AQAAAEY0LUJHUlgtWEQ0RS1aQ1dWLUM1SlctR0RIQg==\"/>\n        <SerialMasked Value=\"F4-BGRX-XD4E-ZCWV-C5JW-XXXX\"/>\n        <StartDate Value=\"2021-02-08T00:00:00\"/>\n        <UpdateDate Value=\"2021-02-09T00:34:57\"/>\n        <InitialActivationDate Value=\"2021-02-08T00:34:56\"/>\n        <LicenseVersion Value=\"6.x\"/>\n        <ClientProvidedVersion Value=\"2018.4.30f1\"/>\n        <AlwaysOnline Value=\"false\"/>\n        <Entitlements>\n            <Entitlement Ns=\"unity_editor\" Tag=\"UnityPersonal\" Type=\"EDITOR\" ValidTo=\"9999-12-31T00:00:00\"/>\n            <Entitlement Ns=\"unity_editor\" Tag=\"DarkSkin\" Type=\"EDITOR_FEATURE\" ValidTo=\"9999-12-31T00:00:00\"/>\n        </Entitlements>\n    </License>\n<Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><SignedInfo><CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments\"/><SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\"/><Reference URI=\"#Terms\"><Transforms><Transform Algorithm=\"http://www.w3.org/2000/09/xmldsig#enveloped-signature\"/></Transforms><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\"/><DigestValue>m0Db8UK+ktnOLJBtHybkfetpcKo=</DigestValue></Reference></SignedInfo><SignatureValue>o/pUbSQAukz7+ZYAWhnA0AJbIlyyCPL7bKVEM2lVqbrXt7cyey+umkCXamuOgsWPVUKBMkXtMH8L\n5etLmD0getWIhTGhzOnDCk+gtIPfL4jMo9tkEuOCROQAXCci23VFscKcrkB+3X6h4wEOtA2APhOY\nB+wvC794o8/82ffjP79aVAi57rp3Wmzx+9pe9yMwoJuljAy2sc2tIMgdQGWVmOGBpQm3JqsidyzI\nJWG2kjnc7pDXK9pwYzXoKiqUqqrut90d+kQqRyv7MSZXR50HFqD/LI69h68b7P8Bjo3bPXOhNXGR\n9YCoemH6EkfCJxp2gIjzjWW+l2Hj2EsFQi8YXw==</SignatureValue></Signature></root>"

jobs:
  k8sBuilds:
    name: K8s build for ${{ matrix.targetPlatform }} on version ${{ matrix.unityVersion }}
    runs-on: ubuntu-latest
    continue-on-error: true
    strategy:
      fail-fast: false
      matrix:
        targetPlatform:
          - StandaloneLinux64
          - StandaloneWindows64
    steps:
      ###########################
      #         Checkout        #
      ###########################
      - uses: actions/checkout@v2
        with:
          lfs: true

      ###########################
      #         Spin up         #
      ###########################
      - uses: GoogleCloudPlatform/github-actions/setup-gcloud@master
        with:
          version: '288.0.0'
          service_account_email: ${{ secrets.GOOGLE_SERVICE_ACCOUNT_EMAIL }}
          service_account_key: ${{ secrets.GOOGLE_SERVICE_ACCOUNT_KEY }}
      - run: ./dist/bootstrapper/ApplyClusterAndAcquireLock.sh ${{ env.GKE_PROJECT }} ${{ env.GKE_CLUSTER }} ${{ env.GKE_ZONE }}

      ###########################
      #          Build          #
      ###########################
      - uses: frostebite/File-To-Base64@master
        id: read-base64
        with:
          filePath: ~/.kube/config
      - uses: ./
        id: k8s-unity-build
        with:
          targetPlatform: ${{ matrix.targetPlatform }}
          kubeConfig: ${{ steps.read-base64.outputs.base64 }}
          githubToken: ${{ secrets.GITHUB_TOKEN }}
          projectPath: test-project
          unityVersion: 2019.3.15f1

      ###########################
      #          Upload         #
      ###########################
      - uses: frostebite/K8s-Download-Volume@master
        with:
          kubeConfig: ${{ steps.read-base64.outputs.base64 }}
          volume: ${{ steps.k8s-unity-build.outputs.volume }}
          sourcePath: repo/build/
      - uses: actions/upload-artifact@v2
        with:
          name: Kubernetes Build (${{ matrix.targetPlatform }})
          path: k8s-volume-download
          retention-days: 14

      ###########################
      #        Spin down        #
      ###########################
      - run: ./dist/bootstrapper/ReleaseLockAndAttemptShutdown.sh ${{ env.GKE_PROJECT }} ${{ env.GKE_CLUSTER }} ${{ env.GKE_ZONE }}
        if: ${{ always() }}
