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

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?

Related

Why is Jenkins caching my old broken Jenkinsfile?

I have pushed several updates, which actually shows in the changes in jenkins, but the error still shows the same error message when the first Jenkinsfile caused the error.
example:
Started by an SCM change
Obtained Jenkinsfile from git git#bitbucket[myserver]/jenkins_docker.git
Running in Durability level: MAX_SURVIVABILITY
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 2: Expected an agent # line 2, column 3.
agent {
^
WorkflowScript: 2: No agent type specified. Must be one of [any, docker, dockerfile, label, none] # line 2, column 3.
agent {
^
2 errors
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
My jenkinsfile is now updated to look like this:
pipeline {
agent {
label: 'nodejs10'
}
stages {
stage('Test') {
steps {
echo 'Testing...'
}
}
}
}
ok it's not caching. I realised my mistakes.
I had the nodejs plugin installed but I hadn't configured node10 in the global tool configurations for nodejs plugin:
https://wiki.jenkins.io/display/JENKINS/NodeJS+Plugin
Also I was calling it wrong. I have now changed agent to 'any', and followed the instructions on this artcile as below:
https://medium.com/#gustavo.guss/jenkins-starting-with-pipeline-doing-a-node-js-test-72c6057b67d4
pipeline {
agent any
tools {nodejs "nodejs10"}
// stuff here...
}
this works. Though I now have a different issue with npm not installing, but that is now in a separate quesiton.

How to dynamically switch between a docker agent and 'regular' agent in declarative pipleine

I have a jenkinsfile that sometimes needs to run on a node, and sometimes on a docker agent, depending on certain parameters. I'd like to have the jenkins pipeline dynamically switch between using a docker agent declaration and a more normal agent declaration. The only thing I'm missing is how to parameterize the agent declaration. I'm trying to start simple with passing an agent declaration through a variableSo far I have:
def agentDeclaration = {
docker {
label '...'
image "..."
args "..."
}
}
...
pipeline {
agent agentDeclaration
...
}
But this fails with the error:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 117: Only "agent none", "agent any" or "agent {...}" are allowed. # line 117, column 13.
agent agentDeclaration
^
WorkflowScript: 117: No agent type specified. Must be one of [any, docker, dockerfile, kubernetes, label, none] # line 117, column 13.
agent agentDeclaration
I see that similar questions have been asked before here and here. Is this possible?
Try passing the docker agent declaration as a string like def agentDeclaration = “{ docker { ... } }“ and dont forget to escape the double quotes within.

Use GroovyPostbuildSummaryAction in Jenkins pipeline

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)

How do I get ArtifactoryMavenBuild to run with a variable name as an argument for declarative Jenkins?

I am trying to write a Jenkins pipeline using declarative syntax (if I really can't make any progress, I'll switch to script). However, I can't figure out how to get the return value of functions to store to a variable so I can use that variable as an argument of the next function.
The stage of my pipeline looks like this:
stage ('Build') {
steps {
def artServer = getArtifactoryServer(artifactoryServerID: 'my-server')
def mvBuild = newMavenBuild()
def buildInfo = newBuildInfo()
ArtifactoryMavenBuild(mavenBuild: mvBuild, tool: "M3", pom: "pom.xml", goals: "-B clean test -Dmaven.test.failure.ignore", opts: "", buildInfo: buildInfo)
}
}
My error log is:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 19: Expected a step # line 19, column 17.
def artServer = getArtifactoryServer(artifactoryServerID: 'GE-Propel-Artifactory')
^
WorkflowScript: 20: Expected a step # line 20, column 17.
def mvBuild = newMavenBuild()
^
WorkflowScript: 21: Expected a step # line 21, column 17.
def buildInfo = newBuildInfo()
The ArtifactoryMavenBuild function works when I put it like this:
ArtifactoryMavenBuild(mavenBuild: newMavenBuild(), tool: "M3", pom: "pom.xml", goals: "-B clean test -Dmaven.test.failure.ignore", opts: "", buildInfo: newBuildInfo())
But I need to be able to reference mvBuild and buildInfo again for a later step.
The documentation for declarative jenkins for the Artifactory plugin is here: https://jenkins.io/doc/pipeline/steps/artifactory/
Try to wrap you script code into a script {} step like so:
stage ('Build') {
steps {
script {
def artServer = getArtifactoryServer(artifactoryServerID: 'my-server')
def mvBuild = newMavenBuild()
def buildInfo = newBuildInfo()
ArtifactoryMavenBuild(mavenBuild: mvBuild, tool: "M3", pom: "pom.xml", goals: "-B clean test -Dmaven.test.failure.ignore", opts: "", buildInfo: buildInfo)
}
}
}

groovy script to remove users from jenkins project role alone?

import hudson.security.*
import jenkins.security.*
import jenkins.model.Jenkins
def sids = Jenkins.instance.authorizationStrategy.getAllSIDs()
return sids
In the Build Section;
def removeAMP(Job jobName, user ) {
println jobName.name.center(80,'-')
def authorizationMatrixProperty = jobName.getProperty(AuthorizationMatrixProperty.class)
Map<Permission,Set<String>> Permissions = authorizationMatrixProperty.getGrantedPermissions()
println "Permission Map Before: " + Permissions + cr
println "Permission Values: " + Permissions.values() + cr
for (Set<String> permissionUsers:Permissions.values()) {
permissionUsers.remove(user)
}
println "Permission Map After: " + Permissions + cr
jobName.save();
}
I created a job in jenkins with "Execute system grrovy script" for removing users from project role not from jenkins global role.with following scripts,but am getting error as follows:
ERROR: Build step failed with exception
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 10: unable to resolve class Job
# line 10, column 19.
def removeAMP(Job jobName, user ) {
^
1 error
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:302)
at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:861)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:550)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:499)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:302)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:281)
at groovy.lang.GroovyShell.parseClass(GroovyShell.java:731)
at groovy.lang.GroovyShell.parse(GroovyShell.java:743)
at groovy.lang.GroovyShell.parse(GroovyShell.java:723)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:680)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:666)
at hudson.plugins.groovy.SystemGroovy.perform(SystemGroovy.java:81)
at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:782)
at hudson.model.Build$BuildExecution.build(Build.java:205)
at hudson.model.Build$BuildExecution.doRun(Build.java:162)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:534)
at hudson.model.Run.execute(Run.java:1738)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:98)
at hudson.model.Executor.run(Executor.java:410)
Build step 'Execute system Groovy script' marked build as failure
Finished: FAILURE
Please help me with correct script.Thanks in advance
Ashif
Do you need to add
import hudson.model.Job

Resources