jenkins-pipeline流水线构建示例

pipeline {
    agent any
    stages {
        
        stage('拉取代码') {
            agent none
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '68f2087f-a034-4d39-a9ff-1f776dd3dfa8', url: 'https://gitee.com/leifengyang/yygh-parent.git']]])
            }
        }
        
        stage('项目编译') {
            agent none
            steps {
                sh 'mvn clean package -Dmaven.test.skip=true'
                
            }
        }
        
        stage('构建镜像') {
            parallel {
                stage('构建hospital-manage镜像') {
                    agent none
                    steps {
                        sh 'ls hospital-manage/target'
                        sh 'docker build -t hospital-manage:latest -f hospital-manage/Dockerfile  ./hospital-manage/'
                    }
                }
				stage('构建server-gateway镜像') {
                    agent none
                    steps {
                        sh 'ls server-gateway/target'
                        sh 'docker build -t server-gateway:latest -f server-gateway/Dockerfile  ./server-gateway/'
                    }
                }
            }
        }
        
        stage('镜像推送') {
            parallel {
                stage('推送hospital-manage镜像') {
                    agent none
                    steps {
                        withCredentials([usernamePassword(credentialsId : 'REGISTRY_USER' ,usernameVariable : 'DOCKER_USER_VAR' ,passwordVariable : 'DOCKER_PWD_VAR' ,)]) {
                            sh 'echo "$DOCKER_PWD_VAR" | docker login $REGISTRY -u "$DOCKER_USER_VAR" --password-stdin'
                            sh 'docker tag hospital-manage:latest $REGISTRY/$DOCKERHUB_NAMESPACE/hospital-manage:SNAPSHOT'
                            sh 'docker push  $REGISTRY/$DOCKERHUB_NAMESPACE/hospital-manage:SNAPSHOT'
							//删除本地镜像
							sh "docker rmi -f hospital-manage:latest"
							sh "docker rmi -f $REGISTRY/$DOCKERHUB_NAMESPACE/hospital-manage:SNAPSHOT"
                        }
                    }
                }
				
				stage('推送server-gateway镜像') {
					agent none
					steps {
						withCredentials([usernamePassword(credentialsId : 'REGISTRY_USER' ,usernameVariable : 'DOCKER_USER_VAR' ,passwordVariable : 'DOCKER_PWD_VAR' ,)]) {
						    sh 'echo "$DOCKER_PWD_VAR" | docker login $REGISTRY -u "$DOCKER_USER_VAR" --password-stdin'
							sh 'docker tag server-gateway:latest $REGISTRY/$DOCKERHUB_NAMESPACE/server-gateway:SNAPSHOT'
							sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/server-gateway:SNAPSHOT'
							//删除本地镜像
							sh "docker rmi -f server-gateway:latest"
							sh "docker rmi -f $REGISTRY/$DOCKERHUB_NAMESPACE/server-gateway:SNAPSHOT"
					    }
                    }
				}
            }
        }
        
        stage('init-server'){
            steps {
                script {                 
                   server = getServer()                                   
                }
            }
        }
		
		stage('项目部署') {
            parallel {
                
                stage('hospital-manage部署到dev环境') {
                    agent none			
                    steps {
					    withCredentials([usernamePassword(credentialsId : 'REGISTRY_USER' ,usernameVariable : 'DOCKER_USER_VAR' ,passwordVariable : 'DOCKER_PWD_VAR' ,)]) {
					        sshCommand remote: server, command: "echo '$DOCKER_PWD_VAR' | docker login $REGISTRY -u '$DOCKER_USER_VAR' --password-stdin"
                            // 更新或下载镜像
                            sshCommand remote: server, command: "docker pull $REGISTRY/$DOCKERHUB_NAMESPACE/server-gateway:SNAPSHOT"
                        }	
                    }
                }	 
            }
        }
    }
    environment {
        DOCKER_CREDENTIAL_ID = 'dockerhub-id'
        GITHUB_CREDENTIAL_ID = 'github-id'
        KUBECONFIG_CREDENTIAL_ID = 'demo-kubeconfig'
        REGISTRY = 'registry.cn-qingdao.aliyuncs.com'
        DOCKERHUB_NAMESPACE = 'ttj_mall'
        GITHUB_ACCOUNT = 'kubesphere'
        APP_NAME = 'devops-java-sample'
        ALIYUNHUB_NAMESPACE = 'lfy_hello'
        def server = ''
    }
}



def getServer(){
    def remote = [:]
    remote.name = '172.31.0.4'
    remote.host = '172.31.0.4'
    remote.user = 'root'
    remote.port = 22
    remote.password = 'root'
    remote.allowAnyHosts = true
    return remote
}
posted @ 2022-02-15 16:17  紫川先生  阅读(171)  评论(0编辑  收藏  举报