I have a shared library that extends the pipeline.
Inside of my class I have a variable that is a Gstring.
If I call a method after this variable, the Jenkinsfile produces an error when running
class SlackNotifierImpl implements SlackNotifier, Serializable {
....
final def currentBuildJob = "${script.env.JOB_NAME} ${script.env.BUILD_NUMBER}"
def variable = someMethod()
}
but, if I do something else after defining the variable, there is no problem
class SlackNotifierImpl implements SlackNotifier, Serializable {
....
final def currentBuildJob = "${script.env.JOB_NAME} ${script.env.BUILD_NUMBER}"
def variable = 'xxx'
}
The error produced is the following:
* sometimes, I get this:
com.cloudbees.groovy.cps.impl.CpsCallableInvocation
* some other times, I get this:
java.lang.UnsupportedOperationException: Refusing to marshal org.codehaus.groovy.runtime.GStringImpl for security reasons
at hudson.util.XStream2$BlacklistedTypesConverter.marshal(XStream2.java:452)
at com.thoughtworks.xstream.core.AbstractReferenceMarshaller.convert(AbstractReferenceMarshaller.java:69)
at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:58)
at com.thoughtworks.xstream.core.AbstractReferenceMarshaller$1.convertAnother(AbstractReferenceMarshaller.java:84)
at hudson.util.RobustReflectionConverter.marshallField(RobustReflectionConverter.java:265)
at hudson.util.RobustReflectionConverter$2.writeField(RobustReflectionConverter.java:252)
Caused: java.lang.RuntimeException: Failed to serialize org.ecg.es.jenkins.pipeline.shared.library.helpers.notification.SlackNotifierImpl#currentBuildJob
The Jenkinsfile is the following:
Jenkinsfile (uses declarative):
#!/usr/bin/env groovy
/*
Template for Sending Slack Notifications with the Result of the Build
*/
import com.lesfurets.jenkins.unit.global.lib.Library
@Library('pipeline-library@development') import org.ecg.es.jenkins.pipeline.shared.library.LibraryFacade
def library = new LibraryFacade(this)
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building..'
}
}
}
post {
success {
echo 'Build was succesful.'
script {
library.sendSuccessfulBuildNotificationToSlack()
}
}
changed {
echo 'Build status has changed.'
script {
library.sendChangedBuildStatusNotificationToSlack()
}
}
failure {
echo 'Build was not succesful.'
script {
library.sendFailedBuildNotificationToSlack()
}
}
}
}
def testMethod(){
echo 'Testing this works'
}
Any idea of what is wrong here?
Thanks, best
C.