You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:定位Jenkins Pipeline的WorkflowScript类源码及更多可用方法

Finding Jenkins WorkflowScript Source & Beyond the .with Method

Hey there! I’ve been down this exact rabbit hole before when building Jenkins Pipelines and shared libraries—let me walk you through how to find the WorkflowScript class source and uncover its useful methods beyond .with.

Locating the WorkflowScript Source Code

  • The WorkflowScript class lives in the workflow-cps-plugin, the core plugin powering Jenkins Pipeline. You can find its source directly in the Jenkins codebase at plugins/workflow-cps/src/main/groovy/org/jenkinsci/plugins/workflow/cps/WorkflowScript.groovy.
  • For a quick on-instance check, fire up Jenkins’ Script Console (under Manage Jenkins > Script Console) and run this command to inspect the class:
    // List all public methods (including inherited ones)
    println WorkflowScript.class.getMethods().collect { it.name }.unique().sort()
    

Useful Methods Beyond .with

Here are some of the most practical methods built into WorkflowScript (or exposed through its context) that you’ll use constantly in pipelines and shared libraries:

  • echo: Outputs log messages to the Jenkins build console
  • sh: Executes shell commands (for Linux/macOS agents)
  • bat: Runs Windows batch commands
  • node: Allocates a Jenkins agent node to execute subsequent steps
  • stage: Defines a visible stage in the pipeline UI (typically used as a block, backed by this method)
  • parallel: Runs multiple pipeline branches concurrently
  • timeout: Wraps steps to enforce a maximum execution time
  • retry: Automatically retries a block of code a specified number of times
  • sleep: Pauses the pipeline for a set duration (e.g., sleep(time: 5, unit: 'MINUTES'))
  • properties: Sets pipeline-level properties like build parameters, triggers, or display name
  • load: Imports and runs external Groovy scripts into the current pipeline context
  • library: Loads a shared library (though the @Library annotation is more common for declarative pipelines)

Bonus: Inherited Methods

Don’t forget that WorkflowScript extends Groovy’s groovy.lang.Script class, so you also get all standard Groovy Script methods, like:

  • getBinding(): Accesses the pipeline’s variable binding context
  • setProperty()/getProperty(): Manipulate pipeline variables dynamically
  • run(): Executes the script’s main logic (you’ll rarely call this directly)

Pro Tip for Shared Library Development

When writing shared libraries, you don’t need to explicitly reference this to call these methods—they’re automatically available in the script context. If you want to see only methods directly defined in WorkflowScript (not inherited), run this in the Script Console:

println WorkflowScript.class.declaredMethods.collect { it.name }.sort()

内容的提问来源于stack exchange,提问作者 jeffsmith712

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:06:39