int total_timeout_minutes = 60
def knowhere_wheel=''
pipeline {
    agent {
        kubernetes {
            label 'knowhere-cpu-e2e'
            inheritFrom 'default'
            yamlFile 'ci/pod/cpu-e2e.yaml'
            defaultContainer 'main'
        }
    }
    options {
        timeout(time: total_timeout_minutes, unit: 'MINUTES')
        buildDiscarder logRotator(artifactDaysToKeepStr: '30')
        parallelsAlwaysFailFast()
        disableConcurrentBuilds(abortPrevious: true)

    }
    stages {
        stage("Build"){

            steps {
                script{
                    def date = sh(returnStdout: true, script: 'date +%Y%m%d').trim()
                    def gitShortCommit = sh(returnStdout: true, script: "echo ${env.GIT_COMMIT} | cut -b 1-7 ").trim()
                    version="${env.CHANGE_ID}.${date}.${gitShortCommit}"
                    sh "./build.sh -t Debug"
                    knowhere_wheel="knowhere-${version}-cp38-cp38-linux_x86_64.whl"
                    sh "cd python  && VERSION=${version} python3 setup.py bdist_wheel"
                    dir('python'){
                      archiveArtifacts artifacts: "dist/${knowhere_wheel}", followSymlinks: false
                    }
                }
            }
        }
        stage("Test"){
            steps {
                script{
                    checkout([$class: 'GitSCM', branches: [[name: '*/main']], extensions: [],
                    userRemoteConfigs: [[credentialsId: 'milvus-ci', url: 'https://github.com/milvus-io/knowhere-test.git']]])
                    dir('tests'){
                      unarchive mapping: ["dist/${knowhere_wheel}": "${knowhere_wheel}"]
                      sh "ls -lah"
                      sh "pip3 install ${knowhere_wheel} \
                          && pip3 install -r requirements.txt &&  pytest -v -m recall"
                    }
                     sh 'cp /tmp/knowhere_ci.log knowhere_ci.log'
                     archiveArtifacts artifacts: 'knowhere_ci.log', followSymlinks: false
                }
            }
        }

    }
}
