[JIRA] (JENKINS-57564) Documentation Update - Jenkins Shared Library Pipeline - Singleton vs On-Demand Singleton

6 views
Skip to first unread message

bsnuggs1@gmail.com (JIRA)

unread,
May 20, 2019, 11:43:02 AM5/20/19
to jenkinsc...@googlegroups.com
Brandon Snuggs created an issue
 
Jenkins / Improvement JENKINS-57564
Documentation Update - Jenkins Shared Library Pipeline - Singleton vs On-Demand Singleton
Issue Type: Improvement Improvement
Assignee: Unassigned
Components: core
Created: 2019-05-20 15:42
Labels: singletons
Priority: Minor Minor
Reporter: Brandon Snuggs

In the Jenkins documentation, it mentions that all var definitions are on-demand singletons, but does not explain what that means. Documentation reference: https://jenkins.io/doc/book/pipeline/shared-libraries/#defining-global-variables

 

Does this mean that the singleton is determined at the moment the job is called, so its a unique instance for that particular job? And how does this compare to an explicit @Singleton call when placed on a class?

 

I tried setting up my shared pipeline so I could review both sides of it.  When I run the code below, I can see in the output that both classes print out the information I expect. They both start as empty strings and then are set equal to the env.BUILD_TAG.

 

My concern is that if I have multiple jobs running concurrently, do they share the same Singleton or is it unique to each build? So far I have not been able to prove this, so I was hoping someone has an answer to this.

 

Structure

src
 - org
 - utilities
 - GlobalStateClass.groovy
vars
 - globalStateVar.groovy
 - main.groovy

GlobalStateClass.groovy

#!groovy
package org.utilities
@Singleton
class GlobalStateClass implements Serialization{
 public String version = ""
}

 

globalStateVar.groovy

#!groovy
class globalStateVar implements Serialization{
 public String version = ""
}

main.groovy

#!groovy
import org.utilities.GlobalStateClass
def call () {
 println "Old Values: ${ GlobalStateClass.instance.version } ${ state.version }"
 String v = env.BUILD_TAG
 GlobalState.instance.version = v
 state.version = v
 println "New Values: ${ GlobalStateClass.instance.version } ${ state.version }"
}

 

Jenkinsfile

pipeline {
 agent any
 stages {
   stage('test 0'){
     main()
   }
   stage('test 1'){
     main()
   }
   stage('test 2'){
     main()
   }
   stage('test 3'){
     main()
   }
 }
}
Add Comment Add Comment
 
This message was sent by Atlassian Jira (v7.11.2#711002-sha1:fdc329d)

o.v.nenashev@gmail.com (JIRA)

unread,
May 20, 2019, 5:43:01 PM5/20/19
to jenkinsc...@googlegroups.com

o.v.nenashev@gmail.com (JIRA)

unread,
May 20, 2019, 5:43:01 PM5/20/19
to jenkinsc...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages