name: '[docker image]'

on:
  push:
    branches:
      - "master"
      - "staging"
    # do not rebuild if...
    paths-ignore:
      # changes to .openshift directory... mostly devops config
      - '.openshift/*'
      # docs (docs are built on readthedocs any way)
      - 'docs/**/*'

jobs:
  build-docker-img:
    runs-on: ubuntu-latest
    env:
      MATOMO_ID_PROD: '12'
      MATOMO_URL_PROD: 'https://stats.humanbrainproject.eu/'
      PRODUCTION: 'true'
      DOCKER_REGISTRY: 'docker-registry.ebrains.eu/siibra/'

      SIIBRA_API_STABLE: 'https://siibra-api-stable.apps.hbp.eu/v3_0,https://siibra-api-prod.apps.tc.humanbrainproject.eu/v3_0'
      SIIBRA_API_RC: 'https://siibra-api-rc.apps.hbp.eu/v3_0'
      SIIBRA_API_LATEST: 'https://siibra-api-latest.apps-dev.hbp.eu/v3_0'

    steps:
    - uses: actions/checkout@v4
      with:
        lfs: true
    - name: 'Set matomo env var'
      run: |
        echo "Using github.ref: $GITHUB_REF"

        echo "BRANCH_NAME=${GITHUB_REF#refs/heads/}" >> $GITHUB_ENV
        
        echo "MATOMO_URL=${{ env.MATOMO_URL_PROD }}" >> $GITHUB_ENV
        echo "MATOMO_ID=${{ env.MATOMO_ID_PROD }}" >> $GITHUB_ENV

        if [[ "$GITHUB_REF" == 'refs/heads/master' ]]
        then
          echo "Either master, using prod env..."
          echo "SIIBRA_API_ENDPOINTS=${{ env.SIIBRA_API_STABLE }}" >> $GITHUB_ENV
        elif [[ "$GITHUB_REF" == 'refs/heads/staging' ]]
        then
          echo "Either staging, using staging env..."
          echo "SIIBRA_API_ENDPOINTS=${{ env.SIIBRA_API_RC }}" >> $GITHUB_ENV
        else
          echo "Currently, only master and staging are built"
          exit 1
        fi

    - name: 'Set version variable & expmt feature flag'
      run: |
        if [[ "$GITHUB_REF" == 'refs/heads/master' ]] || [[ "$GITHUB_REF" == 'refs/heads/staging' ]]
        then
          echo "prod/staging build, do not enable experimental features"
        else
          echo "dev bulid, enable experimental features"
        fi
    - name: 'Build docker image'
      run: |
        DOCKER_BUILT_TAG=${{ env.DOCKER_REGISTRY }}siibra-explorer:$BRANCH_NAME
        echo "Building $DOCKER_BUILT_TAG"
        docker build \
          --build-arg MATOMO_URL=$MATOMO_URL \
          --build-arg MATOMO_ID=$MATOMO_ID \
          --build-arg SIIBRA_API_ENDPOINTS=$SIIBRA_API_ENDPOINTS \
          --build-arg EXPERIMENTAL_FEATURE_FLAG=$EXPERIMENTAL_FEATURE_FLAG \
          -t $DOCKER_BUILT_TAG \
          .
        echo "Successfully built $DOCKER_BUILT_TAG"
        echo "DOCKER_BUILT_TAG=$DOCKER_BUILT_TAG" >> $GITHUB_ENV

    - name: 'Push to docker registry'
      run: |
        echo "Login to docker registry"
        docker login \
          -u '${{ secrets.EBRAINS_DOCKER_REG_USER }}' \
          -p '${{ secrets.EBRAINS_DOCKER_REG_TOKEN }}' \
          docker-registry.ebrains.eu
        echo "Pushing $DOCKER_BUILT_TAG"
        docker push $DOCKER_BUILT_TAG
    - name: 'If master, also tag as per package.json'
      if: ${{ github.ref == 'refs/heads/master' }}
      run: |
        VERSION=$(jq -r '.version' package.json)
        VERSIONED_DOCKER_BUILT_TAG=${{ env.DOCKER_REGISTRY }}siibra-explorer:$VERSION
        docker tag $DOCKER_BUILT_TAG $VERSIONED_DOCKER_BUILT_TAG
        docker push $VERSIONED_DOCKER_BUILT_TAG

  setting-vars:
    if: success()
    runs-on: ubuntu-latest
    outputs:
      BRANCH_NAME: ${{ steps.set-vars.outputs.BRANCH_NAME }}
      BUILD_TEXT: ${{ steps.set-vars.outputs.BUILD_TEXT }}
      DEPLOY_ID: ${{ steps.set-vars.outputs.DEPLOY_ID }}
      SXPLR_VERSION: ${{ steps.set-vars.outputs.SXPLR_VERSION }}
    steps:
      - uses: actions/checkout@v3
      - id: set-vars
        name: Set vars
        run: |
          echo "Using github.ref: $GITHUB_REF"

          BRANCH_NAME=${GITHUB_REF#refs/heads/}
          echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_OUTPUT

          echo "Branch is $BRANCH_NAME ."

          if [[ "$BRANCH_NAME" == 'master' ]]
          then
            echo "BUILD_TEXT=" >> $GITHUB_OUTPUT
          else
            echo "BUILD_TEXT=$BRANCH_NAME" >> $GITHUB_OUTPUT
          fi

          # DEPLOY_ID == remove _ / and lowercase everything from branch
          DEPLOY_ID=$(echo ${BRANCH_NAME//[_\/]/} | awk '{ print tolower($0) }')
          echo "DEPLOY_ID=$DEPLOY_ID" >> $GITHUB_OUTPUT

          SXPLR_VERSION=$(jq -r '.version' package.json)
          echo "SXPLR_VERSION=$SXPLR_VERSION"
          echo "SXPLR_VERSION=$SXPLR_VERSION" >> $GITHUB_OUTPUT

  trigger-deploy-master-prod:
    if: ${{ needs.setting-vars.outputs.BRANCH_NAME == 'master' && success() }}
    needs:
      - build-docker-img
      - setting-vars
    uses: ./.github/workflows/deploy-on-okd.yml
    with:
      FULL_DEPLOY_ID: siibra-explorer-branch-deploy-2-prodpathviewer
      OKD_ENDPOINT: https://okd.hbp.eu:443
      OKD_PROJECT: interactive-viewer
    secrets:
      okd_token: ${{ secrets.OKD_PROD_SECRET }}

  trigger-deploy-master-rancher:
    if: ${{ needs.setting-vars.outputs.BRANCH_NAME == 'master' && success() }}
    needs:
      - build-docker-img
      - setting-vars
    uses: ./.github/workflows/deploy-helm.yml
    with:
      DEPLOYMENT_NAME: master
      IMAGE_TAG: ${{ needs.setting-vars.outputs.SXPLR_VERSION }}
    secrets:
      KUBECONFIG: ${{ secrets.KUBECONFIG }}

  trigger-deploy-staging-viewer-validation:
    if: ${{ needs.setting-vars.outputs.BRANCH_NAME == 'staging' && success() }}
    needs:
      - build-docker-img
      - setting-vars
    uses: ./.github/workflows/deploy-on-okd.yml
    with:
      FULL_DEPLOY_ID: siibra-explorer-branch-deploy-2-stagingpathed
      OKD_ENDPOINT: https://okd.hbp.eu:443
      OKD_PROJECT: interactive-viewer
    secrets:
      okd_token: ${{ secrets.OKD_PROD_SECRET }}
  
  # cluster offline
  # TODO need to find new home for data-validation

  # trigger-deploy-staging-data-validation:
  #   if: ${{ needs.setting-vars.outputs.BRANCH_NAME == 'staging' && success() }}
  #   needs:
  #     - build-docker-img
  #     - setting-vars
  #   uses: ./.github/workflows/deploy-on-okd.yml
  #   with:
  #     FULL_DEPLOY_ID: siibra-explorer-rc
  #     OKD_ENDPOINT: https://okd.jsc.hbp.eu:443
  #     OKD_PROJECT: siibra-explorer
  #   secrets:
  #     okd_token: ${{ secrets.OKD_JSC_TOKEN }}
  