MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

Jenkins Tutorials: Meaning of $class in scripted pipeline

In a Jenkins Pipeline, $class is a special map key that tells Jenkins which plugin or core class to create when a step or property accepts more than one possible implementation, as in checkout([$class: 'GitSCM', ...]). This page explains how $class works, when you still need it, how it maps to the shorter symbol syntax, and how to fix the errors it most often causes.

In Jenkins pipeline scripts, the $class parameter is used to specify the class or type of an object. The complete list of $class values depends on the specific plugins and extensions installed in your Jenkins instance. Each plugin or extension may introduce its own set of classes that can be used in the pipeline script.

However, here are some common $class values that are frequently used in Jenkins pipeline scripts:

  • 'ChoiceParameter': Represents a parameter with a choice or selection.
  • 'CascadeChoiceParameter': Represents a parameter with cascading choices or selections.
  • 'GroovyScript': Represents a script block containing Groovy code.
  • 'StringParameterDefinition': Represents a parameter for accepting a string value.
  • 'BooleanParameterDefinition': Represents a parameter for accepting a boolean value.
  • 'RunParameterDefinition': Represents a parameter for selecting a specific Jenkins run or build.
  • 'FileParameterDefinition': Represents a parameter for accepting a file as input.
  • 'TextParameterDefinition': Represents a parameter for accepting multiline text as input.
  • 'PasswordParameterDefinition': Represents a parameter for accepting a password or sensitive information.

These are just a few examples, and the available $class values can vary depending on the plugins and extensions installed in your Jenkins environment. It’s recommended to refer to the documentation of the specific plugins you are using to determine the supported $class values and their configurations.


What $class actually does

Many Pipeline steps take an argument whose type is an *extension point*: an abstract type that several plugins can implement. checkout accepts any SCM (Git, Subversion and others), properties accepts any job property, and parameters accepts any parameter definition. When you pass a Groovy map for one of these arguments, Jenkins needs to know which concrete class to build, and the $class key tells it. Every other key in the map is passed to that class's @DataBoundConstructor arguments or @DataBoundSetter properties.

// $class picks the implementation; the other keys configure it
checkout([
  $class: 'GitSCM',
  branches: [[name: '*/main']],
  userRemoteConfigs: [[url: 'https://github.com/example/app.git']],
  extensions: [[$class: 'CleanBeforeCheckout']]
])Code language: PHP (php)

The value is usually the simple class name, such as GitSCM. If two installed plugins define classes with the same simple name, use the fully qualified name instead, for example $class: 'hudson.plugins.git.GitSCM'. The Structs plugin, which every Pipeline installation depends on, does this conversion.

Note: $class is not limited to scripted Pipeline. It works the same way in Declarative Pipeline, for example inside steps { checkout([$class: 'GitSCM', ...]) } or in a script block.

$class versus symbols

Plugin authors can add a @Symbol annotation to their classes. When a class has a symbol, you can call it like a function instead of writing a $class map. Symbols are shorter and easier to read, and the Snippet Generator produces them today. The $class form is still useful when a plugin has no symbol. This is common for older plugins and for nested objects such as Git extensions.

// $class form
properties([
  [$class: 'BuildDiscarderProperty',
    strategy: [$class: 'LogRotator', numToKeepStr: '10']],
  [$class: 'ParametersDefinitionProperty', parameterDefinitions: [
    [$class: 'StringParameterDefinition', name: 'VERSION',
      defaultValue: '1.0.0', description: 'Release version'],
    [$class: 'BooleanParameterDefinition', name: 'RUN_TESTS',
      defaultValue: true, description: 'Run the test stage']
  ]]
])

// Equivalent symbol form (preferred when available)
properties([
  buildDiscarder(logRotator(numToKeepStr: '10')),
  parameters([
    string(name: 'VERSION', defaultValue: '1.0.0', description: 'Release version'),
    booleanParam(name: 'RUN_TESTS', defaultValue: true, description: 'Run the test stage')
  ])
])Code language: PHP (php)

Both blocks produce the same job configuration. The table below lists each class from the post above with its symbol and the plugin that provides it.

