jenkins pipeline使用公共库shared libraries

524次阅读
没有评论

不会飞的渡渡鸟原创,转载请说明出处

当 jenkins 部署的项目有很多相同的步骤的时候,就可以使用 jenkins 公共库引入的方式构建项目。

官方文档示例: https://jenkins.io/doc/book/pipeline/shared-libraries/

开启 pipeline 公共库

系统管理 Global Pipeline Libraries

在这里面选择使用 Git 管理你的公共库资源

jenkins pipeline 使用公共库 shared libraries

Name 是名称,这里写: pipeline-library

Default version 就是 Git 分支

这里我使用了 git 进行代码仓库管理, 目录结构如下

jenkins pipeline 使用公共库 shared libraries

src groovy 源文件

 

在 pipeline 中使用公共库,首先需要在第一行定义

@Library('pipeline-library')_

示例 – 引用公共变量

src/com/ddn/ddn.groovy

#!/usr/bin/env groovy

package com.GlobalVars

class GlobalVars {
   static String git_repo = "*/test"  // 分支
   static String maven_args = "-DskipTests clean package -U  -P test"
}

这里我写了两个变量

  • git_repo git 分支
  • maven_args maven 打包命令

Jenkins Pipeline 示例

@Library('pipeline-library')_import com.ddn.GlobalVars

node("master") {stage('Git Clone') {checkout([$class: 'GitSCM', branches: [[name: GlobalVars.git_repo]], doGenerateSubmoduleConfigurations: false,userRemoteConfigs: [[credentialsId: 'git 拉取代码 id', url: 'gitxxxxxxx']]])
    }
    stage('Maven  build'){sh "mvn ${GlobalVars.maven_args} -f pom.xml"
    }
}
  • git 分支使用 GlobalVars.git_repo 变量
  • maven 打包使用: GlobalVars.maven_args 变量

项目较多需要统一更改一些公共变量时可以采取这种方法。


如果你多个项目构建步骤相同,可以用 DSL 把相同的构建步骤写入公共变量 vars 里面

示例如下:

这是一个 k8s 项目部署的示例

vars/ddn_deploy.groovy

def call(Map config) {node ('jnlp-slave'){
        // 阿里云私服配置
        def registry_url = "registry-vpc.cn-beijing.aliyuncs.com"
        def registry_auth = "docker-registry-aliyun"
        def image_name = "${registry_url}/project/:${config.app_name}_${env.BUILD_ID}" 
        //
        stage('Git Clone') {checkout([$class: 'GitSCM', branches: [[name: '*/test']], doGenerateSubmoduleConfigurations: false,userRemoteConfigs: [[credentialsId: 'git 账号 ID', url: config.git_url]]])
        }
        stage('Maven  build'){sh "mvn  -DskipTests clean package -U  -P test -f ${config.pom_path}"
        }
        stage("Docker build"){
            sh """
            /bin/cp /data/k8s-file/Dockerfile_template Dockerfile
            sed  -i s#JAR_FILE=.*#JAR_FILE=${config.jar_file}# Dockerfile
            """docker.withRegistry("https://" + registry_url,registry_auth) {def customImage = docker.build(image_name)
                customImage.push()}
        }
        stage("k8s deploy") {kubernetesDeploy configs: 'Deployment.yml', kubeconfigId: "K8s 凭据 id",textCredentials: [serverUrl: 'https://']
        }
    }
}

jenkins pipeline 示例
这是一个通过 jenkins 部署 java 项目的示例,定义 app_name,git 地址,pom 地址, 打包好的 jar 文件地址,

@Library('pipeline-library')_def map = [
        app_name: "ddn-test",
        git_url : "https://git.ddn.com/xxxxx.git",
        pom_path: "ddn/pom.xml",
        jar_file: "ddn/target/ddn.jar"
]

podTemplate(label: 'jnlp-slave', cloud: 'kubernetes'){ddn_deply map}

每一个项目只需要定义四个变量即可,构建步骤都通过 vars/ddn_deploy.groovy 文件控制,这种做法的好处在于便于维护,如果想要更改构建步骤只需更改 ddn_deploy.groovy 文件,而无需对每一个项目都进行更改。

正文完
 
ddn
版权声明:本站原创文章,由 ddn 2019-12-11发表,共计2316字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。