pipeline{ agent any parameters { choice( description: 'pztjwebi:开发环境1', name: 'environment', choices: ['pztj'] ) choice( description: '前端or后端', name: 'projectqh', choices: ['backend','frontend'] ) choice( description: '执行操作(发布|回滚)', name: 'operation', choices: ['develop', 'rollback'] ) choice( description: '分支名', name: 'branchName', choices: ['master'] ) 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{ 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/pztjapi:${build_tag} ." } else if(params.projectqh=='frontend') { sh "docker build -f frontend/dockerfile -t 10.2.1.24:10242/bpa/pztjweb:${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/pztjapi:${build_tag}" } else if(params.projectqh=='frontend') { sh "docker push 10.2.1.24:10242/bpa/pztjweb:${build_tag}" } } } } } } stage('Deploy') { steps { script{ echo "5. Deploy Stage" // if(params.environment=='test') // { // input "确认要部署线上环境吗?" // } if(params.projectqh=='backend') { sh "sed -i 's//${build_tag}/' k8s_pztjapi.yaml" sh "sed -i 's//${env.BRANCH_NAME}/' k8s_pztjapi.yaml" sh "sed -i 's//${params.environment}/' k8s_pztjapi.yaml" sh "kubectl apply -f k8s_pztjapi.yaml --record" } else if(params.projectqh=='frontend') { sh "sed -i 's//${build_tag}/' k8s_pztjweb.yaml" sh "sed -i 's//${env.BRANCH_NAME}/' k8s_pztjweb.yaml" sh "sed -i 's//${params.environment}/' k8s_pztjweb.yaml" sh "kubectl apply -f k8s_pztjweb.yaml --record" } } } } } }