Why is Jenkins caching my old broken Jenkinsfile? - docker

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.

Related

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.

Using activeChoiceReactiveParam in pipeline

How to use activeChoiceReactiveParam in Jenkins pipeline?
this is the relevant part of my jenkins file:
parameters{
activeChoiceParam('foo') {
description('zzzz')
filterable()
choiceType('SINGLE_SELECT')
groovyScript {
script('xx')
fallbackScript('xx')
}
}
}
Getting this error when running the build:
WorkflowScript: 10: Build parameters definitions cannot have blocks # line 10, column 6.
activeChoiceParam('foo') {
^
I think that as this post says, pipeline doesn't support activeChoiceParams.
That post is from 2 years ago but seems to be valid still; the Active Choices plugin repo links a Jenkins issue which blocks pipeline from working well with the plugin, and their pipeline example is similar to what they described in the post I linked.

Cant use changeset in JenkinsFile script

I have a problem with a step in my jenkinsfile script. I am trying to use when changeset to determine if a particular set of files has changed as I want to only build when certain files are changed. I added this step to call a separate build job if the files are changed.
stage('File check') {
when { changeset "**/files"}
steps {
build 'Build and deploy'
}
}
However I get an error
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 5: Unknown conditional changeset. Valid conditionals are: allOf, anyOf, branch, environment, expression, not # line 5, column 21.
when { changeset "**/files"}
What am I missing? Is it a problem with my version of Jenkins/groovy? Im using Jenkins ver. 2.73.3.
Here is what works for me:
String projectDirectory = "terraform"
String changesetPathRegex = "**/${projectDirectory}/**"
stage('Build Dockerfile') {
when { changeset changesetPathRegex }
steps {
dir(projectDirectory) {
sh 'terraform plan'
}
}
}
Should look in the current repo's "terraform" folders and do the things if it sees a change there.

Jenkins Multibranch Pipeline workspace configuration

I'm bumping up against JENKINS-38706. And since its been open for a while, i'm trying to work around it.
My problem is that i'm running a multinode pipeline, with one of the nodes being a windows slave, with the 255 character path limitations.
So, i'm trying to change the workspace for my windows slave stages, and instead of using C:\jenkins\workspace\job-branch-randomcharacters that the multibranch pipeline uses, i'm trying to move it to c:\w\job\branch.
It immediately fails with:
Branch indexing
Obtained Jenkinsfile from 5bc168fcd5b3707048ad4bca4b5ef7478d759531
Running in Durability level: MAX_SURVIVABILITY
[BFA] Scanning build for known causes...
[BFA] No failure causes found
[BFA] Done. 0s
[Bitbucket] Notifying commit build result
[Bitbucket] Build result notified
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 52: Too many arguments for map key "ws" # line 52, column 15.
ws('C:\\w\\$JOB_NAME\\$BRANCH_NAME') {
My Jenkinsfile snippet:
stage ('Snapshot-WINDOWS') {
agent {
node {
label 'win'
ws('C:\\w\\$JOB_NAME\\$BRANCH_NAME') {
body()
}
}
}
steps {
withMaven(
maven: 'Maven 3.5.3',
mavenSettingsConfig: 'settings'
) {
bat 'mvn clean install'
}
}
}
To answer my own question, instead of using ws() i needed to use customWorkspace, and the $BRANCH_NAME gets automatically added with multibranch pipelines.
stage ('Snapshot-WINDOWS') {
agent {
node {
label 'win'
customWorkspace 'C:\\w\\$JOB_NAME'
}
}
steps {
withMaven(
maven: 'Maven 3.5.3',
mavenSettingsConfig: 'settings'
) {
bat 'mvn clean install'
}
}
}
You forgot to declare the workspace.
Example:
def wsDir = "/some/path/${env.BRANCH_NAME}"
ws (wsDir) {
// some block
}

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