@Library('infraLib') _

pipeline {
  agent {
    label 'linux-agents'
  }
  options { disableConcurrentBuilds() }
  environment {
    AWS_PROFILE             = "sme-dev"
    PROJECT_GROUP           = 'delphi'
    OCTOPUS_CLI_SERVER      = "https://octopus.delphi.zone"
    OCTOPUS_CLI_API_KEY     = sh(script: "aws secretsmanager get-secret-value --profile ${env.AWS_PROFILE} --secret-id 'infra/jenkins/octopus_api_key' --output json | jq .SecretString | tr -d '\"'", , returnStdout: true).trim()
    OCTOPUS_CHANNEL         = octopus.setOctoChannel(env.BRANCH_NAME)
    DEPLOY_ENV              = zipPackage.setDeploymentEnvV2(params.DEPLOY_ENV_CHOICE, env.PROJECT_GROUP, env.BRANCH_NAME )
    ZIP_PACKAGE_UPLOAD_MODE = octopus.zipPackageUploadMode(params.ZIP_PACKAGE_UPLOAD_MODE)
    NOTIFICATIONS_CHANNEL   = 'delphi-slz-cicd-alerts'
  }
  parameters {
    choice(
      choices: [
        'Please make a choice',
        'data-health-config',
        'ALL'
      ],
      description: 'Force build all or some modules',
      name: 'FORCE_BUILD'
    )
    // Ability to set particular environment for deployment or to skip it at all
    choice(
      choices: [
        'default',
        'dev',
        'qa',
        'stage',
        'skip_deployment'
      ],
      description: 'Set environment for deployment. Development by default',
      name: 'DEPLOY_ENV_CHOICE'
    )
    choice(
      choices: [
        'error',
        'overwrite',
        'ignore'
      ],
      description: 'Defines whether to raise error, overwrite or ignore if zipPackage of specified version already exists in Octopus.',
      name: 'ZIP_PACKAGE_UPLOAD_MODE'
    )
    choice(
      choices: [
        'no',
        'yes'
      ],
      description: "Don't raise errors in case release already exists",
      name: 'SKIP_RELEASE_CREATION'
    )
  }
  stages {
    stage('Prepare artifacts') {
      steps {
        script {
          def envs = ["dev","qa","stage","prod"]
          def configs = [
            "data-health-config",
          ]
          for (config in configs) {zipPackage.prepareMultiEnvConfigs(config,envs)}
        }
      }
    }
    stage('Parallel Builds') {
      steps {
        script {
          zipStagesMap = [
            [path: 'artifacts/data-health-config', name: 'data-health-config', mode: 'json'],
          ]
          zipStagesList = zipPackage.generateStages(params, zipStagesMap)
          parallel(zipStagesList)
        }
      }
    }
  }
  post {
    always {
      sh "sudo chown -R ec2-user:ec2-user ${env.WORKSPACE}"
    }
    success {
      script {
        if (currentBuild.getPreviousBuild() &&
            currentBuild.getPreviousBuild().getResult().toString() != "SUCCESS") {
          utility.slackNotifyMainBranches(currentBuild.currentResult, env.NOTIFICATIONS_CHANNEL)
        }
      }
    }
    failure {
      script {
        utility.slackNotifyMainBranches(currentBuild.currentResult, env.NOTIFICATIONS_CHANNEL)
      }
    }
    cleanup {
      deleteDir()
    }
  }
}
