@Library('gm-pipeline-library') _

pipeline {
    agent any

    options {
        // Console output add timestamps
        timestamps()
         // Disallow concurrent executions of the Pipeline
        disableConcurrentBuilds()
        // On failure, retry the entire Pipeline the specified number of times.
        retry(1)
    }

    parameters {
        choice(name: 'CACHE', choices: ['', '--no-cache'], description: 'docker build 是否使用cache,默认使用,不使用为--no-cache')
    }

    environment {
        // Image Tag  branch.time.hash
        TAG = dockerTag()
        // Image Full Tag
        IMAGE = "${DOCKER_REGISTRY}/gm-backend/mentha:$TAG"
    }

    stages {
        stage("Begin") {
            steps {
                dingNotify "before"
            }
        }
        stage('Check Code') {
            //when { branch 'test' }
            steps {
                script {
                    currentBuild.displayName = "Check-Code"
                    checkCode "3.6"
                }
            }
        }
        stage('Build Images') {
            steps {
                script {
                    currentBuild.displayName = "Build-Image"
                }
                sh "docker build . ${params.CACHE} -t $IMAGE"
                sh "docker push $IMAGE"
            }
        }
    }

    post {
        always {
            script {
                def stage = "${currentBuild.displayName}"
                if (stage == "Check-Code" && fileExists("./flake.output.txt")) {
                    def description = readFile("./flake.output.txt")
                    dingNotify "after", "${currentBuild.currentResult}", "${description}"
                } else {
                    dingNotify "after", "${currentBuild.currentResult}"
                }
            }
            recordIssues enabledForFailure: true, tools: [flake8()]
        }
    }
}
