Exception inside a Jenskinsfile pipeline stage - jenkins-pipeline

I'm encountering a weird error inside a Jenkinsfile pipeline stage :
an exception which occurred:
in field com.cloudbees.groovy.cps.impl.BlockScopeEnv.locals
in object com.cloudbees.groovy.cps.impl.BlockScopeEnv#195b2bb
in field com.cloudbees.groovy.cps.impl.CallEnv.caller
in object com.cloudbees.groovy.cps.impl.FunctionCallEnv#5866b739
in field com.cloudbees.groovy.cps.Continuable.e
in object org.jenkinsci.plugins.workflow.cps.SandboxContinuable#c914b56
in field org.jenkinsci.plugins.workflow.cps.CpsThread.program
in object org.jenkinsci.plugins.workflow.cps.CpsThread#aa4a561
in field org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.threads
in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup#8e1e22a
in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup#8e1e22a
Caused: java.io.NotSerializableException: hudson.plugins.git.GitChangeSetList
at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:926)
at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:568)
The stage is like this :
stage("Get issues desc") {
steps {
script {
def changeLogSets = currentBuild.changeSets
echo("changeSets = " + changeLogSets)
def issueArray = []
for (int i = 0; i < changeLogSets.size(); i++) {
def entries = changeLogSets[i].items
for (int j = 0; j < entries.length; j++) {
def commitMsg = entries[j].msg
def issueNumbers = (commitMsg =~ /#(\d+)/)
def issueNumber = issueNumbers[0][1]
issueArray << issueNumber
}
}
def issueDesc = sh(script: 'curl --globoff --header "PRIVATE-TOKEN: xxxxxx" https://my-server/rest/endpoint',
returnStdout: true)
def issueDescJson = readJSON text: issueDesc
}
}
}
When I move the 3 last lines (def issueDesc.... def issueDescJson) to the beginning of the same stage, I have no error
Could someone help me ?
Thanks

Related

Elasticsearch painless sorting null_pointer_error

I'm trying to create a sorting script with painless, filtering nested documents.
The reason I'm doing this with a script, is because I need to emulate a COALESCE statement.
My documents have titles stored like:
{
title: [
{
type: MainTitle,
value: [
{
language: eng,
label: The title
},
{
language: ger,
label: Das title
}
]
},
{
type: AvailabilityTitle,
value: [
{
language: eng,
label: New thing!
}
]
}
]
}
title and title.value are nested documents.
I want to sort documents primarily by their english MainTitle and by their german MainTitle only if no english MainTitle exists - even if the german title gave a higher score.
I'm trying to simply sort by the english MainTitle first to try it out and this is the script:
def source = params._source;
def titles = source.title;
if (titles != null && titles.length > 0) {
for(int i=0; i < titles.length; i++) {
def t = titles[i];
if (t.type == 'MainTitle') {
def values = t.value;
if (values != null && values.length > 0) {
for (int j = 0; j < values.length; j++) {
def v = values[j];
if (v.language == 'eng') {
return v.label;
}
}
}
}
}
}
return \"\";
For some reason I'm getting a null_pointer_exception
"script_stack": [
"if (values != null && values.length > 0) { ",
" ^---- HERE"
],
I don't get how values can be null at that point since I'm specifically checking for null just before it.
The null_pointer_exception is thrown, not because values is null, but because values does not have a method/function called length. That is because for some reason values is an ArrayList even though titles earlier is an Array. Apparently they both have the method/function size() so I can just use that.
So this works:
def source = params._source;
def titles = source.title;
if (titles != null && titles.size() > 0) {
for(int i=0; i < titles.size(); i++) {
def t = titles[i];
if (t.type == 'MainTitle') {
def values = t.value;
if (values != null && values.size() > 0) {
for (int j = 0; j < values.size(); j++) {
def v = values[j];
if (v != null && v.language == 'fin') {
return v.label;
}
}
}
}
}
}
return '';

"invalid sequence of tokens near ['for']" error in painless script query

I am getting error while executing this script query using Nest library in .net:
new ScriptQuery
{
Lang = "painless",
Source = "(!doc[params.headquartersCoordinatesField].empty && doc[params.headquartersCoordinatesField].arcDistance(params.latitude, params.longitude) * 0.000621371 <= params.maxDistance) || (!doc[params.offices].empty && (for (def office : doc[params.offices].values){if(office.coordinates).arcDistance(params.latitude, params.longitude) * 0.000621371 < =params.maxDistance{return true;}}))",
Params = new Dictionary<string, object>
{
{"headquartersCoordinatesField", Field<Provider>(f => f.Headquarters.Coordinates)},
{"offices", Field<Provider>(f => f.Offices)},
{"latitude", _latitude},
{"longitude", _longitude},
{"maxDistance", 50}
}
}
This is the error I get :
ServerError: Type: search_phase_execution_exception Reason: "all shards failed" CausedBy: "Type: script_exception Reason: "compile error" CausedBy: "Type: illegal_argument_exception Reason: "invalid sequence of tokens near ['for']." CausedBy: "Type: no_viable_alt_exception Reason: "no_viable_alt_exception: null""""
I also tried boolean variable inside loop and try to return that at the end but I get the same error.
I tried simple for loop with counter (i) to check the syntax but same error. So it seems like anything I use inside loop is returning error.
Can someone help to find the correct syntax ? Thanks in advance.
You cannot have a for loop inside a condition, it doesn't make sense.
Here is how your script should look like:
def hqCoordExist = !doc[params.headquartersCoordinatesField].empty;
def distToHq = doc[params.headquartersCoordinatesField].arcDistance(params.latitude, params.longitude);
if (hqCoordExist && distToHq * 0.000621371 <= params.maxDistance) {
return true;
}
def officesExist = !doc[params.offices].empty;
if (officesExist) {
for (def office : doc[params.offices].values) {
def distToOffice = office.coordinates.arcDistance(params.latitude, params.longitude);
if (distToOffice * 0.000621371 <= params.maxDistance) {
return true;
}
}
}
return false;
This worked for me. I had to check key and size of offices param. :
if(!doc[params.headquartersCoordinatesField].empty && doc[params.headquartersCoordinatesField].arcDistance(params.latitude, params.longitude) * 0.000621371 <= params.maxDistance)
{
return true;
}
def officesCollection = new ArrayList();
if(doc.containsKey(params.offices) && doc[params.offices].size() > 0)
{
officesCollection = doc[params.offices].value;
for (def office : officesCollection)
{
def distToOffice =
office.coordinates.arcDistance(params.latitude, params.longitude);
if (distToOffice * 0.000621371 <= params.maxDistance)
{
return true;
}
}
}
return false;

GRAILS - GORM : DuplicateKeyException during saving a new object

I use GORM to back occurrences in a database, from an excel file.
new ExcelBuilder(excelFile.inputStream).eachLine([labels: true, sheet: 0]) {
if (cell(0)) {
def nameA = cell(0)
def nameB = cell(1)
def a = Chapitre.findByNom(nameA)
def code = cell(2)
def designation = cell(3)
if (code == null || nameA == null || nameB == null) {
flash.messages << "error"
} else if (!Chapitre.findByNom(nameA)) {
flash.messages << "error"
} else if ( Rubrique.where{nom == nameB && chapitre == a}.list().size() == 0) {
flash.messages << "error"
} else if(Object.where{rubrique == Rubrique.findByNom(nameB) && c == code && d == designation}.count() > 0){
flash.messages << "error"
} else {
def b = Rubrique.findByNom(nameB)
def isNew = false;
Object.withNewSession {session2->
def object = Object.findOrCreateByCode(code)
if(object.designation == null)
isNew = true;
object.rubrique = b
object.d= (designation == null)?"":designation
// try {
rowCount += object.save()? 1 : 0
// } catch(ValidationException) {
// if(isNew)
// rowCount++;
// log.info("ErreuRRRRRRRRrrrRRrrRrRRrrrrRrrrRrrrRrrrr")
// }
}
}
}
currentLine++
}
flash.messages << "${rowCount} ligne create or update"
An update will break any worries, the course of the lines of file continue and database recording is effective.
However when it comes to inserting a new object, I get an Exception:
org.springframework.dao.DuplicateKeyException: a different object with the same identifier value was already associated with the session:[fen.NuisanceType#2202];
nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session
The registration of the object in question is effective , but the error raised off the path of the file.
When i uncommented the try and catch I bypasses the error and so all my copies of each file are created in the database .
I thus found a way around my worries, but I do not find it very clean and i come to you to try to understand my problem.
Without further information hard to give any clear answers. Is this code back in a service?(very much doubt it since it has flash.message pointing to controller doing all of it.) Try making it into a service and transactional then maybe you could look at removing withNewTransaction call.
You can read more about error created here:
Grails - DuplicateKeyException
Review comment:
"Well, that problem occurs when you are initializing a class new ClassD with an id or unique property manually, when there is another already in the session. So, you should try to get that entity first (that's what findOrCreateWhere does, but if you use an id you need to use get) and then use the instance found or create a new one for the update"
Hibernate Error: a different object with the same identifier value was already associated with the session
You code tidied up and running from service: (issues may go away) since I have also cleaned up the duplicated finds you were doing:
class TestService {
static transactional=true
def saveRecord() {
def results=[]
new ExcelBuilder(excelFile.inputStream).eachLine([labels: true, sheet: 0]) {
if (cell(0)) {
def nameA = cell(0)
def nameB = cell(1)
def code = cell(2)
def designation = cell(3)
def a = Chapitre.findByNom(nameA)
def b = Rubrique.where{nom == nameB && chapitre == a}
def c = Object.where{rubrique == b && c == code && d == designation}
if (!code||!nameA||!nameB||!a||!b||!c) {
results << "Error saving ${nameA} ${nameB} ${code}"
} else {
//boolean isNew = false
def object = Object.findOrSaveWhere(code:code)
if(object) {
if (!object.designation) {
rowCount++
results << "Record ${object} has no designation ? new Record?"
}
object.rubrique = b
object.d = designation ?: ''
object.save()
results << "Record ${object.id} is saved"
} else {
/*
* Could not save or find code:code now create a new object:
* object = new Object(code:code, rubrique:rubrique: d: designation ?: '').save()
*
*/
}
}
}
currentLine++
}
results << "${rowCount} ligne create or update"
return results
}
}

grails ajax onload and oncomplete

can anyone give me sample coding that using grails ajax onload and oncomplete functions on following reader action? i want show a spinner icon while this action is onload and redirect to the list page while action is oncomplete..
def reader(){
def list = []
def dir = new File("C:/Users/User/Desktop/Summarize_20141212/HR_FILE")
dir.eachFileRecurse(FileType.FILES) { file ->
list << file
}
println list
list.each {
File file = new File(it.path)
def sql = groovy.sql.Sql.newInstance("jdbc:postgresql://localhost:5432/new",
'postgres', 'sa', "org.postgresql.Driver")
def lincoln = 0
file.eachLine() { line ->
if (line.trim().size() == 0) {
return null
} else {
def field = []
def tokens = line.split(',(?=([^\"]*\"[^\"]*\")*[^\"]*$)')
file.name.lastIndexOf('.').with { it != -1 ? file.name[0..<it] : file.name }
lincoln++
field.push(file.name)
for (String t : tokens) {
field.push(t)
}
while (field.size() < 10) {
field.push("")
}
if (lincoln > 1) {
sql.execute('insert into read_hr(version,col01,col02,col03,col04,col05,col06,col07,col08,col09,col10)' +
'VALUES (0,?,?,?,?,?,?,?,?,?,?)', field)
System.out.println(field);
}
}
}
}
redirect (action:"list")
}
These link will useful to you .
http://www.javacodegeeks.com/2013/05/grails-ajax-examples.html
http://javadeveloper.asia/grails-ajax-tutorial-remotelink-tag

Saving of grails domain object with an attribute type object fails

I'm trying to save a domain object with grails 2.3.3 But it is not saved. How can I save it and why is it not saving?
The domain code:
package berg
import nl.jappieklooster.Log
class FieldValue {
Object value
public String toString(){
Log.debug "Field value: {0}", value
return value.toString()
}
static constraints = {
}
}
The code that saves:
// an extract from the bootsrap file
def init = { servletContext ->
def blueFV = new FieldValue(value: Color.blue)
def smallFV = new FieldValue(value: "small")
def fieldVals = [blueFV, smallFV]
saveData(fieldVals,berg.FieldValue)
}
public void saveData(List list, Class type){
def wholeList = type.list() ?: []
println("Started with adding the "+type.getName()+" classes.")
int saved = 0;
int failed = 0;
if(!wholeList){
list.each{ i ->
if(i.validate()){
i.save(flush:true, failOnError: true)
saved++
}
else{
println("! - - - Warning: '"+i.toString()+"' could not be created! - - - !")
failed++
}
}
if(failed > 0)//if one fails, let the message appear more clearly
println(".v.v.")
println("When saving the "+type.getName()+" classes: "+saved+" were saved, "+failed+" failed to be saved.")
if(failed > 0)
println(".^.^.")
}
}
The entire value column does not show up in the database

Resources