# Pipeline for Salesforce deployment check & MegaLinter check
#
# This pipeline is triggered when a commit is added to a Pull Request
#
# This means the Pull Request should already exist before commiting your work or you'll have to push an empty commit
# to trigger the pipeline.
#
# Setup:
#   - Go to Pipelines
#   - Select or create a new pipeline (you may have to Create new -> Existing Azure Pipelines YAML file)
#   - Edit this new pipeline
#   - Add your variables in the "variables" section and tick "Keep this value secret"

#  /!\ This part is only for deployment pipeline

#   - Go to Pipelines and select your pipeline then Edit it
#   - Open the menu ⋮ and select Triggers
#   - In "Branch filters", tick "Override the YAML continuous integration trigger from here"
#   - "Include" all your major branches

name: Deploy to org

# Mandatory variables, used by jobs
variables:
  - name: FORCE_COLOR
    value: "1"
  - name: BRANCH_NAME
    value: $[replace(variables['Build.SourceBranch'], 'refs/heads/', '')]

jobs:
  # Deploy source with SFDX
  - job: Deployment
    timeoutInMinutes: 150
    pool:
      vmImage: ubuntu-latest
    steps:
      # Checkout repo
      - checkout: self
        fetchDepth: 0
        persistCredentials: true
        displayName: Git Checkout

      # Setup Node.js
      - task: UseNode@1
        inputs:
          version: ">=20.0.0"
        displayName: "Setup Node.js"

      # Install SFDX & Dependencies
      - script: |
          npm install @salesforce/cli --global
          sf plugins install @salesforce/plugin-packaging
          echo 'y' | sf plugins install sfdx-hardis
          echo 'y' | sf plugins install sfdmu
          echo 'y' | sf plugins install sfdx-git-delta
          sf version --verbose --json
        displayName: "Install SFDX & plugins"

      # Login & Deploy sfdx sources to related org (configuration: https://hardisgroupcom.github.io/sfdx-hardis/salesforce-ci-cd-setup-auth/ )
      - script: |
          sf hardis:auth:login
          sf hardis:project:deploy:smart
        env:
          SFDX_CLIENT_ID_INTEGRATION: $(SFDX_CLIENT_ID_INTEGRATION)
          SFDX_CLIENT_KEY_INTEGRATION: $(SFDX_CLIENT_KEY_INTEGRATION)
          CI_COMMIT_REF_NAME: $(BRANCH_NAME)
          CONFIG_BRANCH: $(BRANCH_NAME)
          ORG_ALIAS: $(BRANCH_NAME)
          SLACK_TOKEN: $(SLACK_TOKEN)
          SLACK_CHANNEL_ID: $(SLACK_CHANNEL_ID)
          NOTIF_EMAIL_ADDRESS: $(NOTIF_EMAIL_ADDRESS)
          GENERIC_TICKETING_PROVIDER_REGEX: $(GENERIC_TICKETING_PROVIDER_REGEX)
          GENERIC_TICKETING_PROVIDER_URL_BUILDER: $(GENERIC_TICKETING_PROVIDER_URL_BUILDER)
          JIRA_HOST: $(JIRA_HOST)
          JIRA_EMAIL: $(JIRA_EMAIL)
          JIRA_TOKEN: $(JIRA_TOKEN)
          JIRA_PAT: $(JIRA_HOST)
          JIRA_TICKET_REGEX: $(JIRA_TICKET_REGEX)
          SFDX_DEPLOY_WAIT_MINUTES: 150
          SFDX_AUTH_URL_TECHNICAL_ORG: $(SFDX_AUTH_URL_TECHNICAL_ORG)
          CI: "true"
          SYSTEM_ACCESSTOKEN: $(System.AccessToken)
          CI_SFDX_HARDIS_AZURE_TOKEN: $(System.AccessToken)
          SYSTEM_COLLECTIONURI: $(System.CollectionUri)
          SYSTEM_TEAMPROJECT: $(System.TeamProject)
          SYSTEM_JOB_DISPLAY_NAME: $(System.JobDisplayName)
          SYSTEM_JOB_ID: $(System.JobId)
          SYSTEM_PULLREQUEST_PULLREQUESTID: $(System.PullRequest.PullRequestId)
          BUILD_REPOSITORY_ID: $(Build.Repository.ID)
          BUILD_REPOSITORYNAME: $(Build.Repository.Name)
          BUILD_SOURCEBRANCHNAME: $(Build.SourceBranchName)
          BUILD_BUILD_ID: $(Build.BuildId)
        displayName: "Deploy sources to related SF Org"
