def getHost(){ def remote = [:] remote.name = 'eip' remote.host = '10.2.1.254' remote.user = 'root' remote.port = 22 remote.password = 'bpaadmin' remote.allowAnyHosts = true return remote } pipeline{ agent any parameters { choice( description: '前端or后端', name: 'projectqh', choices: ['backend','frontend'] ) choice( description: '执行操作(发布|回滚)', name: 'operation', choices: ['develop', 'rollback'] ) choice( description: '分支名', name: 'branchName', choices: ['master'] ) } 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{ script { if(params.branchName!= 'master') { build_tag = "${params.branchName}-${params.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') { if(params.projectqh=='backend') { sh "cp -r backend/* ./" sh "docker build -f backend/dockerfile -t 10.2.1.24:10242/bpa/finalapi:${build_tag} ." } else if(params.projectqh=='frontend') { sh "docker build -f frontend/dockerfile -t 10.2.1.24:10242/bpa/finalweb:${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') { if(params.projectqh=='backend') { sh "docker push 10.2.1.24:10242/bpa/finalapi:${build_tag}" } else if(params.projectqh=='frontend') { sh "docker push 10.2.1.24:10242/bpa/finalweb:${build_tag}" } } } } } } stage('Deploy') { steps { script{ echo "5. Deploy Stage" // if(params.environment=='test') // { // input "确认要部署线上环境吗?" // } if(params.projectqh=='backend') { server = getHost() sshCommand remote: server, command: """ /home/finalmes/api/install.sh ${build_tag} """ } else if(params.projectqh=='frontend') { server = getHost() sshCommand remote: server, command: """ /home/finalmes/web/install.sh ${build_tag} """ } } } } } }