3.5.1、创建共享库文件
Jenkinslib/src/org/devops/build.groovy
3.5.2、创建共享库
package org.devops
//构建类型
//传入参数构建类型,构建命令
def Build(buildType,buildShell){
//页面传入的buildType匹配mvn、ant、gradle、npm
//m2、ant、Gradle、NPM为Global Tool Configuration配置变量名称
def buildTools=["mvn":"m2","ant":"ant","gradle":"Gradle","npm":"NPM"]
println("当前选择的构建类型为 ${buildType}")
buildHome = tool buildTools[buildType]
sh "${buildHome}/bin/${buildType} ${buildShell}"
}
3.5.3、创建jenkinsfile
Jenkinslib/buildjenkins
#!groovy
//引用Library库
@Library('jenkinslib@master') _
def build = new org.devops.build()
String buildType = "${env.buildType}" //引用参数
String buildShell = "${env.buildShell}" //引用参数
pipeline {
agent {
node {
label "master" //构建主机
}
}
stages {
stage("Build") {
steps {
script {
build.Build(buildType,buildShell)
}
}
}
}
}