This class represents a basic Jenkins pipeline. Use the methods of this class to add stages to your pipeline.
The following plugins are required:
Multibranch Pipeline
or Pipeline
job for your project. Any other
type of build will fail.@Library('fill this out according to your setup') import org.zowe.jenkins_shared_library.pipelines.base.Pipeline node('pipeline-node') { Pipeline pipeline = new Pipeline(this) // Set your config up before calling setup pipeline.admins.add("userid1", "userid2", "userid3") // update branches settings if we have different settings from #defineDefaultBranches() pipeline.branches.addMap([ [ name : 'lts-incremental', isProtected : true, buildHistory : 20, ] ]) // MUST BE CALLED FIRST pipeline.setup() // Create a stage in your pipeline pipeline.createStage(name: 'Some Pipeline Stage', stage: { echo "This is my stage" }) // MUST BE CALLED LAST pipeline.end() }
In the above example, the stages will run on a node labeled 'pipeline-node'
. You
must define the node where your pipeline will execute.
Stages are not executed until the end method is called. This means that you can't rely on stages being executed as soon as they are defined. If you need logic to help determine if a stage should be executed, you must use the proper options allowed by org.zowe.jenkins_shared_library.pipelines.base.arguments.StageArguments.
setup()
setup()
. Which is the same as the
superclass, so under normal OOP the method would be overridden.setup()
, it calls super.setup()
.super.setup()
actually calls the subclass method.
this results in a stack overflow error, since the method is just calling itself until
the method stack is completely used up.I believe this is due to how Jenkins does inheritance. It looks like Jenkins is just taking all the methods available across the superclasses of a class and adding them as a base definition to the class itself. When there's a conflict, it seems that Jenkins just takes the method from the lowest class in the hierarchy and refers all calls to that method no matter what. This theory stems from the fact that if you call a superclass method that accesses a superclass private variable, Jenkins complains that the variable doesn't exist. If you change that variable or method to protected or public, Jenkins doesn't complain anymore and operation acts as normal.
This issue seems to signify that they may never fix this problem.
Modifiers | Name | Description |
---|---|---|
protected static java.lang.String |
_SETUP_STAGE_NAME |
The name of the setup stage. |
protected PipelineControl |
_control |
This control variable represents internal states of items in the pipeline. |
protected org.zowe.jenkins_shared_library.email.Email |
_email |
An Email instance to handle email related functions |
protected boolean |
_isProtectedBranch |
Tracks if the current branch is protected. |
protected boolean |
_shouldSkipRemainingStages |
Tracks if the remaining stages should be skipped. |
protected Stages |
_stages |
The stages of the pipeline to execute. |
Type | Name and description |
---|---|
PipelineAdmins |
admins This is a list of administrator ids that will receive notifications when a build happens on a protected branch. |
java.lang.String |
baseDirectory Base directory of the project. |
Branches<org.zowe.jenkins_shared_library.pipelines.base.models.Branch> |
branches A map of branches. |
java.util.List |
buildOptions Options that are to be added to the build. |
java.util.List |
buildParameters Build parameters that will be defined to the build |
java.util.List<java.lang.String> |
buildUpstreams Upstream builds |
java.lang.String |
packageName Package name of the project |
java.lang.Object |
steps Reference to the groovy pipeline variable. |
java.lang.String |
version Package current semantic version in {@code <major>. |
Constructor and description |
---|
Pipeline
(java.lang.Object steps) Constructs the class. |
Type Params | Return Type | Name and description |
---|---|---|
|
protected void |
_closureWrapper(Stage stage, groovy.lang.Closure closure) Wraps a closure function in a try catch. |
|
protected static java.lang.String |
_getStageSkipOption(Stage stage) Gets the stage skip parameter name. |
|
protected void |
_sendEmailNotification() Send an email notification about the result of the build to the appropriate users |
|
void |
addBuildOption(java.lang.Object option) Add new build options to the pipeline. |
|
void |
addBuildParameter(java.lang.Object param) Add new build parameter to the pipeline |
|
void |
addBuildParameters(java.util.List params) Add new build parameters to the pipeline |
|
void |
addBuildParameters(org.jenkinsci.plugins.structs.describable.UninstantiatedDescribable... params) Add new build parameters to the pipeline. |
|
void |
addUpstreams(java.lang.String... upstreams) Define upstreams of the pipeline. |
|
Stage |
createStage(StageArguments args) Creates a new stage to be run in the Jenkins pipeline. |
|
Stage |
createStage(java.util.Map arguments) Creates a new stage to be run in the Jenkins pipeline. |
|
protected void |
defineDefaultBranches() Setup default branch settings |
|
protected void |
end(EndArguments args) Pseudo end method, should be overridden by inherited classes |
|
protected void |
end(java.util.Map args = [:]) Pseudo end method, should be overridden by inherited classes |
|
void |
endBase(EndArguments args) Signal that no more stages will be added and begin pipeline execution. |
|
void |
endBase(java.util.Map arguments = [:]) Signal that no more stages will be added and begin pipeline execution. |
|
Stage |
getFirstFailingStage() Gets the first failing stage within _stages |
|
Stage |
getStage(java.lang.String stageName) Get a stage from the available stages by name. |
|
void |
setResult(ResultEnum result) Set the build result |
|
protected void |
setup(SetupStageArguments arguments) Pseudo setup method, should be overridden by inherited classes |
|
protected void |
setup(java.util.Map arguments = [:]) Pseudo setup method, should be overridden by inherited classes |
|
void |
setupBase(SetupStageArguments arguments) Initialize the pipeline. |
|
void |
setupBase(java.util.Map arguments = [:]) Initialize the pipeline. |
Methods inherited from class | Name |
---|---|
class java.lang.Object |
java.lang.Object#wait(long, int), java.lang.Object#wait(long), java.lang.Object#wait(), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll() |
The name of the setup stage.
"Setup"
.This control variable represents internal states of items in the pipeline.
An Email instance to handle email related functions
Tracks if the current branch is protected.
Tracks if the remaining stages should be skipped.
The stages of the pipeline to execute. As stages are created, they are added into this control class.
This is a list of administrator ids that will receive notifications when a build happens on a protected branch.
Base directory of the project.
By assigning a value to baseDirectory, many built-in stages will be wrapped with
dir(baseDirectory) {}.
A map of branches.
Options that are to be added to the build.
Build parameters that will be defined to the build
Upstream builds
Package name of the project
org.zowe.my-project
, the artifacts generated by this project will be placed
into <repo>/org/zowe/my-project/
folder.Reference to the groovy pipeline variable.
Package current semantic version in <major>.<minor>.<patch>
format.
"v"
prefix, and should NOT have pre-release string
and metadata.Constructs the class.
When invoking from a Jenkins pipeline script, the Pipeline must be passed the current environment of the Jenkinsfile to have access to the steps.
def pipeline = new Pipeline(this)
steps
- The workflow steps object provided by the Jenkins pipelineWraps a closure function in a try catch.
Used internally by createStage(StageArguments) to handle errors thrown by timeouts and stage executions.
stage
- The stage that is currently executingclosure
- The closure function to executeGets the stage skip parameter name.
stage
- The stage to skip.Send an email notification about the result of the build to the appropriate users
Add new build options to the pipeline.
If you need custome build options, you can use this method to notify pipeline.
properties()
directly in your pipeline will not work because the library
may oeverwrite your settings.def thottle = [ $class : 'JobPropertyImpl', throttle : [ count : 1, durationName : 'hour', userBoost : true ] ] pipeline.addBuildOption(thottle)
option
- build optionAdd new build parameter to the pipeline
def myBuildParamA = string( name: 'param_a', description: 'My custom build parameter A', defaultValue: 'default-value-of-a', trim: true, required: true ) // this line is not neccessary properties(parameters([myBuildParamA]))
Then you may need to notify the pipeline with this code:
// this line is required when you use Pipeline library pipeline.addBuildParameter(myBuildParamA)
param
- build parameterAdd new build parameters to the pipeline
This is a bulk way to add more than one build parameters.
pipeline.addBuildParameters([ string( name: 'param_a', description: 'My custom build parameter A', defaultValue: 'default-value-of-a', trim: true, required: true ), string( name: 'param_b', description: 'My custom build parameter B', defaultValue: 'default-value-of-b', trim: true, required: true ) ])
params
- list of build parametersAdd new build parameters to the pipeline.
This is a bulk way to add more than one build parameters.
pipeline.addBuildParameters(string( name: 'param_a', description: 'My custom build parameter A', defaultValue: 'default-value-of-a', trim: true, required: true ), string( name: 'param_b', description: 'My custom build parameter B', defaultValue: 'default-value-of-b', trim: true, required: true ))
params
- list of build parametersDefine upstreams of the pipeline.
pipeline.addUpstreams('/Explorer-Data Sets/master', '/Explorer-Jobs/master')
upstreams
- all upstreamsCreates a new stage to be run in the Jenkins pipeline.
Stages are executed in the order that they are created. For more details on what arguments can be sent into a stage, see the StageArguments class.
Stages can also encounter various conditions that will cause them to skip. The following skip search order is used when determining if a stage should be skipped.
If the stage is not skipped after executing the above checks, the stage will continue to its execute phase.
args
- The arguments that define the stage.Creates a new stage to be run in the Jenkins pipeline.
arguments
- A map of arguments that can be instantiated to a org.zowe.jenkins_shared_library.pipelines.base.arguments.StageArguments instance.Setup default branch settings
pipeline.branches.addMap([ [ name : 'master', isProtected : false, buildHistory : 10, ] ])
Pseudo end method, should be overridden by inherited classes
args
- Arguments for the end method.Pseudo end method, should be overridden by inherited classes
args
- A map that can be instantiated as EndArguments.Signal that no more stages will be added and begin pipeline execution.
The end method MUST be the last method called as part of your pipeline. The end method is responsible for executing all the stages previously created after setting the required build options and possible stage parameters. Failure to call this method will prevent your pipeline stages from executing.
Prior to executing the stages, various build options are set. Some of these options include the build history and stage skip parameters. After this is done, the method will execute all of the created stages in the order they were defined.
After stage execution, desired logs will be captured and an email will be sent out to those that made the commit. If the build failed or returned to normal, all committers since the last successful build will also receive the email. Finally if this build is on a protected branch, all emails listed in the admins list will also receive a status email.
end
but had to be named
endBase
due to the issues described in Pipeline.args
- Arguments for the end method.Signal that no more stages will be added and begin pipeline execution.
arguments
- A map that can be instantiated as EndArguments.Gets the first failing stage within _stages
Get a stage from the available stages by name.
stageName
- The name of the stage object to get.Set the build result
result
- The new result for the build.Pseudo setup method, should be overridden by inherited classes
arguments
- The arguments for the added stages.Pseudo setup method, should be overridden by inherited classes
arguments
- A map that can be instantiated as SetupStageArgumentsInitialize the pipeline.
This method MUST be called before any other stages are created. If not called, your Jenkins pipeline will fail. It is also recommended that any public properties of this class are set prior to calling setup.
When extending the Pipeline class, this method must be called in any overridden setup methods. Failure to do so will result in the pipeline indicating that setup was never called.
The setup method creates 2 stages in your Jenkins pipeline using the createStage(Map) function.
setup
but had to be named
setupBase
due to the issues described in Pipeline.arguments
- The arguments for the added stages.Initialize the pipeline.
arguments
- A map that can be instantiated as SetupStageArgumentsGroovy Documentation