image: atlassian/default-image:latest



pipelines:
  pull-requests:
    "**":
      - step:
          name: "Merged to main > building Tag "
          script:

            # # if default branch is other than `main` below checkout is required..
            # - git fetch --all
            # - git checkout default-branch
            # # note tested ^^

            - if [ "$BITBUCKET_PR_DESTINATION_BRANCH" = "main" ]; then
              echo "This PR was merged into main, triggering pipeline";
              bash ./.pipeline/generate_tag.sh;
              # chmod +x scripts/generate_tag.sh; # Ensure script is executable
              # ./.pipeline/generate_tag.sh; # Run the external script
            # Add your deployment script here
            - else
              echo "Not merging into main, skipping.";
              fi

  tags:
    "*": # Triggers on any tag creation
      - step:
          name: "Triggering Tag based Deployment Pipeline"
          script:
            - echo "Triggering Tag based pipeline"

      - step:
          name: "QA Deployment & Await Approval"
          deployment: qa #  TODO add to corresponding slallc-ktm > sla-php > sla-php > Repository settings > Deployments
          trigger: manual
          script:
            - |
              echo "Deploy to QA"
              # Fetch all tags (ensures we get the latest)
              git fetch --tags
              # Get the latest tag
              LAST_TAG=$(git describe --tags --abbrev=0)


              # Print the last tag (for debugging)..
              echo "Latest Tag: $LAST_TAG"
            - bash ./.pipeline/_sla-integration-deploy.sh mj-laravel11 mj-qa $LAST_TAG # # TODO remember change 2nd parm repo-name
      - step:
          name: "Stage Deployment & Await Approval"
          deployment: staging
          trigger: manual
          script:
            - |
              echo "Deploy to stage"
              echo "Deploy to QA"
              # Fetch all tags (ensures we get the latest)
              git fetch --tags
              # Get the latest tag
              LAST_TAG=$(git describe --tags --abbrev=0)

              # Print the last tag (for debugging)
              echo "Latest Tag: $LAST_TAG"
              echo "TODO Send an HTTP req http://req.utl with req body : {Repo, Tag, Environment, envVars, secrets etc as JSON }, {secret, api key as Header/s}"
      - step:
          name: "Prod Deployment & Await Approval"
          deployment: production
          trigger: manual
          script:
            - |
              echo "Deploy to prod"
              echo "Deploy to QA"
              # Fetch all tags (ensures we get the latest)
              git fetch --tags
              # Get the latest tag
              LAST_TAG=$(git describe --tags --abbrev=0)

              # Print the last tag (for debugging)
              echo "Latest Tag: $LAST_TAG"
              echo "TODO Send an HTTP req http://req.utl with req body : {Repo, Tag, Environment, envVars, secrets etc as JSON }, {secret, api key as Header/s}"


  custom:
    deploy:
      - step:
          name: "Manual Deployment"
          script:
            - echo "Triggering deployment for branch $BITBUCKET_BRANCH"
            - bash ./.pipeline/_sla-integration-deploy.sh mj-laravel11 mj-dev $BITBUCKET_BRANCH  # # TODO remember change 2nd parm repo-name
      ## in future if we have multiple env we will need below to select an env
      # - parallel:
      #   - step:
      #       name: Env - DEV-001
      #       trigger: manual
      #       script:
      #         - echo "Triggering deployment for branch $BITBUCKET_BRANCH  DEV-001"
      #         # todo change your vars here
      #         - bash ./.pipeline/_sla-integration-deploy.sh mj-laravel11 mj-dev $BITBUCKET_BRANCH DEV-001  # # TODO remember change 2nd parm repo-name
      #   - step:
      #       name: Env - DEV-002
      #       trigger: manual
      #       script:
      #         - echo "Triggering deployment for branch $BITBUCKET_BRANCH  DEV-002"
      #         # todo change your vars here
      #         - bash ./.pipeline/_sla-integration-deploy.sh mj-laravel11 mj-dev $BITBUCKET_BRANCH DEV-002  # # TODO remember change 2nd parm repo-name
      # # root step
      # - step:
      #     name: Deploy Root Ending Step
      #     script:
      #       - echo "Completed"

    manual-build-tag-from-current-main:
      - step:
          name: "Deploy - Manually Generated Tag from current main"
          script:
            - echo "Manually triggering tag generation"
            - bash ./.pipeline/generate_tag.sh
