name: Formbricks Cloud Deployment

on:
  workflow_dispatch:
    inputs:
      VERSION:
        description: 'The version of the Docker image to release, full image tag if image tag is v0.0.0 enter v0.0.0.'
        required: true
        type: string
      REPOSITORY:
        description: 'The repository to use for the Docker image'
        required: false
        type: string
        default: 'ghcr.io/formbricks/formbricks'
      ENVIRONMENT:
        description: 'The environment to deploy to'
        required: true
        type: choice
        options:
          - stage
          - prod
  workflow_call:
    inputs:
      VERSION:
        description: 'The version of the Docker image to release'
        required: true
        type: string
      REPOSITORY:
        description: 'The repository to use for the Docker image'
        required: false
        type: string
        default: 'ghcr.io/formbricks/formbricks'
      ENVIRONMENT:
        description: 'The environment to deploy to'
        required: true
        type: string

permissions:
  id-token: write
  contents: write

jobs:
  helmfile-deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4.2.2

      - name: Tailscale
        uses: tailscale/github-action@v3
        with:
          oauth-client-id: ${{ secrets.TS_OAUTH_CLIENT_ID }}
          oauth-secret: ${{ secrets.TS_OAUTH_SECRET }}
          tags: tag:github

      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
        with:
          role-to-assume: ${{ secrets.AWS_ASSUME_ROLE_ARN }}
          aws-region: "eu-central-1"

      - name: Setup Cluster Access
        run: |
          aws eks update-kubeconfig --name formbricks-prod-eks --region eu-central-1
        env:
          AWS_REGION: eu-central-1

      - uses: helmfile/helmfile-action@v2
        name: Deploy Formbricks Cloud Prod
        if: inputs.ENVIRONMENT == 'prod'
        env:
          VERSION: ${{ inputs.VERSION }}
          REPOSITORY: ${{ inputs.REPOSITORY }}
          FORMBRICKS_S3_BUCKET: ${{ secrets.FORMBRICKS_S3_BUCKET }}
          FORMBRICKS_INGRESS_CERT_ARN: ${{ secrets.FORMBRICKS_INGRESS_CERT_ARN }}
          FORMBRICKS_ROLE_ARN: ${{ secrets.FORMBRICKS_ROLE_ARN }}
        with:
          helmfile-version: 'v1.0.0'
          helm-plugins: >
            https://github.com/databus23/helm-diff,
            https://github.com/jkroepke/helm-secrets
          helmfile-args: apply -l environment=prod
          helmfile-auto-init: "false"
          helmfile-workdirectory: infra/formbricks-cloud-helm

      - uses: helmfile/helmfile-action@v2
        name: Deploy Formbricks Cloud Stage
        if: inputs.ENVIRONMENT == 'stage'
        env:
          VERSION: ${{ inputs.VERSION }}
          REPOSITORY: ${{ inputs.REPOSITORY }}
          FORMBRICKS_INGRESS_CERT_ARN: ${{ secrets.STAGE_FORMBRICKS_INGRESS_CERT_ARN }}
          FORMBRICKS_ROLE_ARN: ${{ secrets.STAGE_FORMBRICKS_ROLE_ARN }}
        with:
          helmfile-version: 'v1.0.0'
          helm-plugins: >
            https://github.com/databus23/helm-diff,
            https://github.com/jkroepke/helm-secrets
          helmfile-args: apply -l environment=stage
          helmfile-auto-init: "false"
          helmfile-workdirectory: infra/formbricks-cloud-helm

