def cat_id = ""

def notifyBuild(status) {
    if (status == null) {
        status = "SUCCESS"
    }
    def msg = """Job Name: ${env.JOB_NAME}
Build Number: ${env.BUILD_NUMBER}
Build Status: $status
Build URL: ${env.BUILD_URL}
"""
    slackSend(message: msg)
}

node {
    try {
        stage("create cat") {
            script {
                def res = build(job: "create-cat", parameters: [
                        string(name: "release", value: release),
                        string(name: "pd_hash", value: pd_hash),
                        string(name: "tikv_hash", value: tikv_hash),
                        string(name: "tidb_hash", value: tidb_hash),
                ])
                def buildVars = res.getBuildVariables()
                cat_id = buildVars.CAT_ID
            }
        }
        stage("execute once simple cases with cat") {
            build(job: "execute-once-simple-cases-with-cat", parameters: [
                    string(name: "cat_id", value: cat_id),
                    string(name: "release", value: release),
            ])
        }
        stage("delete cat") {
            build(job: "delete-cat", parameters: [string(name: "cat_id", value: cat_id)])
        }
    } catch (e) {
        currentBuild.result = "FAILURE"
        throw e
    } finally {
        notifyBuild(currentBuild.result)
    }
}