$class valueSymbolProvided byNotes
StringParameterDefinitionstringJenkins coreSingle-line text
TextParameterDefinitiontextJenkins coreMulti-line text
BooleanParameterDefinitionbooleanParamJenkins coreCheckbox; value is a real boolean in params
ChoiceParameterDefinitionchoiceJenkins coreStatic drop-down list
PasswordParameterDefinitionpasswordJenkins coreMasked input; prefer credentials for real secrets
RunParameterDefinitionrunJenkins coreSelects a build of another job
FileParameterDefinitionnone usableJenkins coreNot supported in Pipeline jobs; see corrections
ChoiceParametercheck Snippet GeneratorActive Choices pluginDrop-down filled by a Groovy script
CascadeChoiceParametercheck Snippet GeneratorActive Choices pluginChoices that react to other parameters
GroovyScriptnoneActive Choices pluginScript model used inside Active Choices parameters

Corrections to the list above

  • $class is not a parameter. It is a reserved map key that selects the class Jenkins should create from that map.
  • ChoiceParameter, CascadeChoiceParameter and GroovyScript are not built in. They come from the Active Choices plugin, and they fail with an unknown-implementation error if that plugin is not installed. The core static choice parameter is ChoiceParameterDefinition (symbol choice).
  • GroovyScript is not a general script block. It is the Active Choices object that holds the script, plus a fallback script, used to generate parameter values.
  • FileParameterDefinition does not work in Pipeline jobs because the uploaded file never reaches the workspace. Use the File Parameters plugin instead, which provides base64File and stashedFile.
  • Most of the core *ParameterDefinition classes have symbols (string, text, booleanParam and so on). Symbols are the recommended syntax in current Pipeline code.

Example: Active Choices parameters with $class

Active Choices is where you still see $class most often, because most published examples use the map form. The scripted Pipeline below defines an environment drop-down and a region drop-down whose options change based on the selected environment. It requires the Active Choices plugin.

properties([
  parameters([
    [$class: 'ChoiceParameter',
      name: 'ENVIRONMENT',
      description: 'Target environment',
      choiceType: 'PT_SINGLE_SELECT',
      filterable: false,
      script: [$class: 'GroovyScript',
        script: [classpath: [], sandbox: true,
                 script: 'return ["dev", "staging", "prod"]'],
        fallbackScript: [classpath: [], sandbox: true,
                 script: 'return ["error"]']
      ]
    ],
    [$class: 'CascadeChoiceParameter',
      name: 'REGION',
      description: 'Region for the selected environment',
      choiceType: 'PT_SINGLE_SELECT',
      referencedParameters: 'ENVIRONMENT',
      filterable: false,
      script: [$class: 'GroovyScript',
        script: [classpath: [], sandbox: true, script: '''
          if (ENVIRONMENT == "prod") { return ["us-east-1", "eu-west-1"] }
          return ["us-east-1"]
        '''],
        fallbackScript: [classpath: [], sandbox: true,
                 script: 'return ["error"]']
      ]
    ]
  ])
])

node {
  stage('Deploy') {
    echo "Deploying to ${params.ENVIRONMENT} in ${params.REGION}"
  }
}Code language: PHP (php)
  • choiceType controls the widget: PT_SINGLE_SELECT, PT_MULTI_SELECT, PT_RADIO or PT_CHECKBOX.
  • referencedParameters lists the parameters whose values are passed into the cascade script as variables. Separate multiple names with commas.
  • fallbackScript runs if the main script throws an error, so the form still renders.
  • The inner script maps are Script Security sandbox scripts. If a script calls methods the sandbox does not allow, an administrator must approve them under Manage Jenkins > In-process Script Approval.

Note: Parameters defined with properties only appear after the Pipeline has run once. Run the job once (it uses default or empty values). After that, Build with Parameters shows the new form.

Other places you will see $class

Outside parameters, $class appears mostly in older Pipelines and in steps that wrap freestyle build features. The comment on each line shows the modern equivalent.

step([$class: 'ArtifactArchiver', artifacts: 'target/*.jar'])   // archiveArtifacts 'target/*.jar'
step([$class: 'JUnitResultArchiver', testResults: '**/TEST-*.xml']) // junit '**/TEST-*.xml'
wrap([$class: 'TimestamperBuildWrapper']) { sh 'make' }         // timestamps { sh 'make' }
checkout([$class: 'GitSCM', branches: [[name: '*/main']],
          userRemoteConfigs: [[url: 'https://github.com/example/app.git']]])
                                                               // git url: '...', branch: 'main'Code language: PHP (php)

