#!groovy

pipeline {
  agent any

  options {
    ansiColor('xterm')
    disableConcurrentBuilds()
  }

  parameters {
    listGitBranches branchFilter: '.*', credentialsId: 'theorchard-ci-ssh-key', defaultValue: 'refs/heads/develop', name: 'APOLLO_API_BRANCH', quickFilterEnabled: true, remoteURL: 'git@github.com:theorchard/apollo-api.git', selectedValue: 'DEFAULT', sortMode: 'NONE', tagFilter: '*', type: 'PT_BRANCH'
    listGitBranches branchFilter: '.*', credentialsId: 'theorchard-ci-ssh-key', defaultValue: 'refs/heads/develop', name: 'DSP_API_BRANCH', quickFilterEnabled: true, remoteURL: 'git@github.com:theorchard/dsp-api.git', selectedValue: 'DEFAULT', sortMode: 'NONE', tagFilter: '*', type: 'PT_BRANCH'
    listGitBranches branchFilter: '.*', credentialsId: 'theorchard-ci-ssh-key', defaultValue: 'refs/heads/develop', name: 'PRIVATE_API_BRANCH', quickFilterEnabled: true, remoteURL: 'git@github.com:theorchard/filtr-privateapi.git', selectedValue: 'DEFAULT', sortMode: 'NONE', tagFilter: '*', type: 'PT_BRANCH'
    listGitBranches branchFilter: '.*', credentialsId: 'theorchard-ci-ssh-key', defaultValue: 'refs/heads/develop', name: 'FILTR_ADMIN_BRANCH', quickFilterEnabled: true, remoteURL: 'git@github.com:theorchard/filtr-admin.git', selectedValue: 'DEFAULT', sortMode: 'NONE', tagFilter: '*', type: 'PT_BRANCH'
  }

  environment {
    APOLLO_API_BRANCH  = "${params.APOLLO_API_BRANCH.minus('refs/heads/')}"
    DSP_API_BRANCH     = "${params.DSP_API_BRANCH.minus('refs/heads/')}"
    FILTR_ADMIN_BRANCH = "${params.FILTR_ADMIN_BRANCH.minus('refs/heads/')}"
    PRIVATE_API_BRANCH = "${params.PRIVATE_API_BRANCH.minus('refs/heads/')}"
  }

  stages {
    stage('Deploy') {
      parallel {
        stage('Info') {
          steps {
            script {
              currentBuild.description = """apollo-api: ${env.APOLLO_API_BRANCH}
                dsp-api: ${env.DSP_API_BRANCH}
                filtr-admin: ${env.FILTR_ADMIN_BRANCH}
                filtr-privateapi: ${env.PRIVATE_API_BRANCH}"""
            }
            echo "Apollo API branch: ${env.APOLLO_API_BRANCH}"
            echo "DSP API branch: ${env.DSP_API_BRANCH}"
            echo "Filtr Admin branch: ${env.FILTR_ADMIN_BRANCH}"
            echo "Private API branch: ${env.PRIVATE_API_BRANCH}"
          }
        }
        stage('Apollo API') {
          steps {
            build job: "DNA/apollo-api/${env.APOLLO_API_BRANCH}", parameters: [string(name: 'ECR', value: '475275892927.dkr.ecr.us-east-1.amazonaws.com'), string(name: 'FORCE_BUILD', value: 'api'), booleanParam(name: 'DEPLOY_TO_TEST', value: true)]
          }
        }
        stage('DSP API') {
          steps {
            build job: "DNA/dsp-api/${env.DSP_API_BRANCH}", parameters: [string(name: 'ECR', value: '475275892927.dkr.ecr.us-east-1.amazonaws.com'), booleanParam(name: 'DEPLOY_TO_TEST', value: true)]
          }
        }
        stage('Filtr Admin') {
          steps {
            build job: "DNA/filtr-admin/${env.FILTR_ADMIN_BRANCH}", parameters: [string(name: 'ECR', value: '475275892927.dkr.ecr.us-east-1.amazonaws.com'), booleanParam(name: 'DEPLOY_TO_TEST', value: true)]
          }
        }
        stage('Private API') {
          steps {
            build job: "DNA/filtr-privateapi/${env.PRIVATE_API_BRANCH}", parameters: [string(name: 'ECR', value: '475275892927.dkr.ecr.us-east-1.amazonaws.com'), booleanParam(name: 'DEPLOY_TO_TEST', value: true)]
          }
        }
      }
    }
  }
}
