@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/autotest-backend:$TAG"

        ALIAS = "${DOCKER_REGISTRY}/gm-backend/autotest-backend:v1.0.0"
    }

    stages {
        stage("Begin") {
            steps {
                dingNotify "before"
            }
        }
        stage('Build Image') {
            steps {
                sh "docker build . ${params.CACHE} -t $IMAGE -f ./Dockerfile"
                sh "docker push $IMAGE"

                sh "docker tag $IMAGE $ALIAS && docker push $ALIAS"
            }
        }
    }

    post {
        always {
            dingNotify "after", "${currentBuild.currentResult}"
        }
    }
}