Jenkins 插件 - 如何获取当前正在执行的作业?

Jenkins plugin - how to get currently executing job?

我正在构建一个 jenkins 管道插件(要从管道调用的方法)并且需要获取有关当前 运行 作业的检索信息,该作业调用了我的方法。

我发现有几个问题在谈论它,例如这里 - Jenkins Plugin How to get Job information

但我不知道如何使用这些信息。我确实有权访问 Jenkins 实例,但没有关于当前项目、工作、构建等的任何信息。我​​如何才能获得该信息?

注意,这是一个流水线步骤插件,里面没有perform方法。

好的,经过搜索,我终于在最明显的地方找到了答案——documentation for writing pipeline steps plugins and the corresponding API documentation

方法来自 Execution class。在其中,只需调用 getContext(),其中 returns StepContext,然后调用 .get 方法来获取您需要的其余内容:

public class MyExecution extends SynchronousNonBlockingStepExecution<ReturnType> {
    ...

    @Override
    protected ReturnType run() throws Exception {
        try {
            StepContext context = getContex();

            // get currently used workspace path
            FilePath path = context.get(FilePath.class);

            //get current run
            Run run = context.get(Run.class);

            // ... and so on ...
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    ...
}