我们在管道开发中使用Jenkins Shared Libraries。我们已经在其中编写了很多库,现在我们希望对其进行更好的组织。 jenkins共享库的vars文件夹中是否可能具有文件夹结构?
最佳答案
1)共享库项目的文件夹结构:
如上图所示,您可以在src
文件夹中组织文件夹结构,就像组织java包一样。
然后将它们导入到我们声明全局变量的vars
文件夹中。
2)vars/acidReleaseProcess.groovy
的内容:
#!groovy
import com.fmr.pipeline.AcidReleaseProcess
def relProcess
/**
* Create a instance of class: AcidReleaseProcess
*
* @param appName uDeploy Application Name
* @param compName uDeploy Component Name
* @param compVersion component version to deploy
* @return A self reference
*/
def call(appName, compName, compVersion) {
relProcess = new AcidReleaseProcess(this, appName, compName, compVersion)
return this
}
/**
* Execute Install, Go Live and Stop process on specify env
*
* @param envName name of environment to delpoy, exmaple: 'DIT', 'PAC', 'XQ1'
*/
def blueGreenDeploy(envName) {
relProcess.deployToPassive(envName)
relProcess.swapPassiveToActive(envName)
relProcess.stopPassive(envName)
}
3)
com.fmr.pipeline.AcidReleaseProcess.groovy
class AcidReleaseProcess implements Serializable {
def script, appName, uDeployVers, passiveGroup = [:]
AcidReleaseProcess(script, appName, compName, compVersion) {
this.script = script
this.appName = appName
this.uDeployVers = compName + ":" + compVersion
}
def deployToPassive(envName) {
script.echo "Checking uDeploy environment ${envName} group state"
script.sh 'docker pull ...'
script.withCredentials([script.string(credentialsId: '<...>', variable: 'UDEPLOY_AUTH_TOKEN')]) {
def group1_state = script.sh(
returnStdout: true,
script: "docker run --rm -i ... \
-a ${appName} \
-e ${envName}-${group1} " + '-t ${UDEPLOY_AUTH_TOKEN}'
).trim()
def group2_state = script.sh(
returnStdout: true,
script: "docker run --rm -i ...y \
-a ${appName} \
-e ${envName}-${group2} " + '-t ${UDEPLOY_AUTH_TOKEN}'
).trim()
script.echo "${envName}-${group1} is ${group1_state}, ${envName}-${group2} is ${group2_state}"
if("${group1_state}" == "Live" && "${group2_state}" == "Live") {
script.echo "Environment: ${envName}, ${group1} and ${group2} both are Live," +
"only allow install on Non-Live environment"
script.sh 'exit 1'
}
def groupName = "${group1_state}" != "Live" ? group1 : group2
passiveGroup[envName] = groupName
}
def uDeployEnv = envName + '-' + passiveGroup[envName]
script.echo "Install on ${uDeployEnv}"
script.step([
$class : 'UCDeployPublisher',
siteName: 'pi_udeploy_srvpiudupload',
deploy : [
$class: 'com.urbancode.jenkins.plugins.ucdeploy.DeployHelper$DeployBlock',
deployApp: appName,
deployEnv: uDeployEnv,
deployProc: UDEPLOY_INSTALL_PROC_NAME,
deployVersions: uDeployVers,
deployOnlyChanged: false
]
])
}
}
4)我们应该使
vars
文件夹中的文件内容尽可能简单,并将可重用或复杂的代码组织到src
文件夹下的类文件中。5)一些提示:
relProcess = new AcidReleaseProcess(this, appName, compName, compVersion) // this is the pipeline object
AcidReleaseProcess(script, appName, compName, compVersion) { this.script = script // script here is the pipeline object
script
sh -> script.shecho -> script.echowith... -> script.with...string -> script.stringetc...