我在 vars 下的 jenkins 共享库中有一个 x.groovy 和一个 y.groovy 文件。 我需要使用 x.groovy 中函数的输出作为函数 y.groovy 的输入。
x.groovy 看起来像这样:
void testM(String msg) {
int paddingLen = 100
String bottomLine = '└'.padRight(paddingLen, '─') + '┘\n'
String topLine = '┌'.padRight(paddingLen, '─') + '┐\n'
String pM = topLine +
"|"+ msg.padRight(200) + "|\n" +
bottomLine
printf(pM)
}
def display(message) {
loadColors()
echo "${GREEN}[INFO] - $message ${NC}"
}
在 jenkinsfile 中我需要将其用作
display.testM("print something here")
。
我该怎么做,请指教
不清楚你的结构看起来如何,但是
即
vars/x.groovy
def firstFunction() {...}
def sesondFunction() {...}
vars/y.groovy
def firstFunction() {...}
def sesondFunction() {...}
会打电话 x.firstFunction()
或
y.firstFunction()
默认情况下
y() or x()
它将尝试执行文件中的call()
函数(如果不存在,您将看到错误)
在您的情况下最好创建一个文件
vars/DisplayMessage.groovy
与
String formatMessage(String msg) {
int paddingLen = 100
String bottomLine = '└'.padRight(paddingLen, '─') + '┘\n'
String topLine = '┌'.padRight(paddingLen, '─') + '┐\n'
String pM = topLine +
"|"+ msg.padRight(200) + "|\n" +
bottomLine
return pM
}
def call(String message) {
String formatedMessage = x.formatMessage(message)
loadColors()
echo "${GREEN}[INFO] - ${formatedMessage} ${NC}"
}
并在管道中使用它作为
DisplayMessage("your message")
如果没有,您可以随时使用评论中的
y.display(x.testM("print something here"))