Jenkins管道:构建步骤的返回值 [英] Jenkins pipeline: return value of build step

查看:19
本文介绍了Jenkins管道:构建步骤的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Jenkins 的这个集成管道中,我使用 并行触发不同的构建构建步骤,如下:

In this integration pipeline in Jenkins, I am triggering different builds in parallel using the build step, as follows:

stage('trigger all builds')
{
  parallel
  {
    stage('componentA')
    {
      steps
      {
        script 
        {
          def myjob=build job: 'componentA', propagate: true, wait: true
        }
      }
    }
    stage('componentB')
    {
      steps 
      {
        script
        {
          def myjob=build job: 'componentB', propagate: true, wait: true
        }
      }
    }
  }
}

我想访问 build 步骤的返回值,以便在我的 Groovy 脚本中知道触发了哪个作业名称和编号.

I would like to access the return value of the build step, so that I can know in my Groovy scripts what job name, number was triggered.

我在示例中发现返回的对象有像 getProjectName()getNumber() 这样的 getter,我可以使用这些方法.

I have found in the examples that the object returned has getters like getProjectName() or getNumber() that I can use for this.

但是我怎么知道返回对象的确切类以及我可以调用它的方法列表呢?管道文档 中似乎缺少这一点.我特别要求这种情况,但一般来说,我如何知道返回对象的类及其文档?

But how do I know the exact class of the returned object and the list of methods I can call on it? This seems to be missing from the Pipeline documentation. I am asking for this case in particular, but generally speaking, how can I know the class of the returned object and its documentation?

推荐答案

步骤文档是根据插件捆绑的一些文件生成的,有时是不够的.一种简单的方法是通过调用 getClass:

The step documentation is generated based on some files that are bundled with the plugin, which sometimes isn't enough. One easy way would be to just print out the class of the result object by calling getClass:

def myjob=build job: 'componentB', propagate: true, wait: true
echo "${myjob.getClass()}"

这个输出会告诉你结果(在这种情况下)是一个 org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper 它有 已发布 Javadoc.

This output would tell you that the result (in this case) is a org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper which has published Javadoc.

对于其他情况,我通常必须深入研究 Jenkins 源代码.这是我的总体策略:

For other cases, I usually have to dive into the Jenkins source code. Here is my general strategy:

  • Figure out which plugin the step comes from either by the step documentation, jenkins.io steps reference, or just searching the internet
  • From the plugin site, go to the source code repository
  • Search for the String literal of the step name, and find the step type that returns it. In this case, it looks to be coming from the BuildTriggerStep class, which extends AbstractStepImpl

@Override
public String getFunctionName() {
    return "build";
}

  • 查看嵌套的 DescriptorImpl 查看返回的执行类

  • Look at the nested DescriptorImpl to see what execution class is returned

    public DescriptorImpl() {
        super(BuildTriggerStepExecution.class);
    }
    

  • 转到 BuildTriggerStepExecution 并查看start()方法中的执行体

  • Go to BuildTriggerStepExecution and look at the execution body in the start() method

    阅读 工作流程步骤 README 表明应该调用 context.onSuccess(value) 来返回结果.该文件中有一个位置,但这仅适用于无等待"的情况,它总是立即返回并且是 null (来源).

    Reading over the workflow step README shows that something should call context.onSuccess(value) to return a result. There is one place in that file, but that is only on the "no-wait" case, which always returns immediately and is null (source).

    if (step.getWait()) {
        return false;
    } else {
        getContext().onSuccess(null);
        return true;
    }
    

  • 好的,所以它没有在步骤执行中完成,所以它必须在其他地方.我们还可以在存储库中搜索 onSuccess,看看还有什么可以从这个插件触发它.我们发现 RunListener 实现会为步骤执行异步设置结果,如果它是这样配置的:

  • Ok, so it isn't completing in the step execution, so it must be somwhere else. We can also search the repository for onSuccess and see what else might trigger it from this plugin. We find that a RunListener implementation handles setting the result asynchronously for the step execution if it has been configured that way:

    for (BuildTriggerAction.Trigger trigger : BuildTriggerAction.triggersFor(run)) {
        LOGGER.log(Level.FINE, "completing {0} for {1}", new Object[] {run, trigger.context});
        if (!trigger.propagate || run.getResult() == Result.SUCCESS) {
            if (trigger.interruption == null) {
                trigger.context.onSuccess(new RunWrapper(run, false));
            } else {
                trigger.context.onFailure(trigger.interruption);
            }
        } else {
            trigger.context.onFailure(new AbortException(run.getFullDisplayName() + " completed with status " + run.getResult() + " (propagate: false to ignore)"));
        }
    }
    run.getActions().removeAll(run.getActions(BuildTriggerAction.class));
    

  • trigger.context.onSuccess(new RunWrapper(run, false));RunWrapper 结果的来源

    这篇关于Jenkins管道:构建步骤的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