求助:定位Jenkins Pipeline的WorkflowScript类源码及更多可用方法
.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
WorkflowScriptclass lives in the workflow-cps-plugin, the core plugin powering Jenkins Pipeline. You can find its source directly in the Jenkins codebase atplugins/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 consolesh: Executes shell commands (for Linux/macOS agents)bat: Runs Windows batch commandsnode: Allocates a Jenkins agent node to execute subsequent stepsstage: Defines a visible stage in the pipeline UI (typically used as a block, backed by this method)parallel: Runs multiple pipeline branches concurrentlytimeout: Wraps steps to enforce a maximum execution timeretry: Automatically retries a block of code a specified number of timessleep: 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 nameload: Imports and runs external Groovy scripts into the current pipeline contextlibrary: Loads a shared library (though the@Libraryannotation 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 contextsetProperty()/getProperty(): Manipulate pipeline variables dynamicallyrun(): 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

