// -*- mode: groovy -*-

// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.
//
// Jenkins pipeline
// See documents at https://jenkins.io/doc/book/pipeline/jenkinsfile/

// timeout in minutes
max_time = 240

node('utility') {
  // Loading the utilities requires a node context unfortunately
  checkout scm
  utils = load('ci/Jenkinsfile_utils.groovy')
  custom_steps = load('ci/jenkins/Jenkins_steps.groovy')
}
utils.assign_node_labels(utility: 'utility', linux_cpu: 'mxnetlinux-cpu')

utils.main_wrapper(
core_logic: {
  utils.parallel_stage('Build', [
    custom_steps.compile_unix_cpu_openblas(),
    custom_steps.compile_unix_openblas_debug_cpu(),
    custom_steps.compile_unix_mkl_cpu(),
    custom_steps.compile_unix_mkldnn_cpu(),
    custom_steps.compile_unix_mkldnn_mkl_cpu(),
    custom_steps.compile_unix_int64_cpu(),
    custom_steps.compile_unix_openblas_cpu_no_tvm_op(),
  ])

  utils.parallel_stage('Tests', [
    custom_steps.test_unix_python3_cpu(),
    custom_steps.test_unix_python3_debug_cpu(),
    custom_steps.test_unix_python3_mkl_cpu(),
    custom_steps.test_unix_python3_mkldnn_cpu(),
    custom_steps.test_unix_python3_mkldnn_mkl_cpu(),
    custom_steps.test_unix_scala_cpu(),
    custom_steps.test_unix_scala_mkldnn_cpu(),
    custom_steps.test_unix_clojure_cpu(),
    custom_steps.test_unix_clojure_integration_cpu(),
    custom_steps.test_unix_perl_cpu(),
    custom_steps.test_unix_r_cpu(),
    custom_steps.test_unix_r_mkldnn_cpu(),
    custom_steps.test_unix_julia07_cpu(),
    custom_steps.test_unix_julia10_cpu(),
    custom_steps.test_unix_onnx_cpu(),
    custom_steps.test_unix_cpp_cpu(),
    custom_steps.test_static_scala_cpu(),
    custom_steps.test_static_python_cpu(),
    custom_steps.test_static_python_cpu_cmake(),
    /*  Disabled due to master build failure:
     *  http://jenkins.mxnet-ci.amazon-ml.com/blue/organizations/jenkins/incubator-mxnet/detail/master/1221/pipeline/
     *  https://github.com/apache/incubator-mxnet/issues/11801
    custom_steps.test_unix_distributed_kvstore_cpu()
    */
    custom_steps.test_unix_python3_cpu_no_tvm_op(),
  ])
}
,
failure_handler: {
  // Only send email if master or release branches failed
  if (currentBuild.result == "FAILURE" && (env.BRANCH_NAME == "master" || env.BRANCH_NAME.startsWith("v"))) {
    emailext body: 'Build for MXNet branch ${BRANCH_NAME} has broken. Please view the build at ${BUILD_URL}', replyTo: '${EMAIL}', subject: '[BUILD FAILED] Branch ${BRANCH_NAME} build ${BUILD_NUMBER}', to: '${EMAIL}'
  }
}
)
