//前端项目打包构建;支持多环境 pipeline { agent any environment { //GIT路径 GIT_PATH = "threegene/dev/zproduct/server/demo.git" //项目名称, 使用Job名称作为项目名称 PROJECT_NAME = "${JOB_NAME}" //自定义项目名称 //PROJECT_NAME = "threegene-livex-center-html" //镜像分组,如果空则采用服务名前缀作为分组 //IMAGE_GROUP = "" } stages { stage('Init') { steps { script{ dev_imgFile = "" prd_imgFile = "" _imageTagForTest = "" _imageTag = "" _imageGroup = env.IMAGE_GROUP _dockerFile = "nginx_dockerfile" _envs = ["prod","test"] _envTexts = [prod:'生产环境',test:'测试环境'] //if(!params.build_prd){ // _envs = ["test"] //} if(_imageGroup==null || _imageGroup==""){ _imageGroup = "${PROJECT_NAME.split('-')[0]}" } } } } stage('Checkout') { steps { echo "git: ${GIT_PATH}" echo "branche: ${branch_or_tag}" // https://plugins.jenkins.io/git-parameter/ checkout([$class: 'GitSCM', branches: [[name: "${branch_or_tag}"]], extensions: [], userRemoteConfigs: [[credentialsId: 'gitlab-ssh', url: "${GIT_HOST}:${GIT_PATH}"]] ]) echo "code checkout completed" } } stage('构建&生成镜像') { when { expression { params.branch_or_tag != "" } } steps { script{ //nodejs('node10.16.0') {} nodejs('node14.17.3') {} def workDic = "${WORKSPACE}" def imgName = "/${_imageGroup}/${PROJECT_NAME}" sh "npm install" //sh "npm --registry https://registry.npmmirror.com install express" // sh "npm --registry http://oss.npmmirror.com install express" for(env in _envs){ echo "build: ${env}" if(env=="test"){ sh "npm run build:${env}" }else{ sh "npm run build" } def imageTag = sh(script:"date '+%Y%m%d%H%M%S'", returnStdout: true).replace("\n","") def imgFile = "${IMAGE_HOST_TEST}${imgName}:${imageTag}" if(env=="test"){ _imageTagForTest = imageTag dev_imgFile = "${IMAGE_HOST_TEST}${imgName}:${_imageTagForTest}" dir("${workDic}/"){ sh "docker build . -f ${JENKINS_HOME}/${_dockerFile} -t ${dev_imgFile}" sh "docker push ${dev_imgFile}" sh "docker rmi ${dev_imgFile}" } }else{ _imageTag = imageTag prd_imgFile = "${IMAGE_HOST_PRD}${imgName}:${_imageTag}" dir("${workDic}/"){ sh "docker build . -f ${JENKINS_HOME}/${_dockerFile} -t ${prd_imgFile}" //sh "docker push ${prd_imgFile}" } } def envText = _envTexts[env] echo "${envText}镜像标签: ${imageTag}" echo "${envText}镜像: ${imgFile}" } } } } stage('部署测试环境') { when { expression { _imageTagForTest != "" } } steps { script{ def k8sConf = "k8s-deploy-scripts/test-${PROJECT_NAME}.yaml" sh "sed -i '/${IMAGE_HOST_TEST}/s/[0-9]\\{14\\}/${_imageTagForTest}/' /opt/${k8sConf}" sh(script: "ssh ${K8S_HOST} kubectl apply -f /data/${k8sConf}") //echo "部署镜像标签: ${_imageTagForTest}" } } } stage('确认上传线上包') { when { allOf { expression { params.branch_or_tag == 'origin/release' }; expression { prd_imgFile != "" } } } stages { stage('input') { input { message '请选择是否发布线上环境,勾选isUpload提交表示发布线上,不勾选反之' id 'upload-package' ok '提交' parameters { booleanParam defaultValue: false, description: '', name: 'isUpload' } } steps { script{ if(isUpload == 'false'){ echo "上传线上仓库?: ${isUpload}" sh "docker rmi ${prd_imgFile}" echo "测试镜像版本 :${dev_imgFile}" }else{ echo "上传线上仓库?: ${isUpload}" sh "docker push ${prd_imgFile}" sh "docker rmi ${prd_imgFile}" echo "线上镜像版本 :${prd_imgFile}" } } } } } } } }
标签:prd,部署,前端,imgFile,echo,imageTag,sh,docker,k8s From: https://www.cnblogs.com/kxtomato/p/17296990.html