name: "Development deployment"

on:
  push:
    branches:
      - "development"

jobs:
  build-for-development:
    name: "Build"
    uses: "./.gitea/workflows/build-deployment-images.yaml"
    with:
      target: "deployment"
      platform: "linux/amd64"
      registry: "nexus.bthlab.bthlabs.net:8002"
    secrets:
      VAULT_ROLE_ID: "${{ secrets.VAULT_ROLE_ID }}"
      VAULT_SECRET_ID: "${{ secrets.VAULT_SECRET_ID }}"

  deploy-to-deployment:
    name: "Deploy"
    runs-on: "ubuntu-latest"
    needs:
      - "build-for-development"
    env:
      KUBERNETES_NAMESPACE: "hotpocket-development"
      KUBERNETES_CLUSTER: "k8s.bthlab"
    steps:
      - name: "Checkout the code"
        uses: "actions/checkout@v2"
      - name: "Get build options"
        id: "get-build-options"
        uses: "./.gitea/actions/get-build-options"
      - name: "Get `backend` version"
        id: "get-backend-version"
        uses: "./.gitea/actions/get-service-version"
        with:
          service: "backend"
      - name: "Setup k8s"
        uses: "./.gitea/actions/setup-k8s"
        with:
          arch: "${{ steps.get-build-options.outputs.build-arch }}"
      - name: "Import Secrets"
        id: "import-secrets"
        uses: "hashicorp/vault-action@v2"
        with:
          url: "https://vault.bthlabs.pl/"
          method: "approle"
          roleId: "${{ secrets.VAULT_ROLE_ID }}"
          secretId: "${{ secrets.VAULT_SECRET_ID }}"
          secrets: |
            gitea/data/k8s.bthlab config | KUBECONFIG_PAYLOAD
      - name: "Set up kubeconfig"
        env:
          KUBECONFIG_PAYLOAD: "${{ steps.import-secrets.outputs.KUBECONFIG_PAYLOAD }}"
        run: |
          set -x

          echo ${KUBECONFIG_PAYLOAD} | base64 -d >"/opt/k8s/etc/kubeconfig"
          export KUBECONFIG="/opt/k8s/etc/kubeconfig"
          
          /opt/k8s/bin/kubectl config use-context ${KUBERNETES_CLUSTER}
          /opt/k8s/bin/kubectl get node
      - name: "Run `backend` Django migrations"
        env:
          BACKEND_TAG: "deployment-${{ steps.get-backend-version.outputs.version }}-${{ steps.get-backend-version.outputs.build-number }}"
        run: |
          set -x

          (
            cd deployment/hotpocket_bthlab ;
            export KUBECONFIG="/opt/k8s/etc/kubeconfig" ;
            /opt/k8s/bin/kubectl config use-context ${KUBERNETES_CLUSTER} ;
            /opt/k8s/bin/kubectl -n ${KUBERNETES_NAMESPACE} apply -f resources/backend/config-map-local-deps.yaml ;
            /opt/k8s/bin/kubectl -n ${KUBERNETES_NAMESPACE} set image cronjobs/backend-job-migrations migrations=nexus.bthlab.bthlabs.net:8002/hotpocket/backend:${BACKEND_TAG} ;
            /opt/k8s/bin/kubectl -n ${KUBERNETES_NAMESPACE} delete jobs --ignore-not-found=true backend-job-migrations ;
            /opt/k8s/bin/kubectl -n ${KUBERNETES_NAMESPACE} create job backend-job-migrations --from=cronjob/backend-job-migrations ;
            /opt/k8s/bin/kubectl -n ${KUBERNETES_NAMESPACE} wait --for=condition=complete --timeout=300s job/backend-job-migrations
          )
      - name: "Deploy"
        env:
          BACKEND_TAG: "deployment-${{ steps.get-backend-version.outputs.version }}-${{ steps.get-backend-version.outputs.build-number }}"
        run: |
          set -x
          (
            cd deployment/hotpocket_bthlab ;
            export KUBECONFIG="/opt/k8s/etc/kubeconfig" ;
            /opt/k8s/bin/kubectl config use-context ${KUBERNETES_CLUSTER} ;
            /opt/k8s/bin/kustomize edit set image hotpocket-backend=nexus.bthlab.bthlabs.net:8002/hotpocket/backend:${BACKEND_TAG} ;
            /opt/k8s/bin/kustomize build . | /opt/k8s/bin/kubectl apply -f -
          )
