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"))
Related
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?
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.
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 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)
I'd need to get started with JJB however one issue I'm facing is that I'm not able to upload jobs in an existing Folder (I'm using Cloudbees Folder plugin).
I've arranged an example based on an existing question available on SO:
- project:
name: playground
- job:
name: sample_job
description: 'Automatically generated test'
project-type: freestyle
builders:
- shell: 'ls'
So the job should be loaded into the "playground" folder.
However the syntax seems uncorrect:
File "/usr/lib64/python2.7/site-packages/yaml/constructor.py", line 37, in get_single_data
node = self.get_single_node()
File "/usr/lib64/python2.7/site-packages/yaml/composer.py", line 36, in get_single_node
document = self.compose_document()
File "/home/francesco/.local/lib/python2.7/site-packages/jenkins_jobs/local_yaml.py", line 282, in compose_document
node = self.compose_node(None, None)
File "/usr/lib64/python2.7/site-packages/yaml/composer.py", line 82, in compose_node
node = self.compose_sequence_node(anchor)
File "/usr/lib64/python2.7/site-packages/yaml/composer.py", line 110, in compose_sequence_node
while not self.check_event(SequenceEndEvent):
File "/usr/lib64/python2.7/site-packages/yaml/parser.py", line 98, in check_event
self.current_event = self.state()
File "/usr/lib64/python2.7/site-packages/yaml/parser.py", line 393, in parse_block_sequence_entry
"expected <block end>, but found %r" % token.id, token.start_mark)
yaml.parser.ParserError: while parsing a block collection
in "/home/francesco/git/tests-jobs/jobs/test.yaml", line 1, column 1
expected <block end>, but found '?'
in "/home/francesco/git/tests-jobs/jobs/test.yaml", line 2, column 1
The existing answers provided so far on this question are related to folders created as part of JJB. In my case the folder has been already created ahead.
Any help ?
#francesco, Basically you have to use jobs, job template to create a jenkins job. I have tried this based on your question and able to execute without failures. Check if this answers your question.
---
- project:
name: playground
jobs:
- sample_job
- job-template:
name: sample_job
description: 'Automatically generated test'
project-type: freestyle
builders:
- shell: 'ls'