Use GroovyPostbuildSummaryAction in Jenkins pipeline - jenkins

How can I use GroovyPostbuildSummaryAction in my Jenkins pipeline?
I tried:
import org.jvnet.hudson.plugins.groovypostbuild.*;
But it does not work:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 81: unable to resolve class GroovyPostbuildSummaryAction
# line 81, column 34.
GroovyPostbuildSummaryAction action = new GroovyPostbuildSummaryAction("star-gold.png");
I want to do a test with a shared library in my pipeline and using this snippet: https://gist.github.com/hayderimran7/7a49d0fa484f7561120b4190f8f3a888#file-retro-add-badges-groovy

Its not clear if you invoke the action for example
GroovyPostbuildSummaryAction action = new GroovyPostbuildSummaryAction('star-gold.png')
action.appendText('Comment line',false)
currentBuild.rawBuild.getActions().add(action)
The above works for me (we currently use GroovyPostBuild plugin 2.3.1)

Related

WorkflowScript: 1: unable to resolve class groovy.yaml.YamlSlurper

I am use the Jenkins docker container to run Jenkins. I have this pipeline:
import groovy.yaml.YamlSlurper
def wholeFileData
def ys = new YamlSlurper()
pipeline {
agent any
stages {
...
... and I get this error:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 1: unable to resolve class groovy.yaml.YamlSlurper
# line 1, column 1.
import groovy.yaml.YamlSlurper
^
How can I fix this?

Groovy: Unable to read a json file from jenkins workspace

I have to read some input from report.json file which is in the jenkins workspace. I am using the following code to read the file but it says file not found error. I am running this script on Groovy Postbuild step.
import hudson.model.*
def fileContents = new File('C:\\jenkinstest_slave\\workspace\\Cypress\\mochawesome-report\\report.json').readLines()
def result = fileContents.findAll { it.contains('passPercent') }
manager.listener.logger.println("matching word from findAll method= " +result)
Error:
Groovy script failed:
java.io.FileNotFoundException
Can someone please help me to resolve this?

Jenkins "unable to resolve class Declarative" error when building Python script

I began learning to use to Jenkins and wanted to make it run a Python script of mine automatically. I followed their tutorial and created a new Project called Pipeline Test.
I've also added the GitHub repo of a Python script I wanted to test (https://github.com/mateasmario/spam-bot).
As you can see, I've created a Jenkinsfile in that repo. Because my script is called spam-bot.py, I want my Jenkinsfile to run that script every time I click "Build now" inside Jenkins. This is the content of my Jenkinsfile:
Jenkinsfile (Declarative Pipeline)
pipeline {
agent { docker { image 'python:3.10.1-alpine' } }
stages {
stage('build') {
steps {
sh 'python spam-bot.py'
}
}
}
}
The problem is, whenever I click "Build now", my build fails and the console outputs the following error:
Started by user Mario Mateas
Obtained Jenkinsfile from git https://github.com/mateasmario/spam-bot.git
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 1: unable to resolve class Declarative
# line 1, column 26.
Jenkinsfile (Declarative Pipeline)
^
1 error
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:958)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:605)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:554)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:142)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:127)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:571)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:523)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:334)
at hudson.model.ResourceController.execute(ResourceController.java:99)
at hudson.model.Executor.run(Executor.java:432)
Finished: FAILURE
I looked up for this error on the internet, but didn't find any useful information, and that's why I decided to ask here.
I also don't have any Docker container configured. Do I need to configure one? I took a look over Jenkins' documentation for Docker, but didn't see any useful information for adding a Python image (as the one mentioned at the beginning of the Jenkinsfile) to the container.
Your Jenkinsfile contains invalid syntax on the first line, which is why the error is being thrown. Assuming you intended that first line to be a comment, you can modify the pipeline code to be:
// Jenkinsfile (Declarative Pipeline)
pipeline {
...
}
and your pipeline code will have valid syntax.

Using activeChoiceReactiveParam in pipeline

How to use activeChoiceReactiveParam in Jenkins pipeline?
this is the relevant part of my jenkins file:
parameters{
activeChoiceParam('foo') {
description('zzzz')
filterable()
choiceType('SINGLE_SELECT')
groovyScript {
script('xx')
fallbackScript('xx')
}
}
}
Getting this error when running the build:
WorkflowScript: 10: Build parameters definitions cannot have blocks # line 10, column 6.
activeChoiceParam('foo') {
^
I think that as this post says, pipeline doesn't support activeChoiceParams.
That post is from 2 years ago but seems to be valid still; the Active Choices plugin repo links a Jenkins issue which blocks pipeline from working well with the plugin, and their pipeline example is similar to what they described in the post I linked.

How to add nested view in Jenkins using groovy?

I am trying to add a nested view using groovy script but is failing
Jenkins.instance.instance.addView(new hudson.plugins.nested__view.NestedView("Sample View"))
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 3: unable to resolve class hudson.plugins.nested__view.NestedView
# line 3, column 35.
Jenkins.instance.instance.addView(new hudson.plugins.nested__view.NestedView("ViewX"
This worked for me in the Jenkins script console:
Jenkins.instance.addView(new hudson.plugins.nested_view.NestedView("Sample View"))

Resources