我们在管道开发中使用Jenkins Shared Libraries。我们在其中编写了很多库,现在我们希望对其进行更好的组织。 jenkins共享库的vars文件夹中是否可能具有文件夹结构?
答案 0 :(得分:3)
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)一些提示:
将流水线对象传递到src文件夹中的类
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.sh
echo -> script.echo
with... -> script.with...
string -> script.string
etc...
答案 1 :(得分:0)
如@yong所述,您可以在src/
文件夹下设置包结构。从我的角度来看,主要缺点在于,在这里实现步骤更加复杂,因为您必须编写更多代码才能获得与vars/
下的自定义步骤相同的结果。如网站上所写,最好用于辅助功能。
我们的工作:将vars/
下的自定义步骤分组在一个脚本文件中,就像本示例中针对所有日志记录操作一样:
vars / log.groovy(例如,您可以在网站上找到示例)
def info(message) {
echo "INFO: ${message}"
}
def warning(message) {
echo "WARNING: ${message}"
}
您可以在其中随意使用env vars和steps。要在jenkinsfile中使用这些方法,请将它们放在步骤和脚本块中,然后用filename.method
进行调用。
如果我不知道vars/
下的目录是否可能。你试过了吗?