Jenkinsfile 2 erros - jenkins

this is my simple jenkinsfile:
pipeline {
agent any
stages {
stages ('Build') {
steps {
sh 'echo "Hello World" '
sh '''
echo "Multiline shell steps works too"
ls -lah
'''
}
}
}
}
But when i was executing this in jenkins i'm getting the below errors:
Branch indexing
Connecting to https://api.github.com using admin/****** (GitHub Access Token)
Obtained Jenkinsfile from 030d6e5dc55356678255ca506b05e8cc223d15a6
Running in Durability level: MAX_SURVIVABILITY
GitHub has been notified of this commit’s build result
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 4: Expected a stage # line 4, column 9.
stages ('Build') {
^
WorkflowScript: 3: No stages specified # line 3, column 5.
stages {
^
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)
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: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:337)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:428)
Finished: FAILURE
Any idea what i am missing here. Thanks

Related

Import properties file from jenkins

I'm using Jenkins 2.289.2 on RHEL8.
I tried to read a property file from the pipeline.
def properties = readProperties file :'my.properties'
When I build the project I got the below error. I installed Plugin Utilities API plugin in Jenkins. Am I missing any steps?
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 35: Expected a step # line 35, column 9.
def properties = readProperties file :'my.properties''
^
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: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:337)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:429)
Finished: FAILURE
If you are using a declarative pipeline this kind of statement must reside inside a script block. In general any scripted pipeline syntax or plain groovy DSL should be encapsulated inside a script block when used in a declarative pipeline.
You can read more about it in the Pipeline Syntax documentation.
To solve it try using something like:
pipeline {
agent any
stages {
stage('Hello') {
steps {
script {
def properties = readProperties file :'my.properties'
// Rest of code
}
}
}
}
}

How to set docker file in jenkin pipeline stage

I have a simple JenkinsFile uploaded in my git repo
pipeline {
agent any
stages {
stage('Initialize'){
def dockerHome = tool 'myDocker'
env.PATH = "${dockerHome}/bin:${env.PATH}"
}
stage('Push to Docker Registry'){
withCredentials([usernamePassword(credentialsId: 'dockerHubAccount', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
pushToImage(CONTAINER_NAME, CONTAINER_TAG, USERNAME, PASSWORD)
}
}
}
}
In jenkins, i have created a pipeline project and just passed path to my git repo.
myDocker is defined in global tool configuration.
dockerhub credentials are defined in jenkins->credentials.
On build , i am getting this:
Started by user ********
Obtained Jenkinsfile from git https://github.com/*****/*****.git
Running in Durability level: MAX_SURVIVABILITY
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 4: Not a valid stage section definition: "def dockerHome = tool 'myDocker'". Some extra configuration is required. # line 4, column 9.
stage('Initialize'){
^
WorkflowScript: 4: Not a valid stage section definition: "env.PATH = "${dockerHome}/bin:${env.PATH}"". Some extra configuration is required. # line 4, column 9.
stage('Initialize'){
^
WorkflowScript: 8: Unknown stage section "withCredentials". Starting with version 0.5, steps in a stage must be in a ‘steps’ block. # line 8, column 9.
stage('Push to Docker Registry'){
^
WorkflowScript: 4: Expected one of "steps", "stages", or "parallel" for stage "Initialize" # line 4, column 9.
stage('Initialize'){
^
WorkflowScript: 8: Expected one of "steps", "stages", or "parallel" for stage "Push to Docker Registry" # line 8, column 9.
stage('Push to Docker Registry'){
^
5 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)
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: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:428)
Finished: FAILURE
I am sure , i am making any small mistake. Please redirect me to right direction.
You need to wrap your code within steps inside a stage like below.
pipeline {
agent any
stages {
stage('Initialize'){
steps {
def dockerHome = tool 'myDocker'
env.PATH = "${dockerHome}/bin:${env.PATH}" }
}
stage('Push to Docker Registry'){
steps {
withCredentials([usernamePassword(credentialsId: 'dockerHubAccount', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
pushToImage(CONTAINER_NAME, CONTAINER_TAG, USERNAME, PASSWORD)
}
}
}
}}
Documentation Link. Creating Pipeline

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.

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