T-Lo
T-Lo

Reputation: 273

$class syntax in jenkins scripted dsl

Often when looking at scripted jenkins pipeline code, I see this pattern...

step([$class: 'GitHubSetCommitStatusBuilder',
        statusMessage: [content: 'Pipeline Started']])

I have not had any luck finding documentation on this technique and would love it if someone could explain what this is doing and when/why it is useful. I believe this is a way to instantiate and populate the members of an underlying groovy class - but more detail would be appreciated.

Also is this documented anywhere?

Upvotes: 19

Views: 11020

Answers (3)

EricD
EricD

Reputation: 316

There's also another quick reference from "Pipeline: Basic Steps" github repo documentation :

https://github.com/jenkinsci/workflow-basic-steps-plugin/blob/master/CORE-STEPS.md

Syntax

As an example, you can write a Pipeline script:

node {
    sh 'make something'
    step([$class: 'ArtifactArchiver', artifacts: 'something'])
}

Here we are running the standard Archive the artifacts post-build action (hudson.tasks.ArtifactArchiver), and configuring the Files to archive property (artifacts) to archive our file something produced in an earlier step. The easiest way to see what class and field names to use is to use the Snippet Generator feature in the Pipeline configuration page.

See the compatibility list for the list of currently supported steps.

Simplified syntax

Build steps and post-build actions in Jenkins core as of 2.2, and in some plugins according to their individual changelogs, have defined symbols which allow for a more concise syntax. Snippet Generator will offer this when available. For example, the above script may also be written:

node {
    sh 'make something'
    archiveArtifacts 'something'
}

NB: The Simplified syntax, makes reference to Plugin Steps parameters

Upvotes: 3

amahfouz
amahfouz

Reputation: 2398

Here is a reference that briefly explains the syntax. Basically, you are providing the step() function a map of arguments in the form of name-value pairs. The first argument which is especially denoted by the name $class tells the function which class (plugin) to instantiate.

It also seems that this syntax is being deprecated in favor of shorter syntax, also explained in the same link.

Upvotes: 13

Addo Zhang
Addo Zhang

Reputation: 386

I am also struggling with this syntax, but unfortunately have not found any doc yet.

I guess this syntax is used to doing the instance initialization.

All step classes implement interface BuildStep. After script loaded, all step instances initiated, then their perform method are invoked during build procedure.

All above is my conjecture.

Upvotes: 0

Related Questions