20

Jenkins version = 2.19 Jenkins Multibranch Pipeline plugin version = 2.92

I have a Jenkinsfile with a few conditional stages based on the branch.

Here is a modified for the sake of brevity version of my Jenkinsfile:

node {
    stage('Checkout') {
        checkout scm
    }

    stage('Clean Verify') {
        sh 'mvn clean verify'
    }

    if (env.BRANCH_NAME == "develop") {
        stage('Docker') {
            sh 'mvn docker:build -DpushImage'
        }
    }
}

I am using the multibranch pipeline plugin.

It successfully detects and builds all my branches.

The problem I have is that all builds report as failed even though if i hover each stage it reports 'Success'.

I have attached an image showing a feature branch where the two stages i wanted to run have run and completed with success but you can see the build has actually reported as failed.

Jenkins failed build

I get the exact same outcome for develop branch as well - it executes the Docker stage successfully but the build reports failed.

My expectation is that each branch will report success as the stages that ran for that branch all passed.

EDIT 1

Here's the end of the build log (i'm hoping this is sufficient as i didn't want to pick out all the private info but let me know if required)

[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 30.459 s
[INFO] Finished at: 2017-02-21T15:13:02+11:00
[INFO] Final Memory: 84M/769M
[INFO] ------------------------------------------------------------------------
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] sh
Required context class hudson.FilePath is missing
Perhaps you forgot to surround the code with a step that provides this, such as: node
[Pipeline] End of Pipeline
org.jenkinsci.plugins.workflow.steps.MissingContextVariableException: Required context class hudson.FilePath is missing
    at org.jenkinsci.plugins.workflow.steps.StepDescriptor.checkContextAvailability(StepDescriptor.java:253)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:179)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:126)
at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:108)
at groovy.lang.GroovyObject$invokeMethod.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:151)
at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:21)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:115)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:103)
at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:149)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:146)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:16)
at WorkflowScript.run(WorkflowScript:93)
at ___cps.transform___(Native Method)
at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:57)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:109)
at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:82)
at sun.reflect.GeneratedMethodAccessor501.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
at com.cloudbees.groovy.cps.Next.step(Next.java:58)
at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:154)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:33)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:30)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108)
at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:30)
at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:163)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:328)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$100(CpsThreadGroup.java:80)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:240)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:228)
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:63)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112)
at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
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
1
  • Can you show us what's in the build log? Commented Feb 22, 2017 at 1:38

7 Answers 7

39

So after looking more closely at the log file it helped me to track down the problem.

It's worth noting that clicking on the build stage to view the logs is what threw me - this is what I had been doing. When I actually went to the full console log output i saw the error about:

Required context class hudson.FilePath is missing
Perhaps you forgot to surround the code with a step that provides this, such as: node

Underneath the node {} section that I had I had a statement for deploys:

def branch = readFile('branch').trim()
if (branch == master) {
    ...
}

The problem was that the readFile statement was defined outside of a node.

The answer was to put the readFile statement within a node {} section.

2
  • 1
    The fact that this must execute within the context of a node is a very important detail. That is what tripped me up.
    – Brandon
    Commented Sep 25, 2017 at 14:31
  • 2
    But looking at the original post, it looks like the code is inside a node {} block?
    – Chris F
    Commented Aug 22, 2019 at 19:09
23

I know this is old, but I ran into a similar issue with a declarative pipeline and landed here. As it turns out, I was trying to use a sh to set an environment variable within the pipeline block, but my main agent was none, i.e.:

pipeline {
    agent none
    environment {
        VERSION = sh(returnStdout: true, script: 'git describe --tags')
    }
}

That resulted in the same error Required context class hudson.FilePath is missing. Moving it to a stage with an agent worked as expected.

2
  • 1
    You can define a global variable in the following way: create a variable on top of your file (before the pipeline), like so: def my_variable. Then you can instantiate this global variable within your first build step by: script { my_variable = sh(script: "do_stuff", returnStdout: true).trim() }. So you can reuse that variable now in your other stages, since it's now a global variable in your Jenkinsfile. Hope this helps.
    – Ivan Bilan
    Commented Sep 28, 2018 at 12:16
  • 1
    I had the same issue when I was using credentials("credential-id") from an environment block that had an agent with label none, which in my case was the first block before block stages. Commented Mar 26, 2019 at 15:32
6

my solution for the error Required context class hudson.FilePath is missing Perhaps you forgot to surround the code with a step that provides this, such as: node

is:

#!/usr/bin/env groovy
import hudson.model.*

node('master') {
    sh("your shell script")   
}
5

In my case, it suddenly stopped working, with the error:

Required context class hudson.FilePath is missing
Perhaps you forgot to surround the code with a step that provides this, such as: node

The reason was that the node was simply down. Had to restart it and relaunch its agent (it was slave).

2
  • Thanks for this, we get this once in every 100 jobs or so and it has been hard to figure out what is going on, but I think your comment might be giving us a clue here. thanks. Commented Oct 16, 2020 at 21:28
  • my ec2 slave nodes time was out of sync with the master nodes, rebooted and the pipeline worked. This answer gave me a hint. Thank you. Commented Apr 22, 2021 at 2:30
1

The sh command is not closed with a quote in the end.

1
  • Thanks for pointing that out. This was just a typo in my SO post though not in the Jenkinsfile itself
    – wimnat
    Commented Feb 23, 2017 at 0:25
1

This error can happen if your branch gets deleted and will show the below error:

Caused by: org.codehaus.groovy.runtime.InvokerInvocationException: org.jenkinsci.plugins.workflow.steps.MissingContextVariableException: Required context class hudson.FilePath is missing 14:25:07 Perhaps you forgot to surround the code with a step that provides this, such as: node

and THEN at the end it will also say:

ERROR: Couldn't find any revision to build. Verify the repository and branch configuration for this job.

In our case we had sometimes set a job to use a branch that was still in a PR and once the PR was merged the branch was auto-deleted (e.g. https://stackoverflow.com/a/57328204/292408).

Restore the branch in the job if this is your case and it should work again. If you are seeing this error inconsistently then this might be your issue.

2
  • 1
    happened to me on a terrible runaway build situation where someone configured each branch to build each other branch and folks were still rebasing and re-pushing when things in the queue were meant to run against commits that didn't exist anymore... thanks a lot for leaving this comment >.<
    – sinback
    Commented Oct 21, 2021 at 19:05
  • W00t! Glad it helped you, thanks! Commented Oct 21, 2021 at 21:05
0

I had this error:

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

It was caused by ambiguous interpolation:

environment {
    FILE = "some-$BRANCH.yml"
}

The correct expression in this case would be:

"some-${BRANCH}.yml"

Not the answer you're looking for? Browse other questions tagged or ask your own question.