Post always not executed on declarative pipeline exception - jenkins

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.

Related

Want to move the entire post steps in Jenkins Global Shared library

I am new to Jenkins and I want to use Jenkins shared library for my post build steps so that I don't have to write them again and again but the problem is when I call my groovy method in the post section of the pipeline it gives the following error.
WorkflowScript: 12: The ‘post’ section can only contain build condition names with code blocks. Valid condition names are [always, changed, fixed, regression, aborted, success, unsuccessful, unstable, failure, notBuilt, cleanup] # line 12, column 9.
build()
^
WorkflowScript: 11: post can not be empty # line 11, column 5.
post {
^
2 errors
My pipeline syntax:
#Library("new-library") _
pipeline {
agent any
stages {
stage('Hello') {
steps {
echo 'Hello world!'
}
}
}
post {
build()
}
}
build is the name of the groovy file.
My groovy file which is on GitHub
def call() {
always {
echo 'This will always run'
}
success {
echo 'This will run only if successful'
}
unstable {
echo 'This will run only if run was marked as unstable'
}
changed {
echo 'This will run only if the state of the pipeline is changed'
echo 'FOR EXAMPLE, if the pipeline was previously failed but not it is successful'
}
}
Simply I want my post steps to be trigger from the Global Shared Library. If there is some other method do let me know as well.
Thank you in advance.

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.

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.

Jenkins pipeline script error WorkflowScript: Expected a step

I have created a testcase from a much bigger Jenkins pipeline project and it works fine.
pipeline {
agent none
stages {
stage ("Check Parameters")
{
steps {
echo "In pipeline"
script {
echo "Start condition check"
}
build job: 'printuser'
//def slaveJob = build job: 'printuser'
//println slaveJob.rawBuild.log
}
}
}
}
Howevr, inorder to get the output of printuser job onto the pipeline i comment build job: 'printuser' and uncomment the slaveJob code as below:
//build job: 'printuser'
def slaveJob = build job: 'printuser'
println slaveJob.rawBuild.log
It now fails with the below error:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 13: Expected a step # line 13, column 15.
def slaveJob = build job: 'printuser'
^
1 error
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)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
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:133)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:126)
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:320)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:429)
Finished: FAILURE
Can you please suggest what is the issue here ?
If you want to use groovy, you must put it inside the script section, not outside:
script {
echo "Start condition check"
build job: 'printuser'
def slaveJob = build job: 'printuser'
println slaveJob.rawBuild.log
}
PS: not sure your code will work as such, but you should at least put it inside the script part. Outside of the script closure, jenkins will only accept a set of predefined steps.

How to use jenkins version number plugin in pipeline

I am using version number jenkin plugin in Jenkins ver. 2.107.2. below pipeline code is not working. any help to fix this issue?
pipeline code
pipeline {
agent any
stages {
stage('Pre-Build') {
steps {
sh 'echo Building Docker'
def tag = VersionNumber (versionNumberString: '${BUILD_DATE_FORMATTED, "yyyyMMdd"}-develop-${BUILDS_TODAY}')
}
}
}
}
Error:
Running in Durability level: MAX_SURVIVABILITY
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 8: Expected a step # line 8, column 1.
def tag = VersionNumber (versionNumberString: '${BUILD_DATE_FORMATTED, "yyyyMMdd"}-develop-${BUILDS_TODAY}')
^
1 error
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)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
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:131)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:125)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:560)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:521)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:325)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:429)
Finished: FAILURE
this link How to use jenkins version number plugin in Jenkinsfile? has answer but not working for me.
You are using the declarative Pipeline, which has some restrictions regarding the commands which can be run inside a stage.
Take a look at the syntax guide for declarative pipelines.
You can either use the scripted pipeline or declare the variable in an environment block:
environment {
tag = VersionNumber(versionNumberString: '${BUILD_DATE_FORMATTED,"yyyyMMdd"}-develop-${BUILDS_TODAY}');
}
and later use it as $tag in your pipeline.

Resources