How to find the right $class value and keys

  1. Open any Pipeline job and click Pipeline Syntax, or go to JENKINS_URL/job/<job-name>/pipeline-syntax.
  2. In the Snippet Generator, choose a step such as properties, checkout or step, fill in the form, and click Generate Pipeline Script. The output uses a symbol where one exists and falls back to $class where it does not.
  3. For Declarative options, parameters and triggers, use the Declarative Directive Generator on the same page.
  4. Look up the class's @DataBoundConstructor and @DataBoundSetter fields in the plugin's documentation or source. The map accepts only these keys.

Note: Write the key as $class: (unquoted) or '$class': (single quotes). Do not use double quotes: "$class" is a Groovy GString, so Groovy tries to substitute a variable named class.

Common errors and fixes

SymptomCauseFix
Error saying there is no known implementation of a type named XThe plugin that provides X is not installed, or the name is misspelledInstall the plugin (for example Active Choices) or correct the class name
Error about an ambiguous class nameTwo plugins define a class with the same simple nameUse the fully qualified class name in $class
Warning about unknown parameters for a class, and the setting is ignoredA map key does not match any constructor argument or setterRegenerate the snippet with the Snippet Generator and copy the exact key names
Parameters do not appear in the UIThe properties step has not run yetRun the job once, then use Build with Parameters
Settings made in the job UI disappearproperties replaces the job's properties on every runDefine all job properties in the Jenkinsfile, not partly in the UI

Best practices

  • Use symbols when they exist. Keep $class for classes that have no symbol.
  • Keep job properties and parameters in the Jenkinsfile so they are versioned and reviewed along with the pipeline.
  • Do not collect real secrets through password parameters. Store them as Jenkins credentials and read them with withCredentials.
  • Test plugin upgrades before rolling them out. Class names and fields belong to plugins, so a major plugin release can rename or remove them.
  • For more Pipeline syntax, see the Jenkins Pipeline Complete Reference Tutorials.

Frequently asked questions

What does $class mean in a Jenkinsfile?

It is a special map key that names the concrete class Jenkins should create when a step or property accepts several possible implementations. The other keys in the map configure that class.

Should I use $class or the symbol syntax?

Use the symbol syntax, such as string(...) or buildDiscarder(logRotator(...)), whenever the plugin provides one, because it is shorter and easier to read. Use $class only for classes without a symbol.

Does $class work in Declarative Pipeline?

Yes. Scripted and Declarative Pipelines use the same conversion, so $class maps work inside steps, inside script blocks and as step arguments in either style.

Why do I get a 'no known implementation' error with $class?

Either the class name is wrong or the plugin that provides it is not installed. Check the spelling, install the plugin, or use the fully qualified class name if the simple name is ambiguous.

Where do ChoiceParameter and CascadeChoiceParameter come from?

They come from the Active Choices plugin, not Jenkins core. The core static drop-down is ChoiceParameterDefinition, available as the choice symbol.

Related tutorials

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Automating Jenkins Job Configuration: Job DSL, Multibranch, Job Generator, and JJB

Manually creating many Jenkins jobs is slow and error-prone. This guide compares the main automation options and shows practical jobs-as-code examples for repeatable setup.

Read More

Jenkins Troubleshooting Guide: Fast Triage, Log Analysis, and Common Failure Fixes

A practical Jenkins troubleshooting guide for DevOps teams: find the right logs first, separate controller vs. agent vs. job failures, and resolve common outage patterns quickly.

Read More

Jenkins Pipeline Tutorials: How to create folder and subfolder?

To create folders and subfolders in Jenkins using pipeline code, you can use Jenkins Job DSL or Groovy script. Hereโ€™s how you can accomplish this in a…

Read More

Jenkins Tutorials: ChoiceParameter & CascadeChoiceParameter in Pipeline using ScriptlerScript

Rajesh Kumar I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have…

Read More

Jenkins Tutorials: Variables in Scripted Pipeline

In a scripted Jenkins pipeline, you can use different types of variables depending on your requirements. Here are examples of different variable types commonly used in scripted…

Read More

Top 50 Jenkin pipeline interview questions and answers

1) How to fix a broken build for your project in Jenkins and how to make sure project build doesn’t break in Jenkins at all? The user…

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x