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

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.

Related

Looking for a jenkins pipeline build job syntax that really works

I'm trying to trigger a jenkins pipeline job from inside another jenkins pipeline job, with parameters.
I'm relatively newbie in java/groovy so I search the web for functional samples but all that I found are unusable for syntax or scripting reasons.
Some of my tests below:
How to trigger another Jenkins pipeline that needs a $BRANCH variable?
node() {
build job: 'INVENTORIES', propagate: true, wait: true
}
Failed: java.lang.NoSuchMethodError: No such DSL method 'build' found among steps [ansiblePlaybook
Jenkins pipeline for building other jobs
node() {
stage('Desc1') {
steps {
dir('/var/lib/jenkins/workspace') {
build job: 'INVENTORIES', propagate: true, wait: true
}
}
}
}
Failed: java.lang.NoSuchMethodError: No such DSL method 'steps' found among steps [ansiblePlaybook,
node() {
stages {
stage ("build") { //an arbitrary stage name
steps {
build 'INVENTORIES' //this is where we specify which job to invoke.
}
}
}
}
Failed: java.lang.NoSuchMethodError: No such DSL method 'stages' found among steps [ansiblePlaybook,
I've tried plenty of samples (script block, step block, stage block...) but it never works, always throwing java exception like:
java.lang.ClassCastException: org.jenkinsci.plugins.workflow.steps.CoreStep.delegate expects interface jenkins.tasks.SimpleBuildStep but received class ...
Before I jump from a bridge, could anyone here helps me?
Thanks in advance, I know swimming but it's a little cold
[SOLVED]
It were missing a Pipeline Plugin but the error messages wasn't clear enought and log content too poor to guess.
Thanks to #zett42 to have pointed me on the good search way.
Have a nice day.

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.

Issue in Jenkins integration with GitHub and Ionic project

I am trying to integrate ionic application with Jenkins and i am using GitHub as a repository. When i run the Multibranch pipeline job manually, the Jenkin is displaying below error.
Does anyone know what can be possible issue ? I am trying to learn how CI/CD works with this demo experiment.
I am referring this Youtube video : https://www.youtube.com/watch?v=q5voVJb3rHI&t=605s
Started by user admin
13:42:45 Connecting to https://api.github.com using PrashobThekkyal/*****
Obtained Jenkinsfile from a85fffb01a96c5c5d550326873047c6071557aff
Running in Durability level: MAX_SURVIVABILITY
GitHub has been notified of this commit’s build result
java.lang.IllegalArgumentException: Unsupported class file major version 57
at org.kohsuke.asm6.ClassReader.<init>(ClassReader.java:176)
at org.kohsuke.asm6.ClassReader.<init>(ClassReader.java:158)
at org.kohsuke.asm6.ClassReader.<init>(ClassReader.java:146)
at org.jenkinsci.bytecode.helper.ClassLoadingReferenceTypeHierachyReader.reader(ClassLoadingReferenceTypeHierachyReader.java:64)
at org.jenkinsci.bytecode.helper.TypeHierarchyReader.hierarchyOf(TypeHierarchyReader.java:176)
at org.jenkinsci.bytecode.helper.ClassLoadingReferenceTypeHierachyReader.hierarchyOf(ClassLoadingReferenceTypeHierachyReader.java:39)
at org.jenkinsci.bytecode.helper.TypeHierarchyReader$TypeHierarchy.isAssignableFrom(TypeHierarchyReader.java:321)
at org.jenkinsci.bytecode.helper.TypeHierarchyReader$TypeHierarchy.isAssignableFromAnyInterfaceImplementedBy(TypeHierarchyReader.java:329)
at org.jenkinsci.bytecode.helper.TypeHierarchyReader$TypeHierarchy.isAssignableFrom(TypeHierarchyReader.java:311)
at org.jenkinsci.bytecode.helper.TypeHierarchyReader.isAssignableFrom(TypeHierarchyReader.java:124)
at org.jenkinsci.bytecode.helper.ClassLoadingReferenceTypeHierachyReader.isAssignableFrom(ClassLoadingReferenceTypeHierachyReader.java:39)
at org.jenkinsci.bytecode.helper.TypeHierarchyReader.getCommonSuperClass(TypeHierarchyReader.java:131)
at org.jenkinsci.bytecode.helper.ClassLoadingReferenceTypeHierachyReader.getCommonSuperClass(ClassLoadingReferenceTypeHierachyReader.java:39)
at org.jenkinsci.bytecode.NonClassLoadingClassWriter.getCommonSuperClass(NonClassLoadingClassWriter.java:72)
at org.kohsuke.asm6.SymbolTable.addMergedType(SymbolTable.java:1209)
at org.kohsuke.asm6.Frame.merge(Frame.java:1293)
at org.kohsuke.asm6.Frame.merge(Frame.java:1238)
at org.kohsuke.asm6.MethodWriter.computeAllFrames(MethodWriter.java:1604)
at org.kohsuke.asm6.MethodWriter.visitMaxs(MethodWriter.java:1540)
at org.kohsuke.asm6.tree.MethodNode.accept(MethodNode.java:807)
at org.kohsuke.asm6.commons.JSRInlinerAdapter.visitEnd(JSRInlinerAdapter.java:150)
at org.jenkinsci.bytecode.Transformer$1$1.visitEnd(Transformer.java:109)
at org.kohsuke.asm6.MethodVisitor.visitEnd(MethodVisitor.java:783)
at org.kohsuke.asm6.ClassReader.readMethod(ClassReader.java:1278)
at org.kohsuke.asm6.ClassReader.accept(ClassReader.java:679)
at org.kohsuke.asm6.ClassReader.accept(ClassReader.java:391)
at org.jenkinsci.bytecode.Transformer.transform(Transformer.java:115)
at hudson.ClassicPluginStrategy$AntClassLoader2.defineClassFromData(ClassicPluginStrategy.java:706)
at jenkins.util.AntClassLoader.getClassFromStream(AntClassLoader.java:1310)
at jenkins.util.AntClassLoader.findClassInComponents(AntClassLoader.java:1363)
at jenkins.util.AntClassLoader.findClass(AntClassLoader.java:1326)
at jenkins.util.AntClassLoader.loadClass(AntClassLoader.java:1079)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at org.jenkinsci.plugins.workflow.cps.CpsScript.<init>(CpsScript.java:69)
at WorkflowScript.<init>(WorkflowScript)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
at java.base/java.lang.reflect.ReflectAccess.newInstance(ReflectAccess.java:166)
at java.base/jdk.internal.reflect.ReflectionFactory.newInstance(ReflectionFactory.java:404)
at java.base/java.lang.Class.newInstance(Class.java:591)
at org.codehaus.groovy.runtime.InvokerHelper.createScript(InvokerHelper.java:434)
Caused: groovy.lang.GroovyRuntimeException: Failed to create Script instance for class: class WorkflowScript. Reason
at org.codehaus.groovy.runtime.InvokerHelper.createScript(InvokerHelper.java:466)
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:561)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:522)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:327)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:429)
Finished: FAILURE
Jenkinsfile
pipeline {
agent any
stages {
stage ('Build') {
steps {
echo "Building Multibranch Pipeline"
}
}
stage ('Testing') {
steps {
echo "Testing Multibranch Pipeline"
}
}
stage ('Deployment') {
steps {
echo "False Deploying Multibranch Pipeline"
}
}
}
}
Jenkins Job configuration:
After days of analysis finally I have found the solution. I have did a very basic mistake of not reading version requirement to run Jenkins.
Java requirement to run Jenkins job
As mentioned in above URL, under the section 'Running Jenkins', they have clearly mentioned the support versions of java to run Jenkins job. So I checked java version and it was running on Java 13-a, yes that was stupid, because the Jenkins job will only run with java version 8 and 11.
So degrade your java version to 8 or 11 and it will work smoothly.
Proof
I thought to delete my stupid question, but later I thought to keep it, if in case it could help somebody. Kindly please don't give any kind of (+ve or -vew) points to this post, as it doesn't make any sense.

