Skip to content

1. vue案例

1.1 创建pipeline

yaml
def git_address = "http://house.freehan.ink/root/vue3_web_element-demo-plus.git" 
def git_auth = "02d72dd6-0eab-4261-b0c0-12824e083421"

//构建版本的名称
def tag = "latest"

//Harbor私服地址
def harbor_url = "registry.cn-zhangjiakou.aliyuncs.com"

//Harbor的项目名称
def harbor_project_name = "vue-demo"

//Harbor的凭证
def harbor_auth = "1e7ee468-9da7-469b-a43a-xxx"



//启动时间
def start = new Date().format('yyyy-MM-dd HH:mm:ss')


//创建一个Pod的模板,label为jenkins-slave-vue
podTemplate(label: 'jenkins-slave-vue', cloud: 'kubernetes', containers: [
    containerTemplate(
        name: 'jnlp',
        image: "registry.cn-zhangjiakou.aliyuncs.com/hsuing/inbound-agent:latest-jdk11",
        ttyEnabled: true
    ),
    containerTemplate(
        name: 'docker',
        image: "registry.cn-zhangjiakou.aliyuncs.com/hsuing/docker:stable",
        ttyEnabled: true,
        command: 'cat'
    )
  ],
  volumes: [
	hostPathVolume(mountPath: '/var/run/docker.sock', hostPath:'/var/run/docker.sock'),
  ],
)

