name: deploy

on:
  repository_dispatch:
    types: [trigger-ocelot-brand-build-success]

jobs:
  deploy:
    # see example https://github.com/do-community/example-doctl-action
    # see example https://github.com/do-community/example-doctl-action/blob/main/.github/workflows/workflow.yaml
    name: Deploy defined version to cluster
    runs-on: ubuntu-latest
    env:
      SECRET: ${{ secrets.SECRET }}
      CONFIGURATION: "this"
      GITHUB_OCELOT_REF_JUST_BUILT: ${{ github.event.client_payload.ocelot_ref }}
      DOCKERHUB_OCELOT_TAG_JUST_BUILT: ${{ github.event.client_payload.BUILD_VERSION }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      - name: Decrypt .env
        run: gpg --quiet --batch --yes --decrypt --passphrase="${{ env.SECRET }}" --output .env .env.enc
      - name: Load .env
        uses: aarcangeli/load-dotenv@v1.0.0
        with:
          quiet: true
      - name: Set GITHUB_OCELOT_REF
        run: |
          if [ -z ${GITHUB_OCELOT_REF} ]; then
            echo "GITHUB_OCELOT_REF=${GITHUB_OCELOT_REF_JUST_BUILT}" >> $GITHUB_ENV
          fi
        shell: bash
      - name: Checkout Ocelot code
        uses: actions/checkout@v3
        with:
          repository: 'Ocelot-Social-Community/Ocelot-Social'
          ref: ${{ env.GITHUB_OCELOT_REF }}
          path: 'ocelot/'
          fetch-depth: 0
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          path: "ocelot/deployment/configurations/${{ env.CONFIGURATION }}"
      - name: Set DOCKERHUB_OCELOT_TAG
        run: |
          if [ -z ${DOCKERHUB_OCELOT_TAG} ]; then
            echo "DOCKERHUB_OCELOT_TAG=${DOCKERHUB_OCELOT_TAG_JUST_BUILT}" >> $GITHUB_ENV
          fi
        shell: bash
      - name: Decrypt all secrets
        run: ocelot/deployment/scripts/secrets.decrypt.sh
      - name: Upgrade Cluster
        run: ocelot/deployment/scripts/cluster.upgrade.sh
      #- name: Sleep for 4 minutes
      #  run: sleep 240s
      #- name: Reset and seed Neo4j database
      #  run: ocelot/deployment/scripts/cluster.reseed.sh