Jenkins++:Pipeline脚本 V1.0(Jenkins + +: pipeline script v1 0)

// GitLab的凭证
def git_auth="e3c9f3d5-6a52-4c10-86a3-80911a71bb24"
// 项目GitLab仓库
def git_url="http://192.168.1.101:82/test-team/tensquare_parent.git"
//镜像的版本号
def tag = "latest"
//Harbor的url地址
def harbor_url = "192.168.1.101:7701"
//镜像库项目名称
def harbor_project = "tensquare"
//Harbor的登录凭证ID
def harbor_auth = "737a6d3f-8be3-4ec0-a36a-b1afd9a8f380"

node{
    
    //获取当前选择的项目名称
   def selectedProjectNames = "${project_name}".split(",")
   
    stage('拉取代码'){
        checkout([
            $class: 'GitSCM',
            branches: [
                [
                    name: '*/${branch}'
                ]
            ],
            doGenerateSubmoduleConfigurations: false,
            extensions: [
                
            ],
            submoduleCfg: [
                
            ],
            userRemoteConfigs: [
                [
                    credentialsId: "${git_auth}",
                    url: "${git_url}"
                ]
            ]
        ])
    }
    stage('编译,安装公共子工程') {
      sh "mvn -f tensquare_common  clean install"
    }
    stage('编译,打包微服务工程,上传镜像') {
        // 遍历所有项目   
        for(int i=0;i<selectedProjectNames.length;i++){
             def projectInfo = selectedProjectNames[i];
             echo "项目 | ${projectInfo} | 开始制作运行"
             //当前遍历的项目名称
             def currentProjectName = "${projectInfo}"
             //制作镜像
             sh "mvn -f ${currentProjectName} clean package dockerfile:build"
             //定义镜像名称
             def imageName = "${currentProjectName}:${tag}"
             //对镜像打上标签
             sh "docker tag ${imageName} ${harbor_url}/${harbor_project}/${imageName}"
             //把镜像推送到Harbor
            withCredentials([usernamePassword(credentialsId: "${harbor_auth}", passwordVariable: 'password', usernameVariable: 'username')]) 
            {
                //登录到Harbor
                sh "docker login -u ${username} -p ${password} ${harbor_url}"
                //镜像上传
                sh "docker push ${harbor_url}/${harbor_project}/${imageName}"
                sh "echo 镜像上传成功"
            }
            echo "项目 | ${projectInfo} | 制作完成"
            //删除本地镜像
            sh "docker rmi -f `docker images -q --filter reference=${imageName}`"
            //sh "docker rmi -f `docker images -q --filter reference=${harbor_url}/${harbor_project_name}/${imageName}`"
            echo "项目 | ${projectInfo} | 临时镜像已删除"
        }
    }
}
————————
// GitLab的凭证
def git_auth="e3c9f3d5-6a52-4c10-86a3-80911a71bb24"
// 项目GitLab仓库
def git_url="http://192.168.1.101:82/test-team/tensquare_parent.git"
//镜像的版本号
def tag = "latest"
//Harbor的url地址
def harbor_url = "192.168.1.101:7701"
//镜像库项目名称
def harbor_project = "tensquare"
//Harbor的登录凭证ID
def harbor_auth = "737a6d3f-8be3-4ec0-a36a-b1afd9a8f380"

node{
    
    //获取当前选择的项目名称
   def selectedProjectNames = "${project_name}".split(",")
   
    stage('拉取代码'){
        checkout([
            $class: 'GitSCM',
            branches: [
                [
                    name: '*/${branch}'
                ]
            ],
            doGenerateSubmoduleConfigurations: false,
            extensions: [
                
            ],
            submoduleCfg: [
                
            ],
            userRemoteConfigs: [
                [
                    credentialsId: "${git_auth}",
                    url: "${git_url}"
                ]
            ]
        ])
    }
    stage('编译,安装公共子工程') {
      sh "mvn -f tensquare_common  clean install"
    }
    stage('编译,打包微服务工程,上传镜像') {
        // 遍历所有项目   
        for(int i=0;i<selectedProjectNames.length;i++){
             def projectInfo = selectedProjectNames[i];
             echo "项目 | ${projectInfo} | 开始制作运行"
             //当前遍历的项目名称
             def currentProjectName = "${projectInfo}"
             //制作镜像
             sh "mvn -f ${currentProjectName} clean package dockerfile:build"
             //定义镜像名称
             def imageName = "${currentProjectName}:${tag}"
             //对镜像打上标签
             sh "docker tag ${imageName} ${harbor_url}/${harbor_project}/${imageName}"
             //把镜像推送到Harbor
            withCredentials([usernamePassword(credentialsId: "${harbor_auth}", passwordVariable: 'password', usernameVariable: 'username')]) 
            {
                //登录到Harbor
                sh "docker login -u ${username} -p ${password} ${harbor_url}"
                //镜像上传
                sh "docker push ${harbor_url}/${harbor_project}/${imageName}"
                sh "echo 镜像上传成功"
            }
            echo "项目 | ${projectInfo} | 制作完成"
            //删除本地镜像
            sh "docker rmi -f `docker images -q --filter reference=${imageName}`"
            //sh "docker rmi -f `docker images -q --filter reference=${harbor_url}/${harbor_project_name}/${imageName}`"
            echo "项目 | ${projectInfo} | 临时镜像已删除"
        }
    }
}