// ## Jenkinsfile.wildfire.start-build  Start the primary build-test-deploy job while passing some parameters ##

pipeline {
  agent { label 'master' }
  parameters {
    choice(name: 'REPO_BRANCH_OR_TAG', \
                 choices: '\
develop\n\
develop5\n\
release-5.0.0\n\
5.0.0-rc1\n\
5.0.0-rc2\n\
5.0.0-rc3\n\
5.0.0-rc4\n\
5.0.0-rc5\n\
5.0.0\n\
Use Existing Distro\
', \
                 description: 'Repo branch or tag that is to be built.  Branches begin with develop, master, release or maint.  Tags are anything else or "Use Existing Distro" for no build.')
    run(name: 'USE_EXISTING_DISTRO', \
              projectName: 'Wildfire/Wildfire-Build-Test-Deploy', \
              filter: 'STABLE', \
              description: 'Existing distro to be used if "Use Existing Distro" is specified above.  Selected from prior STABLE job runs.')
    choice(name: 'BUILD_FROM_HEAD', \
                 choices: 'No\nYes', \
                 description: 'Use module source repo HEAD rather than tag?  NOTE: Only allowed for "develop" branch.')
    string(name: 'CODECEPT_OPTIONS', \
                 defaultValue: '', \
                 description: 'Additional codeception options.  "--xml" is always used.')
    choice(name: 'DO_LOCAL_TEST', \
                 choices: 'No\nYes', \
                 description: 'Should a local distro test be performed on the new distro?')
    choice(name: 'RUN_UNIT', \
                 choices: 'true\nfalse', \
                 description: 'Are unit test suites to be run?  For W5 this may result in an extra 30 minutes to install distro.')
    choice(name: 'RUN_SPECIAL_TESTS', \
                 choices: 'qa\n\nqa', \
                 description: 'Are QA tests to be run?  May not work for "Use Existing Distro".')
    choice(name: 'DEV', \
                 choices: 'Deploy and Test and Delete\n\nDeploy', \
                 description: 'What to do in the DEV environment.')
    choice(name: 'DEV_LOCATION', \
                 choices: 'da2\ndev\nda1', \
                 description: 'DEV server location: dev = dev-aegirmaster-01, da1 = dev-aegir-01, da2 = dev-aegir-02')
    string(name: 'TEST_MODULE', \
                 defaultValue: '', \
                 description: 'Distro module to be tested.  For example "custom/sbs_translation".  If blank, all modules will be tested.')
    string(name: 'TEST_FILE', \
                 defaultValue: '', \
                 description: 'Distro module test file to run.  For example "wildfireGoogleAnalyticsCest" or "wildfireGoogleAnalyticsCest:basicTest3".  If blank, all module test files will be run.')
    choice(name: 'SAVE_TEST_OUTPUT', \
                 choices: 'false\ntrue', \
                 description: 'Save test output in tar file?')
    choice(name: 'PROMPT_FOR_DEPLOY', \
                 choices: 'No\nYes', \
                 description: 'Wait at Deploy steps and prompt for Deploy or Abort job.')
    choice(name: 'WHEN_TESTS_FAIL', \
                 choices: 'Abort\nContinue', \
                 description: 'What to do if the distro test fails.')
    string(name: 'TEST_EMAIL_LIST', \
                 defaultValue: 'developers@kalaam.org', \
                 description: 'List of emails to notify with test results.')
  }
  options {
    buildDiscarder(logRotator(numToKeepStr:'5'))
    disableConcurrentBuilds()
    skipDefaultCheckout true
  }

  stages {

// Start a build job and exit
//          run(description: 'Existing distro to be used if "Use Existing Distro" is specified above.  Selected from prior STABLE job runs.', name: 'USE_EXISTING_DISTRO', runId: 'Wildfire/Wildfire-Build-Test-Deploy#952'), \

//    stage('Start Build-Test-Deploy Job on dev') {
//      steps {
//        echo 'Doing build with remote test dev ...'
//        build wait: false, job: 'Wildfire-Build-Test-Deploy', parameters: [ \
//          string(name: 'REPO_BRANCH_OR_TAG', value: params.REPO_BRANCH_OR_TAG), \
//          string(name: 'BUILD_FROM_HEAD', value: params.BUILD_FROM_HEAD), \
//          string(name: 'CODECEPT_OPTIONS', value: params.CODECEPT_OPTIONS), \
//          string(name: 'DO_LOCAL_TEST', value: params.DO_LOCAL_TEST), \
//          string(name: 'RUN_UNIT', value: params.RUN_UNIT), \
//          string(name: 'RUN_SPECIAL_TESTS', value: params.RUN_SPECIAL_TESTS), \
//          string(name: 'DEV', value: params.DEV), \
//          string(name: 'DEV_LOCATION', value: 'dev'), \
//          string(name: 'TEST_MODULE', value: params.TEST_MODULE), \
//          string(name: 'TEST_FILE', value: params.TEST_FILE), \
//          string(name: 'SAVE_TEST_OUTPUT', value: params.SAVE_TEST_OUTPUT), \
//          string(name: 'PROMPT_FOR_DEPLOY', value: params.PROMPT_FOR_DEPLOY), \
//          string(name: 'WHEN_TESTS_FAIL', value: params.WHEN_TESTS_FAIL), \
//          string(name: 'TEST_EMAIL_LIST', value: params.TEST_EMAIL_LIST)]
//        }
//      }

    stage('Start Build-Test-Deploy Job on da2') {
      steps {
        echo 'Doing build with remote test da2 ...'
        build wait: false, job: 'Wildfire-Build-Test-Deploy', parameters: [ \
          string(name: 'REPO_BRANCH_OR_TAG', value: params.REPO_BRANCH_OR_TAG), \
          string(name: 'BUILD_FROM_HEAD', value: params.BUILD_FROM_HEAD), \
          string(name: 'CODECEPT_OPTIONS', value: params.CODECEPT_OPTIONS), \
          string(name: 'DO_LOCAL_TEST', value: params.DO_LOCAL_TEST), \
          string(name: 'RUN_UNIT', value: params.RUN_UNIT), \
          string(name: 'RUN_SPECIAL_TESTS', value: params.RUN_SPECIAL_TESTS), \
          string(name: 'DEV', value: params.DEV), \
          string(name: 'DEV_LOCATION', value: params.DEV_LOCATION), \
          string(name: 'TEST_MODULE', value: params.TEST_MODULE), \
          string(name: 'TEST_FILE', value: params.TEST_FILE), \
          string(name: 'SAVE_TEST_OUTPUT', value: params.SAVE_TEST_OUTPUT), \
          string(name: 'PROMPT_FOR_DEPLOY', value: params.PROMPT_FOR_DEPLOY), \
          string(name: 'WHEN_TESTS_FAIL', value: params.WHEN_TESTS_FAIL), \
          string(name: 'TEST_EMAIL_LIST', value: params.TEST_EMAIL_LIST)]
        }
      }

    }

  post {
    always {
      echo 'always...'
    }
    success {
      echo 'success...'
//      mail to: "${EMAIL_LIST}",
//           subject: "SUCCESS: ${TEST_NAME}",
//           body: "See ${BUILD_URL}testReport for test results. ${EMAIL_COMMENT}"
    }
    unstable {
      echo 'unstable...'
//      mail to: "${EMAIL_LIST}",
//           subject: "FAILURE: ${TEST_NAME}",
//           body: "See ${BUILD_URL}testReport for test results. ${EMAIL_COMMENT}"
    }
    failure {
      echo 'failure...'
//      mail to: "michael@kalaam.org",
//           subject: "ERRORS: Build ${currentBuild.displayName}",
//           body: "See ${BUILD_URL} for build with errors."
    }
    changed {
      echo 'Something changed...'
    }

  }

}



