Jenkins Pipeline conditional stage succeeds but Jenkins shows build as failed

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.


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.