在用Groovy编写的脚本管道上,我有两个Jenkinsfile,即- Jenkinsfile1和Jenkinsfile2。
是否可以从Jenkinsfile2调用Jenkinsfile1。
下面是我的Jenkinsfile1
#!groovy
stage('My build') {
node('my_build_node') {
def some_output = True
if (some_output) {
// How to call Jenkinsfile2 here?
}
}
}如果输出的值不是空的,那么如何调用上面的Jenkinsfile2?
或者可以调用另一个使用Jenkinsfile2的Jenkins作业
发布于 2021-04-20 17:44:31
我对你的问题不太清楚。如果您只想将一些Groovy代码加载并评估到您的代码中,可以使用load() (如前面所述的@JoseAO )。除了他的例子外,如果您的文件(Jenkinsfile2.groovy)有一个call()方法,您可以直接使用它,如下所示:
node('master') {
pieceOfCode = load 'Jenkinsfile2.groovy'
pieceOfCode()
pieceOfCode.bla()
}现在,如果您想要触发另一个作业,则可以使用build()步骤,即使您没有使用声明性管道。问题是,您正在调用的管道必须在Jenkins中创建,因为build()使用作业名而不是管道文件名作为参数。下面是一个如何调用名为pipeline2的作业的示例
node('master') {
build 'pipeline2'
}现在,对于您的问题“如果输出的值不是空的,那么如何调用Jenkinsfile2?”,如果我理解正确,您将尝试运行一些shell命令,如果它是空的,您将加载Jenkinsfile/管道。以下是如何实现这一目标:
// Method #1
node('master') {
try {
sh 'my-command-goes-here'
build 'pipeline2' // if you're trying to call another job
// If you're trying to load and evaluate a piece of code
pieceOfCode = load 'Jenkinsfile2.groovy'
pieceOfCode()
pieceOfCode.bla()
}
catch(Exception e) {
print("${e}")
}
}
// Method #2
node('master') {
def commandResult = sh script: 'my-command-goes-here', returnStdout: true
if (commandResult.length() != 0) {
build 'pipeline2' // if you're trying to call another job
// If you're trying to load and evaluate a piece of code
pieceOfCode = load 'Jenkinsfile2.groovy'
pieceOfCode()
pieceOfCode.bla()
}
else {
print('Something went bad with the command.')
}
}诚挚的问候。
发布于 2021-04-20 14:27:06
例如,您的Jenkisfile2 --它是我的“管道2.groovy”。
def pipeline2 = load (env.PATH_PIPELINE2 + '/pipeline2.groovy')
pipeline2.method()https://stackoverflow.com/questions/67175108
复制相似问题