following is the sample file

#!/usr/bin/env groovy
// multi-branch pipline jenkinsfile
// through git-hook to trigg the matching branch
// building the tag version image if current commit has tags
pipeline {
    agent any
    options {
        skipDefaultCheckout false
    }
    triggers {
        // trigger the matching branch
        GenericTrigger (
            causeString: 'Triggered by develop',
            genericVariables: [[key: 'ref', value: '$.ref']],
            printContributedVariables: true,
            printPostContent: true,
            regexpFilterExpression: '^refs/heads/' + BRANCH_NAME + '$',
            regexpFilterText: '$ref',
            token: '7a09336d-946f-4c33-ad95-b58b1045c125'
        )
    }
    stages {
        stage('Initial') {
            steps {
                script {
                  // refresh local tags
                  sh '''git tag -l | xargs git tag -d'''
                  sh '''git pull origin ''' + BRANCH_NAME
                  sh '''git fetch origin --prune'''
                }
                echo gitTagName()
                echo 'Initialization stage'
            }
        }
        stage('test') {
              steps {
                echo 'Testing stage.'
              }
        }
        stage('build tag') {
          when {
            expression {
              gitTagName() != null
            }
          }
          steps {
             echo 'Building'
             script {
                 build(gitTagName())
                  def remote = [:]
                    remote.name = 'k8s-2'
                    remote.host = '127.0.0.1'
                    remote.user = 'root'
                    remote.password = 'root'
                    remote.allowAnyHosts = true
                  sshCommand remote: remote, command: "kubectl rollout restart deployment api-server"
             }
          }
          // catch
          post {
            aborted {
                echo "post condition executed on tag building: aborted ..."
            }
            failure {
                echo "post condition executed on tag building: failure ..."
            }
            success {
                echo "post condition executed on tag building: success ..."
            }
          }
        }
        stage('build latest') {
            steps {
                   echo 'Building'
                   script {
					 build('latest')
					  def remote = [:]
						remote.name = 'k8s-2'
						remote.host = '127.0.0.1'
						remote.user = 'root'
						remote.password = 'root'
						remote.allowAnyHosts = true
					  sshCommand remote: remote, command: "kubectl rollout restart deployment api-server"
                    }
                 }
            }
     }
}

void build(TAG_NAME) {
    sh '''
     #!/bin/sh
     DATE=`date +%m%d%H%M `
     DIR="."
     docker build -t api-server:${TAG_NAME} $DIR | tee $DIR/Docker_build_result.log
     RESULT=$(cat $DIR/Docker_build_result.log | tail -n 1)
    
     if [["$RESULT" != *Successfully*]];then
       exit -1
     fi
    
     echo '>>> Add tag to the new image'
     docker login -u admin -p password 127.0.0.1:10580
     docker tag api-server 127.0.0.1:10580/test/api-server:${TAG_NAME}
     echo '>>> To start push the new image'
     docker push 127.0.0.1:10580/test/api-server:${TAG_NAME}
 '''
}

/** @return The tag name, or `null` if the current commit isn't a tag. */
String gitTagName() {
    commit = getCommit()
    if (commit) {
        desc = sh(script: "git log -1 --decorate=short --oneline|grep -Eo 'tag: (.*)[,)]+'|awk '{print \$2}'|sed 's/)//g'|sed 's/,//g'", returnStdout: true)?.trim()
        if (null != desc && !"".equals(desc)) {
            return desc
        }
    }
    return null
}

/** @return The tag message, or `null` if the current commit isn't a tag. */
String gitTagMessage() {
    name = gitTagName()
    msg = sh(script: "git tag -n10000 -l ${name}", returnStdout: true)?.trim()
    if (msg) {
        return msg.substring(name.size()+1, msg.size())
    }
    return null
}

String getCommit() {
    return sh(script: 'git rev-parse HEAD', returnStdout: true)?.trim()
}

boolean isTag(String desc) {
    return desc =~ /^([1-9]\d|[1-9])(\.([1-9]\d|\d)){2}$/
}