
def getHost(){
    def remote = [:]
    remote.name = 'eip'
    remote.host = '10.6.1.50'
    remote.user = 'root'
    remote.port = 22
    remote.password = '120962839'
    remote.allowAnyHosts = true
    return remote
}

pipeline{
  agent any
  parameters {
      choice(
      description: 'EIP环境',
      name: 'environment',
      choices: ['saasdev1']
    )

    choice(
        description: '执行操作(发布|回滚)',
        name: 'operation',
        choices: ['develop', 'rollback']
    )

    choice(
        description: '是否下载包',
        name: 'isDownloadPackage',
        choices: ['否','是']
    )

    string(
        name: 'tag',
        defaultValue: '',
        description: '版本tag'
    )

  }

    stages
    {

    stage('Prepare') {
         steps {
           script{
                echo "1.Prepare Stage"
	    	        echo "当前环境${params.environment}"
	            	if(params.operation=='develop')
	              	{
	              	 checkout scm
                   script {
                             build_tag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
                              if (env.BRANCH_NAME != 'master') {
                              build_tag = "${env.BRANCH_NAME}-${build_tag}"
                              }
                         }
              		}
	            	else{
                   build_tag="${params.tag}"
		                }
                }
           }
    }
    stage('Test') {
                  steps {
                     script{
                        echo "2.Test Stage"
                      }
                    }

    }
    stage('Build') {
                    steps {
                      script{
                        echo "3.Build Docker Image Stage"
                      	if(params.operation=='develop')
	                      	{
                              nodejs("nodejs") {

                              }
                              if(params.isDownloadPackage=='是')
                              {
                              sh "npm install --unsafe-perm=true --allow-root"
                               sh "rm -rf ./dist/*"
                              sh "npm run build"
                              }
	                      	    sh "docker build -t 10.2.1.24:10242/bpa/kitchenmanageweb:${build_tag} ."
                      		}
                        }
                      }

       }





    stage('Push') {
                    steps {
                      script{
                       echo "4.Push Docker Image Stage"
                          withCredentials([usernamePassword(credentialsId: 'harbor', passwordVariable: 'harborPassword', usernameVariable: 'harborUser')]) {
                           sh "docker login -u ${harborUser} -p ${harborPassword} 10.2.1.24:10242"
	                        	if(params.operation=='develop')
	                          	{
                                 sh "docker push 10.2.1.24:10242/bpa/kitchenmanageweb:${build_tag}"
                              }
	                      	}
                        }
                      }




    }


    stage('Deploy') {

                    steps {
                      script{
                         echo "5. Deploy Stage"
                      //       server = getHost()
                      //  sshCommand remote: server, command: """
                      //  /root/eip/web/shell/linux-eipweb.install.sh eipweb ${build_tag} 80
                      //  """

                             sh "sed -i 's/<BUILD_TAG>/${build_tag}/' k8s_kitchenmanageweb.yaml"
                             sh "sed -i 's/<BRANCH_NAME>/${env.BRANCH_NAME}/' k8s_kitchenmanageweb.yaml"
                             sh "sed -i 's/<NAMESPACES>/${params.environment}/' k8s_kitchenmanageweb.yaml"
                             sh "kubectl apply -f k8s_kitchenmanageweb.yaml "
                         }



                        }
                      }
    }

}
