pipeline {
    agent none

    options {
        timestamps()
    }

    environment {
        SRC_BRANCH = "master"
        IMAGE_TAG = "${params.IMAGE_TAG}-release"
        HELM_BRANCH = "${params.IMAGE_TAG}" 
        TEST_URL = "git@192.168.1.105:Test/milvus-java-test.git"
        TEST_BRANCH = "${params.IMAGE_TAG}"
    }

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

            stages {
                stage("Deploy Server") {
                    steps {
                        gitlabCommitStatus(name: 'Deloy Server') {
                            container('milvus-testframework-java') {
                                script {
                                    load "${env.WORKSPACE}/milvus-java-test/ci/jenkinsfile/deploy_server.groovy"
                                }
                            }
                        }
                    }
                }
                stage("Integration Test") {
                    steps {
                        gitlabCommitStatus(name: 'Integration Test') {
                            container('milvus-testframework-java') {
                                script {
                                    print "In integration test stage"
                                    load "${env.WORKSPACE}/milvus-java-test/ci/jenkinsfile/integration_test.groovy"
                                }
                            }
                        }
                    }
                }
                stage ("Cleanup Env") {
                    steps {
                        gitlabCommitStatus(name: 'Cleanup Env') {
                            container('milvus-testframework-java') {
                                script {
                                    load "${env.WORKSPACE}/milvus-java-test/ci/jenkinsfile/cleanup.groovy"
                                }
                            }
                        }
                    }
                }
            }
            post {
                always {
                    container('milvus-testframework-java') {
                        script {
                            load "${env.WORKSPACE}/milvus-java-test/ci/jenkinsfile/cleanup.groovy"
                        }
                    }
                }
                success {
                    script {
                        echo "Milvus java-sdk test success !"
                    }
                }
                aborted {
                    script {
                        echo "Milvus java-sdk test aborted !"
                    }
                }
                failure {
                    script {
                        echo "Milvus java-sdk test failed !"
                    }
                }
            }
        }
    }
}
