首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从另一个Jenkinsfile中调用Jenkinsfile或Jenkins职务?

如何从另一个Jenkinsfile中调用Jenkinsfile或Jenkins职务?
EN

Stack Overflow用户
提问于 2021-04-20 08:22:16
回答 2查看 1.1K关注 0票数 0

在用Groovy编写的脚本管道上,我有两个Jenkinsfile,即- Jenkinsfile1Jenkinsfile2

是否可以从Jenkinsfile2调用Jenkinsfile1

下面是我的Jenkinsfile1

代码语言:javascript
复制
#!groovy

stage('My build') {
    node('my_build_node') {
        def some_output = True
        if (some_output) {
            // How to call Jenkinsfile2 here?
        }
    }
}

如果输出的值不是空的,那么如何调用上面的Jenkinsfile2

或者可以调用另一个使用Jenkinsfile2的Jenkins作业

EN

回答 2

Stack Overflow用户

发布于 2021-04-20 17:44:31

我对你的问题不太清楚。如果您只想将一些Groovy代码加载并评估到您的代码中,可以使用load() (如前面所述的@JoseAO )。除了他的例子外,如果您的文件(Jenkinsfile2.groovy)有一个call()方法,您可以直接使用它,如下所示:

代码语言:javascript
复制
node('master') {
    pieceOfCode = load 'Jenkinsfile2.groovy'
    pieceOfCode()
    pieceOfCode.bla()
}

现在,如果您想要触发另一个作业,则可以使用build()步骤,即使您没有使用声明性管道。问题是,您正在调用的管道必须在Jenkins中创建,因为build()使用作业名而不是管道文件名作为参数。下面是一个如何调用名为pipeline2的作业的示例

代码语言:javascript
复制
node('master') {
    build 'pipeline2'
}

现在,对于您的问题“如果输出的值不是空的,那么如何调用Jenkinsfile2?”,如果我理解正确,您将尝试运行一些shell命令,如果它是空的,您将加载Jenkinsfile/管道。以下是如何实现这一目标:

代码语言:javascript
复制
// 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.')
    }
}

诚挚的问候。

票数 4
EN

Stack Overflow用户

发布于 2021-04-20 14:27:06

例如,您的Jenkisfile2 --它是我的“管道2.groovy”。

代码语言:javascript
复制
    def pipeline2 = load (env.PATH_PIPELINE2 + '/pipeline2.groovy')
    pipeline2.method()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67175108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档