{
    
    //引用jenkins-slave-vue的pod模块来构建Jenkins-slave-vue的pod 
    node("jenkins-slave-vue"){
        stage('拉取代码'){
            checkout([$class: 'GitSCM', branches: [[name: '*/main']], extensions: [], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_address}"]]])
        }
        //定义newTag,全局变量
        def newTag = sh(returnStdout: true,script: 'echo `date +"%Y%m%d%H%M"_``git describe --tags --always`').trim()
        stage('编译描述') {
			// 自定义设置构建历史显示的名称和描述信息
			// 不同的部署方式设置构建历史显示的名称和描述信息方式不一样,根据自己的部署方式自行百度找到设置方法
			script {
				//设置buildName
				wrap([$class: 'BuildUser']) {
					//修改Description
					buildDescription "${BUILD_USER} > ${project_name} > ${branch}"
				}
			}
        }
        //
		stage('构建镜像'){
			
			//把选择的项目信息转为数组
			def selectedProjects = "${project_name}".split(',')
				for(int i=0;i<selectedProjects.size();i++){
					//取出每个项目的名称
					def currentProjectName = selectedProjects[i];
					//定义镜像名称
					def imageName = "${currentProjectName}:${tag}"
					//定义newTag
					//def newTag = sh(returnStdout: true,script: 'echo `date +"%Y%m%d%H%M"_``git describe --tags --always`').trim()
					//镜像编译
					//sh "sed -i 's#ACTIVEPROFILE#${springProfilesActive}#g' Dockerfile"
				container('docker') {
					sh "docker build -t ${imageName} ."
					//给镜像打标签
					sh "docker tag ${imageName} ${harbor_url}/hsuing/${harbor_project_name}:${newTag}"
					//登录Harbor,并上传镜像
					withCredentials([usernamePassword(credentialsId:"${harbor_auth}", passwordVariable: 'password', usernameVariable:'username')])
					{
						//登录
						sh "docker login -u ${username} -p ${password} ${harbor_url}"
						//上传镜像
						sh "docker push ${harbor_url}/hsuing/${harbor_project_name}:${newTag}"
					}
					//删除本地镜像
					sh "docker rmi -f ${imageName}"
					sh "docker rmi -f ${harbor_url}/hsuing/${harbor_project_name}:${newTag}"
				}
			}
          
			}
		
		//
        stage('部署到k8s平台'){
			def selectedProjects = "${project_name}".split(',')
			//定义newTag
		    //def newTag = sh(returnStdout: true,script: 'echo `date +"%Y%m%d%H%M"_``git describe --tags --always`').trim()
			for(int i=0;i<selectedProjects.size();i++){
				//取出每个项目的名称
				def currentProjectName = selectedProjects[i];

			    def deploy_image_name ="${harbor_url}/hsuing/${harbor_project_name}:${newTag}"
				//基于控制器的方式部署到K8S
				sh """
					sed -i 's#\$IMAGE_NAME#${deploy_image_name}#' deployment.yaml
					sed -i 's#\$APP_NAME#${currentProjectName}#'  deployment.yaml
					sed -i 's#\$APP_REPLICAS#${replicas}#' deployment.yaml
					sed -i 's#\$NAMESPACE#${namespaces}#' deployment.yaml
					sed -i 's#\$PODMEMORY#${podsMem}#' deployment.yaml
					sed -i 's#\$PODCPU#${podsCpu}#' deployment.yaml
					cat deployment.yaml
				"""
			}
				//部署到K8S
				kubernetesDeploy(kubeconfigId: "55a88b59-fd12-4bf0-ba57-8a664fed2f71", configs: "deployment.yaml")
			}
        }
    }
def git_address = "http://house.freehan.ink/root/vue3_web_element-demo-plus.git" 
def git_auth = "02d72dd6-0eab-4261-b0c0-12824e083421"

//构建版本的名称
def tag = "latest"

//Harbor私服地址
def harbor_url = "registry.cn-zhangjiakou.aliyuncs.com"

//Harbor的项目名称
def harbor_project_name = "vue-demo"

//Harbor的凭证
def harbor_auth = "1e7ee468-9da7-469b-a43a-xxx"



//启动时间
def start = new Date().format('yyyy-MM-dd HH:mm:ss')


//创建一个Pod的模板,label为jenkins-slave-vue
podTemplate(label: 'jenkins-slave-vue', cloud: 'kubernetes', containers: [
    containerTemplate(
        name: 'jnlp',
        image: "registry.cn-zhangjiakou.aliyuncs.com/hsuing/inbound-agent:latest-jdk11",
        ttyEnabled: true
    ),
    containerTemplate(
        name: 'docker',
        image: "registry.cn-zhangjiakou.aliyuncs.com/hsuing/docker:stable",
        ttyEnabled: true,
        command: 'cat'
    )
  ],
  volumes: [
	hostPathVolume(mountPath: '/var/run/docker.sock', hostPath:'/var/run/docker.sock'),
  ],
)

{
    
    //引用jenkins-slave-vue的pod模块来构建Jenkins-slave-vue的pod 
    node("jenkins-slave-vue"){
        stage('拉取代码'){
            checkout([$class: 'GitSCM', branches: [[name: '*/main']], extensions: [], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_address}"]]])
        }
        //定义newTag,全局变量
        def newTag = sh(returnStdout: true,script: 'echo `date +"%Y%m%d%H%M"_``git describe --tags --always`').trim()
        stage('编译描述') {
			// 自定义设置构建历史显示的名称和描述信息
			// 不同的部署方式设置构建历史显示的名称和描述信息方式不一样,根据自己的部署方式自行百度找到设置方法
			script {
				//设置buildName
				wrap([$class: 'BuildUser']) {
					//修改Description
					buildDescription "${BUILD_USER} > ${project_name} > ${branch}"
				}
			}
        }
        //
		stage('构建镜像'){
			
			//把选择的项目信息转为数组
			def selectedProjects = "${project_name}".split(',')
				for(int i=0;i<selectedProjects.size();i++){
					//取出每个项目的名称
					def currentProjectName = selectedProjects[i];
					//定义镜像名称
					def imageName = "${currentProjectName}:${tag}"
					//定义newTag
					//def newTag = sh(returnStdout: true,script: 'echo `date +"%Y%m%d%H%M"_``git describe --tags --always`').trim()
					//镜像编译
					//sh "sed -i 's#ACTIVEPROFILE#${springProfilesActive}#g' Dockerfile"
				container('docker') {
					sh "docker build -t ${imageName} ."
					//给镜像打标签
					sh "docker tag ${imageName} ${harbor_url}/hsuing/${harbor_project_name}:${newTag}"
					//登录Harbor,并上传镜像
					withCredentials([usernamePassword(credentialsId:"${harbor_auth}", passwordVariable: 'password', usernameVariable:'username')])
					{
						//登录
						sh "docker login -u ${username} -p ${password} ${harbor_url}"
						//上传镜像
						sh "docker push ${harbor_url}/hsuing/${harbor_project_name}:${newTag}"
					}
					//删除本地镜像
					sh "docker rmi -f ${imageName}"
					sh "docker rmi -f ${harbor_url}/hsuing/${harbor_project_name}:${newTag}"
				}
			}
          
			}
		
		//
        stage('部署到k8s平台'){
			def selectedProjects = "${project_name}".split(',')
			//定义newTag
		    //def newTag = sh(returnStdout: true,script: 'echo `date +"%Y%m%d%H%M"_``git describe --tags --always`').trim()
			for(int i=0;i<selectedProjects.size();i++){
				//取出每个项目的名称
				def currentProjectName = selectedProjects[i];

			    def deploy_image_name ="${harbor_url}/hsuing/${harbor_project_name}:${newTag}"
				//基于控制器的方式部署到K8S
				sh """
					sed -i 's#\$IMAGE_NAME#${deploy_image_name}#' deployment.yaml
					sed -i 's#\$APP_NAME#${currentProjectName}#'  deployment.yaml
					sed -i 's#\$APP_REPLICAS#${replicas}#' deployment.yaml
					sed -i 's#\$NAMESPACE#${namespaces}#' deployment.yaml
					sed -i 's#\$PODMEMORY#${podsMem}#' deployment.yaml
					sed -i 's#\$PODCPU#${podsCpu}#' deployment.yaml
					cat deployment.yaml
				"""
			}
				//部署到K8S
				kubernetesDeploy(kubeconfigId: "55a88b59-fd12-4bf0-ba57-8a664fed2f71", configs: "deployment.yaml")
			}
        }
    }

image-20240806215752988

  • 效果

image-20240806215820445