Unspecified Jenkinsfile Null Pointer Error in Workflow Steps

I encountered the following error in a Jenkinsfile pipeline I was building:
java.lang.NullPointerException
at org.jenkinsci.plugins.workflow.steps.CoreStep$Execution.run(CoreStep.java:80)
at org.jenkinsci.plugins.workflow.steps.CoreStep$Execution.run(CoreStep.java:67)
at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Finished: FAILURE
The issue here, primarily, is that I'm not sure exactly what is going on. Since all it's mentioning is a null pointer error, I can't exactly be sure, and I can't find anything more specific.
Here's my Jenkinsfile:
#!groovy
node {
withEnv(["WORKSPACE=${pwd()}"]) { //Setting Workspace to the current directory
stage('Clone repository...') {
checkout scm //Let checkout automagically handle pulling in all the names we need and whatnot
}
stage('Building WAR...') {
step(withMaven(
// Maven installation declared in the Jenkins "Global Tool Configuration"
maven: 'Maven 3.6.0') {
// Run the maven build
sh 'mvn clean install' //Same as running on local
sh 'mv ${WORKSPACE}/target/QUserService.war ${WORKSPACE}/target/QUserService_War-QUserService-${BRANCH_NAME}-${BUILD_NUMBER}.war'
//For above line, 'mv' is the Linux command to rename/move files, which is needed for the UCD script
}
// withMaven will discover the generated Maven artifacts, JUnit Surefire & FailSafe & FindBugs reports...
)
}
}
}
So - first, you don't need to define WORKSPACE. It is defined for you by Jenkins. You can convince yourself of this by running sh 'set' on a linux agent.
Next, you don't need to check out the project. It will already be there (assuming you're using a pipeline project).
Next, you don't need to put the withMaven inside of a step call. In a scripted pipeline, the stuff in a stage is groovy script. Step isn't required.
node {
stage('Building WAR...') {
withMaven(
maven: 'Maven 3.5.0') {
// Run the maven build
sh 'mvn clean install' //Same as running on local
}
}
I took out the move step and comment just to make it clearer.
I didn't get a null pointer error. See if removeing the step call and removing the step call make the NPE go away. If not, I'd suggest attaching the console output to attempt to see where this happens.

Post always not executed on declarative pipeline exception

Ideally, I want to handle a failure in my declarative Jenkins pipeline and send an email to the committee. But I cannot make post to work at all. I have the following script:
pipeline {
agent any
stages {
stage('Prepare') {
steps {
cleanWs
checkout scm
}
}
}
post {
always {
echo '============'
echo 'In Post part'
echo '============'
echo currentBuild.result // this prints null
}
}
}
In my version of Jenkins cleanWs() is not defined, so the build fails with:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 11: Expected a step # line 11, column 17.
cleanWs
^
1 error
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1073)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:591)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:569)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:546)
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:129)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:123)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:516)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:479)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:253)
at hudson.model.ResourceController.execute(ResourceController.java:98)
at hudson.model.Executor.run(Executor.java:405)
Finished: FAILURE
But as you can see, there is no
============
In Post part
============
So the post always was not executed.
Your pipeline is not executed at all, see the error: MultipleCompilationErrorsException: startup failed:. As the pipeline script cannot even be compiled, a job does not start at all, so obviously post block isn't executed. Since the source cannot be compiled, jenkins is not aware of any post block anyway.

Resources