A Jenkins file that I'm using from a community repo is failing. The owners of the repo say there's likely something wrong with my Jenkins build. Any thoughts on what would cause the error in this step? I have WORK_DIR defined as an environment variable in Jenkins Global configuration.
Error is:
Error when executing always post condition:
org.jenkinsci.plugins.workflow.steps.MissingContextVariableException: Required context class hudson.FilePath is missing
always { dir( "${env.WORK_DIR}" ) {
cucumber reportTitle: 'Apickli test report',
fileIncludePattern: '**/reports.json',
jsonReportDirectory: "target",
sortingMethod: 'ALPHABETICAL',
trendsLimit: 10
} }```
Related
I'm trying to test simple Jenkinsfile Pipeline using Jenkins Pipeline Unit and I have an issue with plugin ansiColor.
I added plugin to build.gradle file:
dependencies {
[...]
testImplementation 'org.jenkins-ci.plugins:ansicolor:1.0.0'
}
I added option to Jenkinsfile
pipeline {
options {
ansiColor('xterm')
[...]
}
After running "gradle clean test" I received an error:
Test testExamplePipeline FAILED (2.2s)
groovy.lang.MissingMethodException: No signature of method: Jenkinsfile.ansiColor() is applicable for argument types: (String) values: [xterm]
at app//TestExampleJenkinsfile.testExamplePipeline(TestExampleJenkinsfile.groovy:16)
How to correctly add plugin to Jenkins Pipeline Unit test?
PS:
Code is available here:
https://github.com/shinhf/jenkins-jpu-test-example
PS2
I know I can always register method using registerAllowedMethod, but I think it's not resolution:
helper.registerAllowedMethod("ansiColor", [String], null)
My Jenkins deployment is failing with the following error:
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] cucumber
Error when executing always post condition:
org.jenkinsci.plugins.workflow.steps.MissingContextVariableException: Required context class hudson.FilePath is missing
Perhaps you forgot to surround the code with a step that provides this, such as: node
Here is where cucumber is defined:
always {
cucumber reportTitle: 'Apickli test report',
fileIncludePattern: '**/reports.json',
jsonReportDirectory: "target",
sortingMethod: 'ALPHABETICAL',
trendsLimit: 10
}
}
This is a new Jenkins build and this same deployment works on the "old" server. I have the cucumber plugin added. Could I be missing another one?
I'm new to Jenkins and figured out that this always {} statement runs after each stage and needed to fix an issue in the first stage.
I'm newbie to Jenkins pipeline and writing a groovy script to parse a json file. However I'm facing an error which many have faced but none of the solutions worked for me. Below is my Jenkinsfile and error msg.
def envname = readJSON file: '${env.WORKSPACE}/manifest.json'
pipeline {
agent any
stages {
stage('Build') {
steps {
echo WORKSPACE
sh "ls -a ${WORKSPACE}"
}
}
}
}
[Pipeline] Start of Pipeline
[Pipeline] readJSON
[Pipeline] End of Pipeline
org.jenkinsci.plugins.workflow.steps.MissingContextVariableException:
Required context class hudson.FilePath is missing Perhaps you forgot
to surround the code with a step that provides this, such as: node at
org.jenkinsci.plugins.pipeline.utility.steps.AbstractFileOrTextStepExecution.run(AbstractFileOrTextStepExecution.java:30)
I even tried readJSON file: '${WORKSPACE}/manifest.json but that didn't work too. I'm sure the mistake is with the first line since when removing that line, there execution is successful. The docs are pretty helpful but I'm not able to track down where exactly I'm going wrong that is why posted here.
UPDATE:
I tried the following methods def envname = readJSON file: "./manifest.json" and def envname = readJSON file: "${env.WORKSPACE}/manifest.json" and even tried them defining under the steps block. Nothing worked. Below is the error msg I recieved when I defined them under step block
WorkflowScript: 5: Expected a step # line 7, column 13
def envname =
^
Below is the official syntax doc of readJson and I can see that I'm using the correct syntax only. but still doesn't work as expected.
https://www.jenkins.io/doc/pipeline/steps/pipeline-utility-steps/#readjson-read-json-from-files-in-the-workspace
'${env.WORKSPACE}/manifest.json' is interpolating the Groovy env map as a shell variable. You need to interpolate it as a Groovy variable like "${env.WORKSPACE}/manifest.json".
sh "ls -a ${WORKSPACE}" is interpolating the shell environment variable WORKSPACE as a Groovy variable. You need to interpolate it as a shell variable like sh 'ls -a ${WORKSPACE}'.
echo WORKSPACE is attempting to resolve the shell variable WORKSPACE as a first class Groovy variable expression. You need to use the Groovy env map instead like echo env.WORKSPACE.
As for the global variable indefinite type assignment on the first line: if it still throws the error above after making those fixes, then it may be due to invalid use of scripted syntax in a declarative syntax pipeline. You likely need to place it inside a step block within your pipeline in that case.
I've solved this myself with the help of "Matt Schuchard"'s below answer. I'm not sure whether this is the only way to solve but this worked for me.
pipeline {
agent any
stages {
stage('Json-Build') {
steps {
script {
def envname = readJSON file: "${env.WORKSPACE}/manifest.json"
element1 = "${envname.dev}"
echo element1
}
}
}
}
}
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.
Unable to get output of Failing Jenkins jobs to pipeline's console
I'm calling several jobs from my pipeline. Below is an example job 'printuser' that gets triggered from my pipeline.
I'm able to get the output of the job on my pipeline console which is my requirement.
pipeline {
agent none
stages {
stage ("Check Parameters"){
steps {
echo "In pipeline"
script {
echo "Start condition check"
}
def slaveJob = build job: 'printuser'
println slaveJob.rawBuild.log
}
}
}
}
The issue happens when 'printuser' fails in which case the failing output does not show in the pipeline console page.
I tried adding the println to the post failure block like below:
post {
failure {
script {
println slaveJob.rawBuild.log
}
}
}
But, I get the below error in doing so.
Error when executing failure post condition:
groovy.lang.MissingPropertyException: No such property: slaveJob for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:270)
Can you please suggest how can I get the output of "FAILING" jobs to the pipeline console ?
On your post block you can print the whole job build log.
post {
failure {
echo Jenkins.getInstance().getItemByFullName('printuser').getLastBuild().logFile.text
}
}
Note that you should approve specific signatures to allow pipeline and groovy to access the above functions.
If you haven't done this yet, the above script will fail with
Start condition check Scripts not permitted to use staticMethod
jenkins.model.Jenkins getInstance. Administrators can decide whether
to approve or reject this signature.
You can follow the error message by clicking on the link of the message which will redirect you to the scriptApproval page of Jenkins where you can add the signature that needs approval to the approval list.
You should have the following list of approved signatures for the above to work
method hudson.model.ItemGroup getItem java.lang.String
method hudson.model.Job getLastBuild
method hudson.model.Run getLogFile
method jenkins.model.Jenkins getItemByFullName java.lang.String
staticMethod jenkins.model.Jenkins getInstance
staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods getText java.io.File
If you follow this solution, you can change the post's failure action to always so it print the log always (not only on errors) and remove the println slaveJob.rawBuild.log
When using the def keyword a variable has block scope. Try slaveJob = build job: 'printuser' without the def.