name: Budibase Release
concurrency:
  group: release
  cancel-in-progress: false

on:
  push:
    tags:
      - "[0-9]+.[0-9]+.[0-9]+"
      # Exclude all pre-releases
      - "!*[0-9]+.[0-9]+.[0-9]+-*"

env:
  # Posthog token used by ui at build time
  POSTHOG_TOKEN: phc_bIjZL7oh2GEUd2vqvTBH8WvrX0fWTFQMs6H5KQxiUxU
  INTERCOM_TOKEN: ${{ secrets.INTERCOM_TOKEN }}
  SENTRY_DSN: ${{ secrets.SENTRY_DSN }}
  PERSONAL_ACCESS_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}

jobs:
  release-images:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: true
          token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
          fetch-depth: 0

      - name: Fail if tag is not in master
        run: |
          if ! git merge-base --is-ancestor ${{ github.sha }} origin/master; then
            echo "Tag is not in master. This pipeline can only execute tags that are present on the master branch"
            exit 1
          fi

      - uses: actions/setup-node@v1
        with:
          node-version: 18.x

      - run: yarn install --frozen-lockfile
      - name: Update versions
        run: ./scripts/updateVersions.sh
      - run: yarn lint
      - run: yarn build
      - run: yarn build:sdk

      - name: Publish budibase packages to NPM
        env:
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
        run: |
          # setup the username and email. I tend to use 'GitHub Actions Bot' with no email by default
          git config --global user.name "Budibase Release Bot"
          git config --global user.email "<>"
          git submodule foreach git commit -a -m 'Release process'
          git commit -a -m 'Release process'
          echo //registry.npmjs.org/:_authToken=${NPM_TOKEN} >> .npmrc
          yarn release

      - name: "Get Current tag"
        id: currenttag
        run: |
          version=$(./scripts/getCurrentVersion.sh)
          echo "Using tag $version"
          echo "version=$version" >> "$GITHUB_OUTPUT"

      - name: Build/release Docker images
        run: |
          docker login -u $DOCKER_USER -p $DOCKER_PASSWORD
          yarn build:docker
        env:
          DOCKER_USER: ${{ secrets.DOCKER_USERNAME }}
          DOCKER_PASSWORD: ${{ secrets.DOCKER_API_KEY }}
          BUDIBASE_RELEASE_VERSION: ${{ steps.currenttag.outputs.version }}

  release-helm-chart:
    needs: [release-images]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Setup Helm
        uses: azure/setup-helm@v1
        id: helm-install

      - name: Get the latest budibase release version
        id: version
        run: |
          release_version=$(cat lerna.json | jq -r '.version')
          echo "RELEASE_VERSION=$release_version" >> $GITHUB_ENV

      # due to helm repo index issue: https://github.com/helm/helm/issues/7363
      # we need to create new package in a different dir, merge the index and move the package back
      - name: Build and release helm chart
        run: |
          git config user.name "Budibase Helm Bot"
          git config user.email "<>"
          git reset --hard
          git fetch
          mkdir sync
          echo "Packaging chart to sync dir"
          helm package charts/budibase --version 0.0.0-master --app-version "$RELEASE_VERSION" --destination sync
          echo "Packaging successful"
          git checkout gh-pages
          echo "Indexing helm repo"         
          helm repo index --merge docs/index.yaml sync
          mv -f sync/* docs
          rm -rf sync
          echo "Pushing new helm release"
          git add -A
          git commit -m "Helm Release: ${{ env.RELEASE_VERSION }}"
          git push

  deploy-to-legacy-preprod-env:
    needs: [release-images]
    uses: ./.github/workflows/deploy-preprod.yml
    secrets: inherit

  # Trigger deploy to new EKS preprod environment
  trigger-deploy-to-preprod-env:
    needs: [release-helm-chart]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Get the latest budibase release version
        id: version
        run: |
          release_version=$(cat lerna.json | jq -r '.version')
          echo "RELEASE_VERSION=$release_version" >> $GITHUB_ENV

      - uses: passeidireto/trigger-external-workflow-action@main
        env:
          PAYLOAD_VERSION: ${{ env.RELEASE_VERSION }}
        with:
          repository: budibase/budibase-deploys
          event: budicloud-preprod-deploy
          github_pat: ${{ secrets.GH_ACCESS_TOKEN }}