name: 'NocoDB Nightly Dev Release'

on:
  # Triggered manually
  workflow_dispatch:
    inputs:
      targetEnv:
        description: "Target Environment"
        required: true
        type: choice
        options:
          - DEV
          # - PROD
  schedule:
    # at the end of every day
    - cron: '0 0 * * *'

jobs:
  # enrich tag for nightly auto release
  set-tag:
    runs-on: 'ubuntu-latest'
    steps:
      - name: set-tag
        id: tag-step
        run: |
          # Get current date
          CURRENT_DATE=$(date +"%Y%m%d")
          CURRENT_TIME=$(date +"%H%M")
          TAG_NAME=${CURRENT_DATE}
          IS_DAILY='Y'
          # Set the tag
          if [[ ${{ github.event_name }} == 'workflow_dispatch' ]]; then
            TAG_NAME=${TAG_NAME}-${CURRENT_TIME}
            IS_DAILY='N'
          fi
          echo "::set-output name=NIGHTLY_BUILD_TAG::${TAG_NAME}"
          echo "::set-output name=IS_DAILY::${IS_DAILY}"
          # Verify the tag
      - name: verify-tag
        run: |
          echo ${{ steps.tag-step.outputs.NIGHTLY_BUILD_TAG }}
          echo ${{ steps.tag-step.outputs.IS_DAILY }}
    outputs:
      nightly_build_tag: ${{ steps.tag-step.outputs.NIGHTLY_BUILD_TAG }}
      is_daily: ${{ steps.tag-step.outputs.IS_DAILY }}
  # Build frontend and backend and publish to npm
  release-npm:
    needs: set-tag
    uses: ./.github/workflows/release-npm.yml
    with:
      tag: ${{ needs.set-tag.outputs.nightly_build_tag }}
      targetEnv: ${{ github.event.inputs.targetEnv || 'DEV' }}
    secrets:
      NPM_TOKEN: "${{ secrets.NPM_TOKEN }}"

  # Build docker image and push to docker hub
  release-docker:
    needs: [set-tag, release-npm]
    uses: ./.github/workflows/release-docker.yml
    with:
      tag: ${{ needs.set-tag.outputs.nightly_build_tag }}
      targetEnv: ${{ github.event.inputs.targetEnv || 'DEV' }}
      isDaily: ${{ needs.set-tag.outputs.is_daily }}
    secrets:
      DOCKERHUB_USERNAME: "${{ secrets.DOCKERHUB_USERNAME }}"
      DOCKERHUB_TOKEN: "${{ secrets.DOCKERHUB_TOKEN }}"
