pipeline {
    agent none

    options {
        timestamps()
    }

    environment {
        PROJECT_NAME = "milvus"
        LOWER_BUILD_TYPE = BUILD_TYPE.toLowerCase()
        SEMVER = "${env.gitlabSourceBranch == null ? params.ENGINE_BRANCH.substring(params.ENGINE_BRANCH.lastIndexOf('/') + 1) : env.gitlabSourceBranch}"
        GITLAB_AFTER_COMMIT = "${env.gitlabAfter == null ? null : env.gitlabAfter}"
        SUFFIX_VERSION_NAME = "${env.gitlabAfter == null ? null : env.gitlabAfter.substring(0, 6)}"
        DOCKER_VERSION_STR = "${env.gitlabAfter == null ? "${SEMVER}-${LOWER_BUILD_TYPE}" : "${SEMVER}-${LOWER_BUILD_TYPE}-${SUFFIX_VERSION_NAME}"}"
    }

    stages {
        stage("Ubuntu 16.04") {
            environment {
                PACKAGE_VERSION = VersionNumber([
                    versionNumberString : '${SEMVER}-${LOWER_BUILD_TYPE}-${BUILD_DATE_FORMATTED, "yyyyMMdd"}'
                ]);

                DOCKER_VERSION = VersionNumber([
                    versionNumberString : '${DOCKER_VERSION_STR}'
                ]);
            }

            stages {
                stage("Run Build") {
                    agent {
                        kubernetes {
                            cloud 'build-kubernetes'
                            label 'build'
                            defaultContainer 'jnlp'
                            yaml """
apiVersion: v1
kind: Pod
metadata:
  name: milvus-build-env
  labels:
    app: milvus
    componet: build-env
spec:
  containers:
  - name: milvus-build-env
    image: registry.zilliz.com/milvus/milvus-build-env:v0.13
    env:
    - name: POD_IP
      valueFrom:
        fieldRef:
          fieldPath: status.podIP
    command:
    - cat
    tty: true
    resources:
      limits:
        memory: "28Gi"
        cpu: "10.0"
        nvidia.com/gpu: 1
      requests:
        memory: "14Gi"
        cpu: "5.0"
  - name: milvus-mysql
    image: mysql:5.6
    env:
    - name: MYSQL_ROOT_PASSWORD
      value: 123456
    ports:
    - containerPort: 3306
      name: mysql
"""
                        }
                    }
                    stages {
                        stage('Build') {
                            steps {
                                gitlabCommitStatus(name: 'Build') {
                                    script {
                                        load "${env.WORKSPACE}/ci/jenkinsfile/milvus_build_no_ut.groovy"
                                        load "${env.WORKSPACE}/ci/jenkinsfile/packaged_milvus_no_ut.groovy"
                                    }
                                }
                            }
                        }
                    }
                    post {
                        aborted {
                            script {
                                updateGitlabCommitStatus name: 'Build', state: 'canceled'
                                echo "Milvus Build aborted !"
                            }
                        }

                        failure {
                            script {
                                updateGitlabCommitStatus name: 'Build', state: 'failed'
                                echo "Milvus Build failure !"
                            }
                        }
                    }
                }

                stage("Publish docker and helm") {
                    agent {
                        kubernetes {
                            label 'publish'
                            defaultContainer 'jnlp'
                            yaml """
apiVersion: v1
kind: Pod
metadata:
  labels:
    app: publish
    componet: docker
spec:
  containers:
  - name: publish-docker
    image: registry.zilliz.com/library/zilliz_docker:v1.0.0
    securityContext:
      privileged: true
    command:
    - cat
    tty: true
    volumeMounts:
    - name: docker-sock
      mountPath: /var/run/docker.sock
  volumes:
  - name: docker-sock
    hostPath:
      path: /var/run/docker.sock
"""
                        }
                    }
                    stages {
                        stage('Publish Docker') {
                            steps {
                                gitlabCommitStatus(name: 'Publish Docker') {
                                    script {
                                        load "${env.WORKSPACE}/ci/jenkinsfile/publish_docker.groovy"
                                    }
                                }
                            }
                        }
                    }
                    post {
                        aborted {
                            script {
                                updateGitlabCommitStatus name: 'Publish Docker', state: 'canceled'
                                echo "Milvus Publish Docker aborted !"
                            }
                        }

                        failure {
                            script {
                                updateGitlabCommitStatus name: 'Publish Docker', state: 'failed'
                                echo "Milvus Publish Docker failure !"
                            }
                        }
                    }
                }

                stage("Deploy to Development") {
                    parallel {
                        stage("Single Node") {
                            agent {
                                kubernetes {
                                    label 'dev-test'
                                    defaultContainer 'jnlp'
                                    yaml """
apiVersion: v1
kind: Pod
metadata:
  labels:
    app: milvus
    componet: test
spec:
  containers:
  - name: milvus-testframework
    image: registry.zilliz.com/milvus/milvus-test:v0.2
    command:
    - cat
    tty: true
    volumeMounts:
    - name: kubeconf
      mountPath: /root/.kube/
      readOnly: true
  volumes:
  - name: kubeconf
    secret:
      secretName: test-cluster-config
"""
                                }
                            }

                            stages {
                                stage("Deploy to Dev") {
                                    steps {
                                        gitlabCommitStatus(name: 'Deloy to Dev') {
                                            container('milvus-testframework') {
                                                script {
                                                    load "${env.WORKSPACE}/ci/jenkinsfile/deploy2dev.groovy"
                                                }
                                            }
                                        }
                                    }
                                }
                                stage("Dev Test") {
                                    steps {
                                        gitlabCommitStatus(name: 'Deloy Test') {
                                            container('milvus-testframework') {
                                                script {
                                                    load "${env.WORKSPACE}/ci/jenkinsfile/dev_test.groovy"
                                                    load "${env.WORKSPACE}/ci/jenkinsfile/upload_dev_test_out.groovy"
                                                }
                                            }
                                        }
                                    }
                                }
                                stage ("Cleanup Dev") {
                                    steps {
                                        gitlabCommitStatus(name: 'Cleanup Dev') {
                                            container('milvus-testframework') {
                                                script {
                                                    load "${env.WORKSPACE}/ci/jenkinsfile/cleanup_dev.groovy"
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            post {
                                always {
                                    container('milvus-testframework') {
                                        script {
                                            load "${env.WORKSPACE}/ci/jenkinsfile/cleanup_dev.groovy"
                                        }
                                    }
                                }
                                success {
                                    script {
                                        echo "Milvus Single Node CI/CD success !"
                                    }
                                }
                                aborted {
                                    script {
                                        echo "Milvus Single Node CI/CD aborted !"
                                    }
                                }
                                failure {
                                    script {
                                        echo "Milvus Single Node CI/CD failure !"
                                    }
                                }
                            }
                        }

//                         stage("Cluster") {
//                             agent {
//                                 kubernetes {
//                                     label 'dev-test'
//                                     defaultContainer 'jnlp'
//                                     yaml """
// apiVersion: v1
// kind: Pod
// metadata:
//   labels:
//     app: milvus
//     componet: test
// spec:
//   containers:
//   - name: milvus-testframework
//     image: registry.zilliz.com/milvus/milvus-test:v0.2
//     command:
//     - cat
//     tty: true
//     volumeMounts:
//     - name: kubeconf
//       mountPath: /root/.kube/
//       readOnly: true
//   volumes:
//   - name: kubeconf
//     secret:
//       secretName: test-cluster-config
// """
//                                 }
//                             }
//                             stages {
//                                 stage("Deploy to Dev") {
//                                     steps {
//                                         gitlabCommitStatus(name: 'Deloy to Dev') {
//                                             container('milvus-testframework') {
//                                                 script {
//                                                     load "${env.WORKSPACE}/ci/jenkinsfile/cluster_deploy2dev.groovy"
//                                                 }
//                                             }
//                                         }
//                                     }
//                                 }
//                                 stage("Dev Test") {
//                                     steps {
//                                         gitlabCommitStatus(name: 'Deloy Test') {
//                                             container('milvus-testframework') {
//                                                 script {
//                                                     load "${env.WORKSPACE}/ci/jenkinsfile/cluster_dev_test.groovy"
//                                                     load "${env.WORKSPACE}/ci/jenkinsfile/upload_dev_cluster_test_out.groovy"
//                                                 }
//                                             }
//                                         }
//                                     }
//                                 }
//                                 stage ("Cleanup Dev") {
//                                     steps {
//                                         gitlabCommitStatus(name: 'Cleanup Dev') {
//                                             container('milvus-testframework') {
//                                                 script {
//                                                     load "${env.WORKSPACE}/ci/jenkinsfile/cluster_cleanup_dev.groovy"
//                                                 }
//                                             }
//                                         }
//                                     }
//                                 }
//                             }
//                             post {
//                                 always {
//                                     container('milvus-testframework') {
//                                         script {
//                                             load "${env.WORKSPACE}/ci/jenkinsfile/cluster_cleanup_dev.groovy"
//                                         }
//                                     }
//                                 }
//                                 success {
//                                     script {
//                                         echo "Milvus Cluster CI/CD success !"
//                                     }
//                                 }
//                                 aborted {
//                                     script {
//                                         echo "Milvus Cluster CI/CD aborted !"
//                                     }
//                                 }
//                                 failure {
//                                     script {
//                                         echo "Milvus Cluster CI/CD failure !"
//                                     }
//                                 }
//                             }
//                         }
                    }
                }
            }
        }
    }

    post {
        always {
            script {
                if (env.gitlabAfter != null) {
                    if (!currentBuild.resultIsBetterOrEqualTo('SUCCESS')) {
                        // Send an email only if the build status has changed from green/unstable to red
                        emailext subject: '$DEFAULT_SUBJECT',
                        body: '$DEFAULT_CONTENT',
                        recipientProviders: [
                            [$class: 'DevelopersRecipientProvider'],
                            [$class: 'RequesterRecipientProvider']
                        ],
                        replyTo: '$DEFAULT_REPLYTO',
                        to: '$DEFAULT_RECIPIENTS'
                    }
                }
            }
        }

        success {
            script {
                updateGitlabCommitStatus name: 'CI/CD', state: 'success'
                echo "Milvus CI/CD success !"
            }
        }

        aborted {
            script {
                updateGitlabCommitStatus name: 'CI/CD', state: 'canceled'
                echo "Milvus CI/CD aborted !"
            }
        }

        failure {
            script {
                updateGitlabCommitStatus name: 'CI/CD', state: 'failed'
                echo "Milvus CI/CD failure !"
            }
        }
    }
}

